Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature-go/peer-5 #311

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions go-backend/api/coreValues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package api

import (
"encoding/json"
"fmt"
"net/http"

"joshsoftware/peerly/apperrors"
"joshsoftware/peerly/pkg/dto"
corevalues "joshsoftware/peerly/service/coreValues"

"github.com/gorilla/mux"
logger "github.com/sirupsen/logrus"
)

func listCoreValuesHandler(coreValueSvc corevalues.Service) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
var organisationID string = ""
if vars["organisation_id"] != "" {
organisationID = vars["organisation_id"]
}

coreValues, err := coreValueSvc.ListCoreValues(req.Context(), organisationID)
if err != nil {

apperrors.ErrorResp(rw, err)
return
}

dto.Repsonse(rw, http.StatusOK, dto.SuccessResponse{Data: coreValues})
})
}

func getCoreValueHandler(coreValueSvc corevalues.Service) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)

coreValue, err := coreValueSvc.GetCoreValue(req.Context(), vars["organisation_id"], vars["id"])
if err != nil {
apperrors.ErrorResp(rw, err)
return
}

dto.Repsonse(rw, http.StatusOK, dto.SuccessResponse{Data: coreValue})
})
}

func createCoreValueHandler(coreValueSvc corevalues.Service) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
const userId int64 = 1
var coreValue dto.CreateCoreValueReq
err := json.NewDecoder(req.Body).Decode(&coreValue)
if err != nil {
logger.WithField("err", err.Error()).Error("Error while decoding request data")
err = apperrors.JSONParsingErrorReq
apperrors.ErrorResp(rw, err)
return
}

fmt.Println("orgId (vars) = ", vars["organisation_id"])
resp, err := coreValueSvc.CreateCoreValue(req.Context(), vars["organisation_id"], userId, coreValue)
if err != nil {

apperrors.ErrorResp(rw, err)
return
}

dto.Repsonse(rw, http.StatusCreated, dto.SuccessResponse{Data: resp})
})
}

func deleteCoreValueHandler(coreValueSvc corevalues.Service) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
const userId int64 = 1
err := coreValueSvc.DeleteCoreValue(req.Context(), vars["organisation_id"], vars["id"], userId)
if err != nil {

apperrors.ErrorResp(rw, err)
return
}

dto.Repsonse(rw, http.StatusOK, dto.SuccessResponse{Data: "Delete successful"})
})
}

func updateCoreValueHandler(coreValueSvc corevalues.Service) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)

var updateReq dto.UpdateQueryRequest
err := json.NewDecoder(req.Body).Decode(&updateReq)
if err != nil {
logger.WithField("err", err.Error()).Error("Error while decoding request data")
err = apperrors.JSONParsingErrorReq
apperrors.ErrorResp(rw, err)
return
}

resp, err := coreValueSvc.UpdateCoreValue(req.Context(), vars["organisation_id"], vars["id"], updateReq)
if err != nil {
apperrors.ErrorResp(rw, err)
return
}

dto.Repsonse(rw, http.StatusOK, dto.SuccessResponse{Data: resp})
})
}
Loading