Skip to content

Commit

Permalink
refactor create approval type
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael Ibuan <[email protected]>
  • Loading branch information
iibuan committed Nov 18, 2024
1 parent 6176dd3 commit 726037a
Show file tree
Hide file tree
Showing 23 changed files with 236 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package approvaltype
import "net/http"

type ApprovalTypeController interface {
CreateApprovalType(w http.ResponseWriter, r *http.Request)
GetApprovalTypeById(w http.ResponseWriter, r *http.Request)
GetApprovalTypes(w http.ResponseWriter, r *http.Request)
}
48 changes: 47 additions & 1 deletion src/goapp/controller/approval-type/approval-type-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package approvaltype

import (
"encoding/json"
"fmt"
"main/model"
"main/pkg/appinsights_wrapper"
"main/pkg/session"
"main/service"
"net/http"
"strconv"
Expand All @@ -19,6 +21,50 @@ func NewApprovalTypeController(service *service.Service) ApprovalTypeController
return &approvalTypeController{service}
}

func (c *approvalTypeController) CreateApprovalType(w http.ResponseWriter, r *http.Request) {
logger := appinsights_wrapper.NewClient()
defer logger.EndOperation()

sessionaz, _ := session.Store.Get(r, "auth-session")
iprofile := sessionaz.Values["profile"]
profile := iprofile.(map[string]interface{})
username := fmt.Sprint(profile["preferred_username"])

var approvalType model.ApprovalType
json.NewDecoder(r.Body).Decode(&approvalType)
approvalType.CreatedBy = username
id, err := c.ApprovalType.Insert(&approvalType)
if err != nil {
logger.TrackException(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

for _, v := range approvalType.Approvers {
err = c.User.Create(&model.User{
UserPrincipalName: v.ApproverEmail,
Name: v.ApproverName,
GivenName: "",
Surname: "",
JobTitle: "",
})
if err != nil {
logger.TrackException(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

v.ApprovalTypeId = id
err := c.RepositoryApprover.Create(&v)
if err != nil {
logger.TrackException(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
json.NewEncoder(w).Encode(id)
}

func (c *approvalTypeController) GetApprovalTypeById(w http.ResponseWriter, r *http.Request) {
logger := appinsights_wrapper.NewClient()
defer logger.EndOperation()
Expand Down Expand Up @@ -74,7 +120,7 @@ func (c *approvalTypeController) GetApprovalTypes(w http.ResponseWriter, r *http
}

for i, v := range approvalTypes {
approvers, err := c.Approver.Get(v.Id)
approvers, err := c.RepositoryApprover.Get(v.Id)
if err != nil {
logger.TrackException(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
6 changes: 4 additions & 2 deletions src/goapp/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var (
r.NewApprover(&db),
r.NewContributionArea(&db),
r.NewExternalLink(&db),
r.NewOssContributionSponsor(&db))
r.NewOssContributionSponsor(&db),
r.NewUser(&db))

serv = s.NewService(
s.NewActivityService(repo),
Expand All @@ -33,7 +34,8 @@ var (
s.NewContributionAreaService(repo),
s.NewEmailService(conf),
s.NewExternalLinkService(repo),
s.NewOssContributionSponsorService(repo))
s.NewOssContributionSponsorService(repo),
s.NewUserService(repo))

cont = c.NewController(
c.NewActivityController(serv),
Expand Down
9 changes: 9 additions & 0 deletions src/goapp/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package model

type User struct {
UserPrincipalName string `json:"userPrincipalName"`
Name string `json:"name"`
GivenName string `json:"givenName"`
Surname string `json:"surname"`
JobTitle string `json:"jobTitle"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type ApprovalTypeRepository interface {
Insert(*model.ApprovalType) (int, error)
Select() ([]model.ApprovalType, error)
SelectById(id int) (*model.ApprovalType, error)
SelectByOption(opt model.FilterOptions) ([]model.ApprovalType, error)
Expand Down
19 changes: 19 additions & 0 deletions src/goapp/repository/approval-type/approval-type-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ func NewApprovalTypeRepository(db *db.Database) ApprovalTypeRepository {
return &approvalTypeRepository{db}
}

func (r *approvalTypeRepository) Insert(approvalType *model.ApprovalType) (int, error) {
row, err := r.QueryRow("usp_RepositoryApprovalType_Insert",
sql.Named("Name", approvalType.Name),
sql.Named("IsActive", approvalType.IsActive),
sql.Named("CreatedBy", approvalType.CreatedBy),
)
if err != nil {
return 0, err
}

var id int
err = row.Scan(&id)
if err != nil {
return 0, err
}

return id, nil
}

func (r *approvalTypeRepository) Select() ([]model.ApprovalType, error) {
rows, err := r.Query("usp_RepositoryApprovalType_Select")
if err != nil {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package repositoryApprover

import (
"main/model"
)

type RepositoryApproverRepository interface {
Insert(approver *model.RepositoryApprover) error
SelectByApprovalTypeId(approvalTypeId int) ([]model.RepositoryApprover, error)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package approver
package repositoryApprover

import (
"database/sql"
db "main/infrastructure/database"
"main/model"
)

type approverRepository struct {
type repositoryApproverRepository struct {
*db.Database
}

func NewApproverRepository(db *db.Database) ApproverRepository {
return &approverRepository{db}
func NewRepostioryApproverRepository(db *db.Database) RepositoryApproverRepository {
return &repositoryApproverRepository{db}
}

func (r *approverRepository) SelectByApprovalTypeId(approvalTypeId int) ([]model.RepositoryApprover, error) {
func (r *repositoryApproverRepository) Insert(approver *model.RepositoryApprover) error {
err := r.Execute("usp_RepositoryApprover_Insert",
sql.Named("RepositoryApprovalTypeId", approver.ApprovalTypeId),
sql.Named("ApproverUserPrincipalName", approver.ApproverEmail))
return err
}

func (r *repositoryApproverRepository) SelectByApprovalTypeId(approvalTypeId int) ([]model.RepositoryApprover, error) {
rows, err := r.Query("usp_RepositoryApprover_Select_ByApprovalTypeId", sql.Named("RepositoryApprovalTypeId", approvalTypeId))
if err != nil {
return nil, err
Expand Down
14 changes: 11 additions & 3 deletions src/goapp/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
rActivityHelp "main/repository/activityhelp"
rActivityType "main/repository/activitytype"
rApprovalType "main/repository/approval-type"
rApprover "main/repository/approver"
rContributionArea "main/repository/contributionarea"
rExternalLink "main/repository/externallink"
rOssContributionSponsor "main/repository/osscontributionsponsor"
rRepositoryApprover "main/repository/repository-approver"
rUser "main/repository/user"
)

type Repository struct {
Expand All @@ -19,10 +20,11 @@ type Repository struct {
ActivityHelp rActivityHelp.ActivityHelpRepository
ActivityType rActivityType.ActivityTypeRepository
ApprovalType rApprovalType.ApprovalTypeRepository
Approver rApprover.ApproverRepository
ContributionArea rContributionArea.ContributionAreaRepository
ExternalLink rExternalLink.ExternalLinkRepository
OssContributionSponsor rOssContributionSponsor.OssContributionSponsorRepository
RepositoryApprover rRepositoryApprover.RepositoryApproverRepository
User rUser.UserRepository
}

type RepositoryOptionFunc func(*Repository)
Expand Down Expand Up @@ -63,7 +65,7 @@ func NewApprovalType(db *database.Database) RepositoryOptionFunc {

func NewApprover(db *database.Database) RepositoryOptionFunc {
return func(r *Repository) {
r.Approver = rApprover.NewApproverRepository(db)
r.RepositoryApprover = rRepositoryApprover.NewRepostioryApproverRepository(db)
}
}

Expand All @@ -90,3 +92,9 @@ func NewActivityHelp(db *database.Database) RepositoryOptionFunc {
r.ActivityHelp = rActivityHelp.NewActivityHelpRepository(db)
}
}

func NewUser(db *database.Database) RepositoryOptionFunc {
return func(r *Repository) {
r.User = rUser.NewUserRepository(db)
}
}
9 changes: 9 additions & 0 deletions src/goapp/repository/user/user-repository-interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package user

import (
"main/model"
)

type UserRepository interface {
Insert(*model.User) error
}
30 changes: 30 additions & 0 deletions src/goapp/repository/user/user-repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package user

import (
"database/sql"
db "main/infrastructure/database"
"main/model"
)

type userRepository struct {
*db.Database
}

func NewUserRepository(db *db.Database) UserRepository {
return &userRepository{db}
}

func (r *userRepository) Insert(user *model.User) error {
err := r.Execute("usp_User_Insert",
sql.Named("UserPrincipalName", user.UserPrincipalName),
sql.Named("Name", user.Name),
sql.Named("GivenName", user.GivenName),
sql.Named("Surname", user.Surname),
sql.Named("JobTitle", user.JobTitle),
)
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion src/goapp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func setApiRoutes() {
httpRouter.GET("/api/popular-topics", m.Chain(rtApi.GetPopularTopics, m.AzureAuth()))

//APPROVAL TYPES API
httpRouter.POST("/api/approval-types", m.Chain(rtApi.CreateApprovalType, m.AzureAuth()))
httpRouter.POST("/api/approval-types", m.Chain(cont.ApprovalType.CreateApprovalType, m.AzureAuth()))
httpRouter.PUT("/api/approval-types/{id}", m.Chain(rtApi.EditApprovalTypeById, m.AzureAuth()))
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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import (
type ApprovalTypeService interface {
Get(opt *model.FilterOptions) ([]model.ApprovalType, int64, error)
GetById(id int) (*model.ApprovalType, error)
Insert(approvalType *model.ApprovalType) (int, error)
}
10 changes: 9 additions & 1 deletion src/goapp/service/approval-type/approval-type-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ func (s *approvalTypeService) GetById(id int) (*model.ApprovalType, error) {
return nil, err
}

data.Approvers, err = s.Repository.Approver.SelectByApprovalTypeId(data.Id)
data.Approvers, err = s.Repository.RepositoryApprover.SelectByApprovalTypeId(data.Id)
if err != nil {
return nil, err
}

return data, nil
}

func (s *approvalTypeService) Insert(approvalType *model.ApprovalType) (int, error) {
id, err := s.Repository.ApprovalType.Insert(approvalType)
if err != nil {
return 0, err
}
return id, nil
}
9 changes: 0 additions & 9 deletions src/goapp/service/approver/approver-service-interface.go

This file was deleted.

18 changes: 0 additions & 18 deletions src/goapp/service/approver/approver-service.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package repositoryApprover

import (
"main/model"
)

type RepositoryApproverService interface {
Create(approver *model.RepositoryApprover) error
Get(approvalTypeId int) ([]model.RepositoryApprover, error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package repositoryApprover

import (
"main/model"
"main/repository"
)

type repositoryApproverService struct {
Repository *repository.Repository
}

func NewRepositoryApproverService(repository *repository.Repository) RepositoryApproverService {
return &repositoryApproverService{repository}
}

func (s *repositoryApproverService) Create(approver *model.RepositoryApprover) error {
return s.Repository.RepositoryApprover.Insert(approver)
}

func (s *repositoryApproverService) Get(approvalTypeId int) ([]model.RepositoryApprover, error) {
return s.Repository.RepositoryApprover.SelectByApprovalTypeId(approvalTypeId)
}
Loading

0 comments on commit 726037a

Please sign in to comment.