Skip to content

Commit

Permalink
Merge pull request #1224 from Avanade/1217-refactor-getapprovaltypebyid
Browse files Browse the repository at this point in the history
refactor get approval type by id
  • Loading branch information
jerricotandelacruz authored Nov 13, 2024
2 parents c51efe5 + 14b4f82 commit 6176dd3
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions src/goapp/controller/approval-type/approval-type-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"main/service"
"net/http"
"strconv"

"github.com/gorilla/mux"
)

type approvalTypeController struct {
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions src/goapp/model/approval-type.go
Original file line number Diff line number Diff line change
@@ -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"`
}
39 changes: 0 additions & 39 deletions src/goapp/pkg/ghmgmtdb/approvalTypesDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions src/goapp/repository/approval-type/approval-type-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/goapp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
34 changes: 0 additions & 34 deletions src/goapp/routes/api/approvalTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (

type ApprovalTypeService interface {
Get(opt *model.FilterOptions) ([]model.ApprovalType, int64, error)
GetById(id int) (*model.ApprovalType, error)
}
14 changes: 14 additions & 0 deletions src/goapp/service/approval-type/approval-type-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6176dd3

Please sign in to comment.