From 14b4f82299e449eb1e0a91869b7a9083bdadde90 Mon Sep 17 00:00:00 2001 From: Ismael Ibuan <102030576+iibuan@users.noreply.github.com> Date: Wed, 13 Nov 2024 18:53:10 +0800 Subject: [PATCH] refactor get approval type by id Signed-off-by: Ismael Ibuan <102030576+iibuan@users.noreply.github.com> --- .../approval-type-controller-interface.go | 1 + .../approval-type/approval-type-controller.go | 21 ++++++++++ src/goapp/model/approval-type.go | 6 +++ src/goapp/pkg/ghmgmtdb/approvalTypesDb.go | 39 ------------------- .../approval-type-repository-interface.go | 1 + .../approval-type/approval-type-repository.go | 21 ++++++++++ src/goapp/routes.go | 2 +- src/goapp/routes/api/approvalTypes.go | 34 ---------------- .../approval-type-service-interface.go | 1 + .../approval-type/approval-type-service.go | 14 +++++++ 10 files changed, 66 insertions(+), 74 deletions(-) diff --git a/src/goapp/controller/approval-type/approval-type-controller-interface.go b/src/goapp/controller/approval-type/approval-type-controller-interface.go index a8376faa..09d3ec44 100644 --- a/src/goapp/controller/approval-type/approval-type-controller-interface.go +++ b/src/goapp/controller/approval-type/approval-type-controller-interface.go @@ -3,5 +3,6 @@ package approvaltype import "net/http" type ApprovalTypeController interface { + GetApprovalTypeById(w http.ResponseWriter, r *http.Request) GetApprovalTypes(w http.ResponseWriter, r *http.Request) } diff --git a/src/goapp/controller/approval-type/approval-type-controller.go b/src/goapp/controller/approval-type/approval-type-controller.go index b9b30854..f3f48153 100644 --- a/src/goapp/controller/approval-type/approval-type-controller.go +++ b/src/goapp/controller/approval-type/approval-type-controller.go @@ -7,6 +7,8 @@ import ( "main/service" "net/http" "strconv" + + "github.com/gorilla/mux" ) type approvalTypeController struct { @@ -17,6 +19,25 @@ func NewApprovalTypeController(service *service.Service) ApprovalTypeController return &approvalTypeController{service} } +func (c *approvalTypeController) GetApprovalTypeById(w http.ResponseWriter, r *http.Request) { + logger := appinsights_wrapper.NewClient() + defer logger.EndOperation() + + vars := mux.Vars(r) + id, _ := strconv.Atoi(vars["id"]) + + approvalType, err := c.ApprovalType.GetById(id) + if err != nil { + logger.TrackException(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(approvalType) +} + func (c *approvalTypeController) GetApprovalTypes(w http.ResponseWriter, r *http.Request) { logger := appinsights_wrapper.NewClient() defer logger.EndOperation() diff --git a/src/goapp/model/approval-type.go b/src/goapp/model/approval-type.go index 794c5a61..3ec17279 100644 --- a/src/goapp/model/approval-type.go +++ b/src/goapp/model/approval-type.go @@ -1,9 +1,15 @@ package model +import "time" + type ApprovalType struct { Id int `json:"id"` Name string `json:"name"` Approvers []RepositoryApprover `json:"approvers"` IsActive bool `json:"isActive"` IsArchived bool `json:"isArchived"` + Created time.Time `json:"created"` + CreatedBy string `json:"createdBy"` + Modified time.Time `json:"modified"` + ModifiedBy string `json:"modifiedBy"` } diff --git a/src/goapp/pkg/ghmgmtdb/approvalTypesDb.go b/src/goapp/pkg/ghmgmtdb/approvalTypesDb.go index d6e5d884..82ca57d4 100644 --- a/src/goapp/pkg/ghmgmtdb/approvalTypesDb.go +++ b/src/goapp/pkg/ghmgmtdb/approvalTypesDb.go @@ -26,45 +26,6 @@ func GetAllActiveApprovers() interface{} { return result } -func SelectApprovalTypeById(id int) (*ApprovalType, error) { - db := ConnectDb() - defer db.Close() - - param := map[string]interface{}{ - "Id": id, - } - - result, err := db.ExecuteStoredProcedureWithResult("usp_RepositoryApprovalType_Select_ById", param) - if err != nil { - return nil, err - } - - approvalType := ApprovalType{ - Id: int(result[0]["Id"].(int64)), - Name: result[0]["Name"].(string), - IsActive: result[0]["IsActive"].(bool), - IsArchived: result[0]["IsArchived"].(bool), - } - - if result[0]["Created"] != nil { - approvalType.Created = result[0]["Created"].(time.Time) - } - - if result[0]["CreatedBy"] != nil { - approvalType.CreatedBy = result[0]["CreatedBy"].(string) - } - - if result[0]["Modified"] != nil { - approvalType.Modified = result[0]["Modified"].(time.Time) - } - - if result[0]["ModifiedBy"] != nil { - approvalType.ModifiedBy = result[0]["ModifiedBy"].(string) - } - - return &approvalType, nil -} - func InsertApprovalType(approvalType ApprovalType) (int, error) { db := ConnectDb() defer db.Close() diff --git a/src/goapp/repository/approval-type/approval-type-repository-interface.go b/src/goapp/repository/approval-type/approval-type-repository-interface.go index cc2ae14f..8073938a 100644 --- a/src/goapp/repository/approval-type/approval-type-repository-interface.go +++ b/src/goapp/repository/approval-type/approval-type-repository-interface.go @@ -6,6 +6,7 @@ import ( type ApprovalTypeRepository interface { Select() ([]model.ApprovalType, error) + SelectById(id int) (*model.ApprovalType, error) SelectByOption(opt model.FilterOptions) ([]model.ApprovalType, error) Total() (int64, error) } diff --git a/src/goapp/repository/approval-type/approval-type-repository.go b/src/goapp/repository/approval-type/approval-type-repository.go index 3567a8e4..f0801c47 100644 --- a/src/goapp/repository/approval-type/approval-type-repository.go +++ b/src/goapp/repository/approval-type/approval-type-repository.go @@ -39,6 +39,27 @@ func (r *approvalTypeRepository) Select() ([]model.ApprovalType, error) { return result, nil } +func (r *approvalTypeRepository) SelectById(id int) (*model.ApprovalType, error) { + row, err := r.QueryRow("usp_RepositoryApprovalType_Select_ById", sql.Named("Id", id)) + if err != nil { + return nil, err + } + + approvalType := model.ApprovalType{} + err = row.Scan( + &approvalType.Id, + &approvalType.Name, + &approvalType.IsArchived, + &approvalType.IsActive, + &approvalType.Created, + &approvalType.CreatedBy, + &approvalType.Modified, + &approvalType.ModifiedBy, + ) + + return &approvalType, err +} + func (r *approvalTypeRepository) SelectByOption(opt model.FilterOptions) ([]model.ApprovalType, error) { rows, err := r.Query("usp_RepositoryApprovalType_Select_ByOption", sql.Named("Offset", opt.Offset), diff --git a/src/goapp/routes.go b/src/goapp/routes.go index 0f577919..dfa35897 100644 --- a/src/goapp/routes.go +++ b/src/goapp/routes.go @@ -202,7 +202,7 @@ func setApiRoutes() { httpRouter.PUT("/api/approval-types/{id}/archived", m.Chain(rtApi.SetIsArchivedApprovalTypeById, m.AzureAuth())) httpRouter.GET("/api/approval-types", m.Chain(cont.ApprovalType.GetApprovalTypes, m.AzureAuth())) httpRouter.GET("/api/approval-types/active", m.Chain(rtApi.GetActiveApprovalTypes, m.AzureAuth(), m.GitHubAuth())) - httpRouter.GET("/api/approval-types/{id}", m.Chain(rtApi.GetApprovalTypeById, m.AzureAuth())) + httpRouter.GET("/api/approval-types/{id}", m.Chain(cont.ApprovalType.GetApprovalTypeById, m.AzureAuth())) //EXTERNAL LINKS API httpRouter.GET("/api/external-links", m.Chain(cont.ExternalLink.GetExternalLinks, m.AzureAuth(), m.IsUserAdmin())) diff --git a/src/goapp/routes/api/approvalTypes.go b/src/goapp/routes/api/approvalTypes.go index 8686d1ab..df3d4a56 100644 --- a/src/goapp/routes/api/approvalTypes.go +++ b/src/goapp/routes/api/approvalTypes.go @@ -27,40 +27,6 @@ type ApproverDto struct { ApproverName string `json:"approverName"` } -func GetApprovalTypeById(w http.ResponseWriter, r *http.Request) { - logger := appinsights_wrapper.NewClient() - defer logger.EndOperation() - - vars := mux.Vars(r) - id, _ := strconv.Atoi(vars["id"]) - - result, err := db.SelectApprovalTypeById(id) - if err != nil { - logger.LogException(err) - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - approversDto, err := getApproversByApprovalTypeId(result.Id) - if err != nil { - logger.LogException(err) - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - approvalTypeDto := ApprovalTypeDto{ - Id: result.Id, - Name: result.Name, - Approvers: *approversDto, - IsActive: result.IsActive, - IsArchived: result.IsArchived, - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(approvalTypeDto) -} - func CreateApprovalType(w http.ResponseWriter, r *http.Request) { logger := appinsights_wrapper.NewClient() defer logger.EndOperation() diff --git a/src/goapp/service/approval-type/approval-type-service-interface.go b/src/goapp/service/approval-type/approval-type-service-interface.go index ae1dcffc..136ec2b6 100644 --- a/src/goapp/service/approval-type/approval-type-service-interface.go +++ b/src/goapp/service/approval-type/approval-type-service-interface.go @@ -6,4 +6,5 @@ import ( type ApprovalTypeService interface { Get(opt *model.FilterOptions) ([]model.ApprovalType, int64, error) + GetById(id int) (*model.ApprovalType, error) } diff --git a/src/goapp/service/approval-type/approval-type-service.go b/src/goapp/service/approval-type/approval-type-service.go index 1f211e16..dbee60dc 100644 --- a/src/goapp/service/approval-type/approval-type-service.go +++ b/src/goapp/service/approval-type/approval-type-service.go @@ -36,3 +36,17 @@ func (s *approvalTypeService) Get(opt *model.FilterOptions) ([]model.ApprovalTyp return approvalTypes, total, nil } + +func (s *approvalTypeService) GetById(id int) (*model.ApprovalType, error) { + data, err := s.Repository.ApprovalType.SelectById(id) + if err != nil { + return nil, err + } + + data.Approvers, err = s.Repository.Approver.SelectByApprovalTypeId(data.Id) + if err != nil { + return nil, err + } + + return data, nil +}