feat: refactor menu service to use ToMenuResponse for response mapping and integrate logging
This commit is contained in:
parent
6296fc623c
commit
cd60dc109f
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
"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"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -63,16 +63,16 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuResponse struct {
|
MenuResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
IconUrl string `json:"icon_url"`
|
IconUrl string `json:"icon_url"`
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
Sequence int `json:"sequence"`
|
Sequence int `json:"sequence"`
|
||||||
Mode string `json:"mode"`
|
Mode string `json:"mode"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Parent *MenuParentResponse `json:"parent"`
|
Parent *MenuParentResponse `json:"parent"`
|
||||||
Permissions []dto.PermissionResponse `json:"permissions,omitempty"`
|
Permissions []pkgdto.PermissionResponse `json:"permissions,omitempty"`
|
||||||
Clients []dto.ClientResponse `json:"clients,omitempty"`
|
Clients []pkgdto.ClientResponse `json:"clients,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuParentResponse struct {
|
MenuParentResponse struct {
|
||||||
|
|
@ -82,7 +82,7 @@ type (
|
||||||
|
|
||||||
GetAllMenuRepositoryResponse struct {
|
GetAllMenuRepositoryResponse struct {
|
||||||
Menus []entities.M_Menu `json:"menus"`
|
Menus []entities.M_Menu `json:"menus"`
|
||||||
dto.PaginationResponse
|
pkgdto.PaginationResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuUpdateRequest struct {
|
MenuUpdateRequest struct {
|
||||||
|
|
@ -96,3 +96,33 @@ type (
|
||||||
ParentID *uuid.UUID `json:"parent_id" form:"parent_id" binding:"omitempty"`
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
"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/dto"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/menu/repository"
|
"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/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -26,6 +26,7 @@ type menuService struct {
|
||||||
menuRepository repository.MenuRepository
|
menuRepository repository.MenuRepository
|
||||||
jwtService service.JWTService
|
jwtService service.JWTService
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
log *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll implements MenuService.
|
// GetAll implements MenuService.
|
||||||
|
|
@ -37,38 +38,8 @@ func (m *menuService) GetAll(ctx context.Context) ([]dto.MenuResponse, error) {
|
||||||
|
|
||||||
responses := make([]dto.MenuResponse, 0, len(entities))
|
responses := make([]dto.MenuResponse, 0, len(entities))
|
||||||
for _, entity := range entities {
|
for _, entity := range entities {
|
||||||
res := dto.MenuResponse{
|
responses = append(responses, dto.ToMenuResponse(entity))
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return responses, nil
|
return responses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,25 +82,7 @@ func (m *menuService) Create(ctx context.Context, menu dto.MenuResponse) (dto.Me
|
||||||
return dto.MenuResponse{}, err
|
return dto.MenuResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// map back to DTO
|
res := dto.ToMenuResponse(created)
|
||||||
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()}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
@ -147,37 +100,7 @@ func (m *menuService) GetById(ctx context.Context, menuId string) (dto.MenuRespo
|
||||||
return dto.MenuResponse{}, err
|
return dto.MenuResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := dto.MenuResponse{
|
return dto.ToMenuResponse(entity), nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByName implements MenuService.
|
// GetByName implements MenuService.
|
||||||
|
|
@ -187,24 +110,7 @@ func (m *menuService) GetByName(ctx context.Context, name string) (dto.MenuRespo
|
||||||
return dto.MenuResponse{}, err
|
return dto.MenuResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := dto.MenuResponse{
|
res := dto.ToMenuResponse(entity)
|
||||||
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()}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
@ -250,24 +156,7 @@ func (m *menuService) Update(ctx context.Context, menuId string, menu dto.MenuUp
|
||||||
return dto.MenuResponse{}, err
|
return dto.MenuResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := dto.MenuResponse{
|
res := dto.ToMenuResponse(updated)
|
||||||
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()}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
@ -276,10 +165,12 @@ func NewMenuService(
|
||||||
menuRepo repository.MenuRepository,
|
menuRepo repository.MenuRepository,
|
||||||
jwtService service.JWTService,
|
jwtService service.JWTService,
|
||||||
db *gorm.DB,
|
db *gorm.DB,
|
||||||
|
log *logrus.Logger,
|
||||||
) MenuService {
|
) MenuService {
|
||||||
return &menuService{
|
return &menuService{
|
||||||
menuRepository: menuRepo,
|
menuRepository: menuRepo,
|
||||||
jwtService: jwtService,
|
jwtService: jwtService,
|
||||||
db: db,
|
db: db,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -177,7 +177,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db)
|
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db)
|
||||||
productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ, log)
|
productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ, log)
|
||||||
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db, 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)
|
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
||||||
clientServ := clientService.NewClientService(clientRepository, db)
|
clientServ := clientService.NewClientService(clientRepository, db)
|
||||||
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue