159 lines
4.2 KiB
Go
159 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"github.com/rs/zerolog"
|
|
"netidhub-saas-be/app/module/menu_actions/mapper"
|
|
"netidhub-saas-be/app/module/menu_actions/repository"
|
|
"netidhub-saas-be/app/module/menu_actions/request"
|
|
"netidhub-saas-be/app/module/menu_actions/response"
|
|
"netidhub-saas-be/app/database/entity"
|
|
"netidhub-saas-be/utils/paginator"
|
|
)
|
|
|
|
// MenuActionsService
|
|
type menuActionsService struct {
|
|
Repo repository.MenuActionsRepository
|
|
Log zerolog.Logger
|
|
}
|
|
|
|
// MenuActionsService define interface of IMenuActionsService
|
|
type MenuActionsService interface {
|
|
All(req request.MenuActionsQueryRequest) (menuActions []*response.MenuActionsResponse, paging paginator.Pagination, err error)
|
|
GetByMenuId(menuId uint) (menuActions []*response.MenuActionsResponse, err error)
|
|
Show(id uint) (menuAction *response.MenuActionsResponse, err error)
|
|
Save(req request.MenuActionsCreateRequest) (err error)
|
|
SaveBatch(req request.MenuActionsBatchCreateRequest) (err error)
|
|
Update(id uint, req request.MenuActionsUpdateRequest) (err error)
|
|
Delete(id uint) error
|
|
}
|
|
|
|
// NewMenuActionsService init MenuActionsService
|
|
func NewMenuActionsService(repo repository.MenuActionsRepository, log zerolog.Logger) MenuActionsService {
|
|
return &menuActionsService{
|
|
Repo: repo,
|
|
Log: log,
|
|
}
|
|
}
|
|
|
|
// All implement interface of MenuActionsService
|
|
func (_i *menuActionsService) All(req request.MenuActionsQueryRequest) (menuActions []*response.MenuActionsResponse, paging paginator.Pagination, err error) {
|
|
results, paging, err := _i.Repo.GetAll(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for _, result := range results {
|
|
menuActions = append(menuActions, mapper.MenuActionsResponseMapper(result))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *menuActionsService) GetByMenuId(menuId uint) (menuActions []*response.MenuActionsResponse, err error) {
|
|
results, err := _i.Repo.GetByMenuId(menuId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, result := range results {
|
|
menuActions = append(menuActions, mapper.MenuActionsResponseMapper(result))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *menuActionsService) Show(id uint) (menuAction *response.MenuActionsResponse, err error) {
|
|
result, err := _i.Repo.FindOne(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mapper.MenuActionsResponseMapper(result), nil
|
|
}
|
|
|
|
func (_i *menuActionsService) Save(req request.MenuActionsCreateRequest) (err error) {
|
|
_i.Log.Info().Interface("data", req).Msg("Creating menu action")
|
|
|
|
isActive := true
|
|
if req.IsActive != nil {
|
|
isActive = *req.IsActive
|
|
}
|
|
|
|
menuAction := req.ToEntity()
|
|
menuAction.IsActive = &isActive
|
|
|
|
return _i.Repo.Create(menuAction)
|
|
}
|
|
|
|
func (_i *menuActionsService) SaveBatch(req request.MenuActionsBatchCreateRequest) (err error) {
|
|
_i.Log.Info().Interface("data", req).Msg("Creating menu actions batch")
|
|
|
|
isActive := true
|
|
var menuActions []*entity.MenuActions
|
|
|
|
// Standard action names mapping
|
|
actionNameMap := map[string]string{
|
|
"view": "View",
|
|
"create": "Create",
|
|
"edit": "Edit",
|
|
"update": "Update",
|
|
"delete": "Delete",
|
|
"approve": "Approve",
|
|
"reject": "Reject",
|
|
"export": "Export",
|
|
"import": "Import",
|
|
"print": "Print",
|
|
}
|
|
|
|
for idx, actionCode := range req.ActionCodes {
|
|
position := idx + 1
|
|
actionName := actionNameMap[actionCode]
|
|
if actionName == "" {
|
|
// Capitalize first letter if not in map
|
|
if len(actionCode) > 0 {
|
|
actionName = string(actionCode[0]-32) + actionCode[1:]
|
|
} else {
|
|
actionName = actionCode
|
|
}
|
|
}
|
|
|
|
menuAction := &entity.MenuActions{
|
|
MenuId: req.MenuId,
|
|
ActionCode: actionCode,
|
|
ActionName: actionName,
|
|
Position: &position,
|
|
ClientId: req.ClientId,
|
|
IsActive: &isActive,
|
|
}
|
|
menuActions = append(menuActions, menuAction)
|
|
}
|
|
|
|
return _i.Repo.CreateBatch(menuActions)
|
|
}
|
|
|
|
func (_i *menuActionsService) Update(id uint, req request.MenuActionsUpdateRequest) (err error) {
|
|
_i.Log.Info().Interface("data", req).Msg("Updating menu action")
|
|
|
|
isActive := true
|
|
if req.IsActive != nil {
|
|
isActive = *req.IsActive
|
|
}
|
|
|
|
menuAction := req.ToEntity()
|
|
menuAction.IsActive = &isActive
|
|
|
|
return _i.Repo.Update(id, menuAction)
|
|
}
|
|
|
|
func (_i *menuActionsService) Delete(id uint) error {
|
|
result, err := _i.Repo.FindOne(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
isActive := false
|
|
result.IsActive = &isActive
|
|
return _i.Repo.Update(id, result)
|
|
}
|
|
|