wms-be/modules/menu/dto/menu_dto.go

129 lines
5.1 KiB
Go

package dto
import (
"errors"
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/google/uuid"
)
const (
// Failed
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
MESSAGE_FAILED_REGISTER_MENU = "failed create menu"
MESSAGE_FAILED_GET_LIST_MENU = "failed get list menu"
MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid"
MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found"
MESSAGE_FAILED_GET_MENU = "failed get menu"
MESSAGE_FAILED_LOGIN = "failed login"
MESSAGE_FAILED_UPDATE_MENU = "failed update menu"
MESSAGE_FAILED_DELETE_MENU = "failed delete menu"
MESSAGE_FAILED_PROSES_REQUEST = "failed proses request"
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
MESSAGE_FAILED_VERIFY_EMAIL = "failed verify email"
MESSAGE_FAILED_CREATE_MENU = "failed create menu"
// Success
MESSAGE_SUCCESS_REGISTER_MENU = "success create menu"
MESSAGE_SUCCESS_GET_LIST_MENU = "success get list menu"
MESSAGE_SUCCESS_GET_MENU = "success get menu"
MESSAGE_SUCCESS_LOGIN = "success login"
MESSAGE_SUCCESS_UPDATE_MENU = "success update menu"
MESSAGE_SUCCESS_DELETE_MENU = "success delete menu"
MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS = "success send verification email"
MESSAGE_SUCCESS_VERIFY_EMAIL = "success verify email"
MESSAGE_SUCCESS_CREATE_MENU = "success create menu"
)
var (
ErrCreateMenu = errors.New("failed to create menu")
ErrGetMenuById = errors.New("failed to get menu by id")
ErrGetMenuByEmail = errors.New("failed to get menu by email")
ErrEmailAlreadyExists = errors.New("email already exist")
ErrUpdateMenu = errors.New("failed to update menu")
ErrMenuNotFound = errors.New("menu not found")
ErrEmailNotFound = errors.New("email not found")
ErrDeleteMenu = errors.New("failed to delete menu")
ErrTokenInvalid = errors.New("token invalid")
ErrTokenExpired = errors.New("token expired")
ErrAccountAlreadyVerified = errors.New("account already verified")
)
type (
MenuCreateRequest struct {
Name string `json:"name" form:"name" binding:"required,min=2,max=100"`
IconUrl string `json:"icon_url" form:"icon_url" binding:"omitempty,min=2,max=100"`
Url string `json:"url" form:"url" binding:"required,min=1"`
TableName string `json:"table_name" form:"table_name" binding:"omitempty,max=10"`
Status string `json:"status" form:"status" binding:"omitempty"`
Mode string `json:"mode" form:"mode" binding:"required,min=1,max=20"`
Sequence int `json:"sequence" form:"sequence" binding:"required"`
ParentID *uuid.UUID `json:"parent_id" form:"parent_id" binding:"omitempty,uuid4"`
}
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 []pkgdto.PermissionResponse `json:"permissions,omitempty"`
Clients []pkgdto.ClientResponse `json:"clients,omitempty"`
}
MenuParentResponse struct {
ID string `json:"id"`
Name string `json:"name"`
}
GetAllMenuRepositoryResponse struct {
Menus []entities.M_Menu `json:"menus"`
pkgdto.PaginationResponse
}
MenuUpdateRequest struct {
Name string `json:"name" form:"name" binding:"omitempty,min=2,max=100"`
IconUrl string `json:"icon_url" form:"icon_url" binding:"omitempty,min=2,max=100"`
Url string `json:"url" form:"url" binding:"omitempty,min=1"`
TableName string `json:"table_name" form:"table_name" binding:"omitempty,max=10"`
Status string `json:"status" form:"status" binding:"omitempty"`
Mode string `json:"mode" form:"mode" binding:"omitempty,min=1,max=20"`
Sequence int `json:"sequence" form:"sequence" 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
}