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"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
|
|
@ -71,8 +71,8 @@ type (
|
|||
Mode string `json:"mode"`
|
||||
Status string `json:"status"`
|
||||
Parent *MenuParentResponse `json:"parent"`
|
||||
Permissions []dto.PermissionResponse `json:"permissions,omitempty"`
|
||||
Clients []dto.ClientResponse `json:"clients,omitempty"`
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
responses = append(responses, dto.ToMenuResponse(entity))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue