wms-be/modules/role/service/role_service.go

132 lines
4.4 KiB
Go

package service
import (
"context"
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
"github.com/Caknoooo/go-gin-clean-starter/modules/role/dto"
"github.com/Caknoooo/go-gin-clean-starter/modules/role/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/role/repository"
"github.com/google/uuid"
"gorm.io/gorm"
)
type RoleService interface {
CreateRole(ctx context.Context, role dto.RoleCreateRequest) (dto.RoleResponse, error)
GetRoles(ctx context.Context, filter query.RoleFilter) ([]dto.RoleResponse, error)
GetRoleByID(ctx context.Context, id string) (dto.RoleResponse, error)
UpdateRole(ctx context.Context, id string, role dto.RoleUpdateRequest) (dto.RoleResponse, error)
DeleteRole(ctx context.Context, id string) error
AssignPermissionsToRole(ctx context.Context, roleId string, permissions []string) error
RemovePermissionsFromRole(ctx context.Context, roleId string, permissions []string) error
AssignRoleToUser(ctx context.Context, userId string, roleId string) error
RemoveRoleFromUser(ctx context.Context, userId string, roleId string) error
GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error)
}
type roleService struct {
roleRepo repository.RoleRepository
refreshTokenRepository authRepo.RefreshTokenRepository
jwtService service.JWTService
db *gorm.DB
}
// AssignPermissionsToRole implements RoleService.
func (r *roleService) AssignPermissionsToRole(ctx context.Context, roleId string, permissions []string) error {
panic("unimplemented")
}
// AssignRoleToUser implements RoleService.
func (r *roleService) AssignRoleToUser(ctx context.Context, userId string, roleId string) error {
panic("unimplemented")
}
// CreateRole implements RoleService.
func (r *roleService) CreateRole(ctx context.Context, req dto.RoleCreateRequest) (dto.RoleResponse, error) {
_, exists, err := r.roleRepo.CheckRoleName(ctx, r.db, req.Name)
if err != nil && err != gorm.ErrRecordNotFound {
return dto.RoleResponse{}, err
}
if exists {
return dto.RoleResponse{}, dto.ErrRoleAlreadyExists
}
clientUUID, err := uuid.Parse(req.ClientID)
if err != nil {
return dto.RoleResponse{}, err
}
role := entities.M_Role{
Name: req.Name,
Description: req.Description,
IconUrl: req.IconUrl,
Type: req.Type,
HomeUrl: req.HomeUrl,
ClientID: clientUUID,
}
createdRole, err := r.roleRepo.CreateRole(ctx, r.db, role)
if err != nil {
return dto.RoleResponse{}, err
}
return dto.RoleResponse{
ID: createdRole.ID.String(),
Name: createdRole.Name,
Description: createdRole.Description,
IconUrl: createdRole.IconUrl,
Type: createdRole.Type,
HomeUrl: createdRole.HomeUrl,
ClientID: createdRole.ClientID.String(),
}, nil
}
// DeleteRole implements RoleService.
func (r *roleService) DeleteRole(ctx context.Context, id string) error {
panic("unimplemented")
}
// GetRoleByID implements RoleService.
func (r *roleService) GetRoleByID(ctx context.Context, id string) (dto.RoleResponse, error) {
panic("unimplemented")
}
// GetRoles implements RoleService.
func (r *roleService) GetRoles(ctx context.Context, filter query.RoleFilter) ([]dto.RoleResponse, error) {
panic("unimplemented")
}
// GetRolesByUserID implements RoleService.
func (r *roleService) GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error) {
panic("unimplemented")
}
// RemovePermissionsFromRole implements RoleService.
func (r *roleService) RemovePermissionsFromRole(ctx context.Context, roleId string, permissions []string) error {
panic("unimplemented")
}
// RemoveRoleFromUser implements RoleService.
func (r *roleService) RemoveRoleFromUser(ctx context.Context, userId string, roleId string) error {
panic("unimplemented")
}
// UpdateRole implements RoleService.
func (r *roleService) UpdateRole(ctx context.Context, id string, role dto.RoleUpdateRequest) (dto.RoleResponse, error) {
panic("unimplemented")
}
func NewRoleService(
roleRepo repository.RoleRepository,
refreshTokenRepo authRepo.RefreshTokenRepository,
jwtService service.JWTService,
db *gorm.DB,
) RoleService {
return &roleService{
roleRepo: roleRepo,
refreshTokenRepository: refreshTokenRepo,
jwtService: jwtService,
db: db,
}
}