initial commit
This commit is contained in:
121
internal/application/profilerole/service.go
Normal file
121
internal/application/profilerole/service.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package profilerole
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
"go.uber.org/fx"
|
||||
|
||||
domainProfile "base/internal/domain/profile"
|
||||
"base/internal/dto"
|
||||
)
|
||||
|
||||
var ErrNotFound = domainProfile.ErrRoleNotFound
|
||||
|
||||
type Service interface {
|
||||
List(ctx context.Context) ([]dto.ProfileRole, error)
|
||||
ListWithLimit(ctx context.Context, limit, offset int) ([]dto.ProfileRole, error)
|
||||
Create(ctx context.Context, req dto.CreateProfileRoleRequest) (*dto.ProfileRole, error)
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*dto.ProfileRole, error)
|
||||
Update(ctx context.Context, req dto.UpdateProfileRoleRequest) (*dto.ProfileRole, error)
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
}
|
||||
|
||||
type service struct {
|
||||
logger zerolog.Logger
|
||||
repo domainProfile.RoleRepository
|
||||
}
|
||||
|
||||
type Param struct {
|
||||
Logger zerolog.Logger
|
||||
Repo domainProfile.RoleRepository
|
||||
|
||||
fx.In
|
||||
}
|
||||
|
||||
func New(param Param) Service {
|
||||
return &service{
|
||||
logger: param.Logger,
|
||||
repo: param.Repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) List(ctx context.Context) ([]dto.ProfileRole, error) {
|
||||
roles, err := s.repo.FindAll(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toDTOs(roles), nil
|
||||
}
|
||||
|
||||
func (s *service) ListWithLimit(ctx context.Context, limit, offset int) ([]dto.ProfileRole, error) {
|
||||
roles, err := s.repo.List(ctx, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toDTOs(roles), nil
|
||||
}
|
||||
|
||||
func (s *service) Create(ctx context.Context, req dto.CreateProfileRoleRequest) (*dto.ProfileRole, error) {
|
||||
role := &domainProfile.Role{
|
||||
ID: uuid.New(),
|
||||
Title: req.Title,
|
||||
}
|
||||
if err := s.repo.Create(ctx, role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toDTO(role), nil
|
||||
}
|
||||
|
||||
func (s *service) GetByID(ctx context.Context, id uuid.UUID) (*dto.ProfileRole, error) {
|
||||
role, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return toDTO(role), nil
|
||||
}
|
||||
|
||||
func (s *service) Update(ctx context.Context, req dto.UpdateProfileRoleRequest) (*dto.ProfileRole, error) {
|
||||
id, err := uuid.Parse(req.ID)
|
||||
if err != nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
role, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
role.Title = req.Title
|
||||
|
||||
if err := s.repo.Update(ctx, role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toDTO(role), nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
role, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return ErrNotFound
|
||||
}
|
||||
return s.repo.Delete(ctx, role.ID)
|
||||
}
|
||||
|
||||
func toDTO(r *domainProfile.Role) *dto.ProfileRole {
|
||||
return &dto.ProfileRole{
|
||||
Id: r.ID.String(),
|
||||
Title: r.Title,
|
||||
}
|
||||
}
|
||||
|
||||
func toDTOs(roles []*domainProfile.Role) []dto.ProfileRole {
|
||||
out := make([]dto.ProfileRole, len(roles))
|
||||
for i, r := range roles {
|
||||
out[i] = *toDTO(r)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user