156 lines
3.8 KiB
Go
156 lines
3.8 KiB
Go
package services
|
|
|
|
import (
|
|
"58team_blog/internal/application/commands"
|
|
"58team_blog/internal/application/common"
|
|
"58team_blog/internal/application/errors"
|
|
"58team_blog/internal/application/mapper"
|
|
"58team_blog/internal/application/queries"
|
|
"58team_blog/internal/domain/entities"
|
|
"58team_blog/internal/domain/repository"
|
|
"fmt"
|
|
)
|
|
|
|
type UserService struct {
|
|
repo repository.UsersRepository
|
|
}
|
|
|
|
func CreateUserService(repo repository.UsersRepository) UserService {
|
|
return UserService{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *UserService) Create(cmd commands.CreateUserCommand) (*common.UserResult, error) {
|
|
// Check user with given name exists
|
|
{
|
|
user, err := s.repo.FindByName(cmd.Username)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if user != nil {
|
|
fmt.Println(user)
|
|
return nil, errors.NewAlreadyExistsError("user: " + user.UserName)
|
|
}
|
|
}
|
|
|
|
// Create new user
|
|
user, err := entities.CreateUser(cmd.Username, cmd.Password,
|
|
cmd.Name, cmd.Role, cmd.Speciality, cmd.Description,
|
|
cmd.Skills, cmd.Avatar, cmd.JoinDate, cmd.Projects, cmd.Motto)
|
|
if err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
entity, err := s.repo.Create(&user)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreateUserResultFromEntity(entity)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) FindById(query queries.UserFindByIdQuery) (*queries.UserFindByIdResult, error) {
|
|
entity, err := s.repo.FindById(query.Id)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreateUserFindByIdResultFromEntity(entity)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) FindByName(query queries.UserFindByNameQuery) (*queries.UserFindByNameResult, error) {
|
|
entity, err := s.repo.FindByName(query.Name)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if entity == nil {
|
|
return nil, errors.NewNotFoundError("User not found.")
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreateUserFindByNameResultFromEntity(entity)
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) GetAll() (*queries.UserGetAllResult, error) {
|
|
entityList, err := s.repo.GetAll()
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
for _, e := range entityList {
|
|
if err := e.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
}
|
|
|
|
result := mapper.CreateUserGetAllResultFromEntity(entityList)
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) Update(cmd commands.UpdateUserCommand) (*common.UserResult, error) {
|
|
entity, err := s.repo.FindById(cmd.Id)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
if cmd.Username != entity.UserName {
|
|
return nil, errors.NewValidationError("You cannot change user name")
|
|
}
|
|
|
|
entity.Password = cmd.Password
|
|
entity.Name = cmd.Name
|
|
entity.Role = cmd.Role
|
|
entity.Speciality = cmd.Speciality
|
|
entity.Description = cmd.Description
|
|
entity.Skills = cmd.Skills
|
|
entity.Avatar = cmd.Avatar
|
|
entity.Projects = cmd.Projects
|
|
entity.Motto = cmd.Motto
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
if err := s.repo.Update(entity); err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreateUserResultFromEntity(entity)
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) Delete(cmd commands.DeleteUserCommand) error {
|
|
entity, err := s.repo.FindById(cmd.Id)
|
|
if err != nil {
|
|
return errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
if err := s.repo.Delete(entity.Id); err != nil {
|
|
return errors.NewDBError(err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|