From cd60dc109fa53e66dc06b7a2120a624b53e448ec Mon Sep 17 00:00:00 2001 From: Habib Fatkhul Rohman Date: Wed, 3 Dec 2025 14:39:10 +0700 Subject: [PATCH] feat: refactor menu service to use ToMenuResponse for response mapping and integrate logging --- modules/menu/dto/menu_dto.go | 54 +++++++++--- modules/menu/service/menu_service.go | 127 ++------------------------- providers/core.go | 2 +- 3 files changed, 52 insertions(+), 131 deletions(-) diff --git a/modules/menu/dto/menu_dto.go b/modules/menu/dto/menu_dto.go index 5ebdede..6b76bc4 100644 --- a/modules/menu/dto/menu_dto.go +++ b/modules/menu/dto/menu_dto.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/Caknoooo/go-gin-clean-starter/database/entities" - "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" + pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" "github.com/google/uuid" ) @@ -63,16 +63,16 @@ type ( } MenuResponse struct { - ID string `json:"id"` - Name string `json:"name"` - IconUrl string `json:"icon_url"` - Url string `json:"url"` - Sequence int `json:"sequence"` - Mode string `json:"mode"` - Status string `json:"status"` - Parent *MenuParentResponse `json:"parent"` - Permissions []dto.PermissionResponse `json:"permissions,omitempty"` - Clients []dto.ClientResponse `json:"clients,omitempty"` + ID string `json:"id"` + Name string `json:"name"` + IconUrl string `json:"icon_url"` + Url string `json:"url"` + Sequence int `json:"sequence"` + Mode string `json:"mode"` + Status string `json:"status"` + Parent *MenuParentResponse `json:"parent"` + Permissions []pkgdto.PermissionResponse `json:"permissions,omitempty"` + Clients []pkgdto.ClientResponse `json:"clients,omitempty"` } MenuParentResponse struct { @@ -82,7 +82,7 @@ type ( GetAllMenuRepositoryResponse struct { Menus []entities.M_Menu `json:"menus"` - dto.PaginationResponse + pkgdto.PaginationResponse } MenuUpdateRequest struct { @@ -96,3 +96,33 @@ type ( ParentID *uuid.UUID `json:"parent_id" form:"parent_id" binding:"omitempty"` } ) + +func ToMenuResponse(entity entities.M_Menu) MenuResponse { + res := MenuResponse{ + ID: entity.ID.String(), + Name: entity.Name, + IconUrl: entity.IconUrl, + Url: entity.Url, + Sequence: entity.Sequence, + Mode: entity.Mode, + Status: entity.Status, + } + if entity.Parent != nil { + res.Parent = &MenuParentResponse{ + ID: entity.Parent.ID.String(), + Name: entity.Parent.Name, + } + } else if entity.ParentID != nil { + res.Parent = &MenuParentResponse{ID: entity.ParentID.String()} + } + if len(entity.Permissions) > 0 { + res.Permissions = make([]pkgdto.PermissionResponse, 0, len(entity.Permissions)) + for _, perm := range entity.Permissions { + res.Permissions = append(res.Permissions, pkgdto.PermissionResponse{ + ID: perm.ID.String(), + Name: perm.Name, + }) + } + } + return res +} diff --git a/modules/menu/service/menu_service.go b/modules/menu/service/menu_service.go index 850b7bd..2c28feb 100644 --- a/modules/menu/service/menu_service.go +++ b/modules/menu/service/menu_service.go @@ -8,8 +8,8 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service" "github.com/Caknoooo/go-gin-clean-starter/modules/menu/dto" "github.com/Caknoooo/go-gin-clean-starter/modules/menu/repository" - staticDto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" "github.com/google/uuid" + "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -26,6 +26,7 @@ type menuService struct { menuRepository repository.MenuRepository jwtService service.JWTService db *gorm.DB + log *logrus.Logger } // GetAll implements MenuService. @@ -37,38 +38,8 @@ func (m *menuService) GetAll(ctx context.Context) ([]dto.MenuResponse, error) { responses := make([]dto.MenuResponse, 0, len(entities)) for _, entity := range entities { - res := dto.MenuResponse{ - ID: entity.ID.String(), - Name: entity.Name, - IconUrl: entity.IconUrl, - Url: entity.Url, - Sequence: entity.Sequence, - Mode: entity.Mode, - Status: entity.Status, - } - - if entity.Parent != nil { - res.Parent = &dto.MenuParentResponse{ - ID: entity.Parent.ID.String(), - Name: entity.Parent.Name, - } - } else if entity.ParentID != nil { - res.Parent = &dto.MenuParentResponse{ID: entity.ParentID.String()} - } - - if len(entity.Permissions) > 0 { - res.Permissions = make([]staticDto.PermissionResponse, 0, len(entity.Permissions)) - for _, perm := range entity.Permissions { - res.Permissions = append(res.Permissions, staticDto.PermissionResponse{ - ID: perm.ID.String(), - Name: perm.Name, - }) - } - } - - responses = append(responses, res) + responses = append(responses, dto.ToMenuResponse(entity)) } - return responses, nil } @@ -111,25 +82,7 @@ func (m *menuService) Create(ctx context.Context, menu dto.MenuResponse) (dto.Me return dto.MenuResponse{}, err } - // map back to DTO - res := dto.MenuResponse{ - ID: created.ID.String(), - Name: created.Name, - IconUrl: created.IconUrl, - Url: created.Url, - Sequence: created.Sequence, - Mode: created.Mode, - Status: created.Status, - } - - if created.Parent != nil { - res.Parent = &dto.MenuParentResponse{ - ID: created.Parent.ID.String(), - Name: created.Parent.Name, - } - } else if created.ParentID != nil { - res.Parent = &dto.MenuParentResponse{ID: created.ParentID.String()} - } + res := dto.ToMenuResponse(created) return res, nil } @@ -147,37 +100,7 @@ func (m *menuService) GetById(ctx context.Context, menuId string) (dto.MenuRespo return dto.MenuResponse{}, err } - res := dto.MenuResponse{ - ID: entity.ID.String(), - Name: entity.Name, - IconUrl: entity.IconUrl, - Url: entity.Url, - Sequence: entity.Sequence, - Mode: entity.Mode, - Status: entity.Status, - } - - if entity.Parent != nil { - res.Parent = &dto.MenuParentResponse{ - ID: entity.Parent.ID.String(), - Name: entity.Parent.Name, - } - } else if entity.ParentID != nil { - res.Parent = &dto.MenuParentResponse{ID: entity.ParentID.String()} - } - - if len(entity.Permissions) > 0 { - res.Permissions = make([]staticDto.PermissionResponse, 0, len(entity.Permissions)) - for _, perm := range entity.Permissions { - res.Permissions = append(res.Permissions, staticDto.PermissionResponse{ - ID: perm.ID.String(), - Name: perm.Name, - // Description: perm.Description, - // Code: perm.Code, - }) - } - } - return res, nil + return dto.ToMenuResponse(entity), nil } // GetByName implements MenuService. @@ -187,24 +110,7 @@ func (m *menuService) GetByName(ctx context.Context, name string) (dto.MenuRespo return dto.MenuResponse{}, err } - res := dto.MenuResponse{ - ID: entity.ID.String(), - Name: entity.Name, - IconUrl: entity.IconUrl, - Url: entity.Url, - Sequence: entity.Sequence, - Mode: entity.Mode, - Status: entity.Status, - } - - if entity.Parent != nil { - res.Parent = &dto.MenuParentResponse{ - ID: entity.Parent.ID.String(), - Name: entity.Parent.Name, - } - } else if entity.ParentID != nil { - res.Parent = &dto.MenuParentResponse{ID: entity.ParentID.String()} - } + res := dto.ToMenuResponse(entity) return res, nil } @@ -250,24 +156,7 @@ func (m *menuService) Update(ctx context.Context, menuId string, menu dto.MenuUp return dto.MenuResponse{}, err } - res := dto.MenuResponse{ - ID: updated.ID.String(), - Name: updated.Name, - IconUrl: updated.IconUrl, - Url: updated.Url, - Sequence: updated.Sequence, - Mode: updated.Mode, - Status: updated.Status, - } - - if updated.Parent != nil { - res.Parent = &dto.MenuParentResponse{ - ID: updated.Parent.ID.String(), - Name: updated.Parent.Name, - } - } else if updated.ParentID != nil { - res.Parent = &dto.MenuParentResponse{ID: updated.ParentID.String()} - } + res := dto.ToMenuResponse(updated) return res, nil } @@ -276,10 +165,12 @@ func NewMenuService( menuRepo repository.MenuRepository, jwtService service.JWTService, db *gorm.DB, + log *logrus.Logger, ) MenuService { return &menuService{ menuRepository: menuRepo, jwtService: jwtService, db: db, + log: log, } } diff --git a/providers/core.go b/providers/core.go index b2bdb7b..144856f 100644 --- a/providers/core.go +++ b/providers/core.go @@ -177,7 +177,7 @@ func RegisterDependencies(injector *do.Injector) { userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db) productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ, log) roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db, log) - menuSvc := menuService.NewMenuService(menuRepository, jwtService, db) + menuSvc := menuService.NewMenuService(menuRepository, jwtService, db, log) maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db) clientServ := clientService.NewClientService(clientRepository, db) permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)