148 lines
3.5 KiB
Go
148 lines
3.5 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"
|
|
"time"
|
|
)
|
|
|
|
type PostService struct {
|
|
repo repository.PostRepository
|
|
}
|
|
|
|
func CreatePostService(repo repository.PostRepository) PostService {
|
|
return PostService{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *PostService) Create(cmd commands.CreatePostCommand) (*common.PostResult, error) {
|
|
entity, err := entities.CreatePost(cmd.UserId, cmd.Title, cmd.Description, cmd.Content)
|
|
if err != nil {
|
|
return nil, errors.NewValidationError("Invalid input data " + err.Error())
|
|
}
|
|
|
|
post, err := s.repo.Create(&entity)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreatePostResultFromEntity(post)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *PostService) FindById(query queries.PostFindByIdQuery) (*queries.PostFindByIdResult, error) {
|
|
post, err := s.repo.FindById(query.Id)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if post == nil {
|
|
return nil, errors.NewNotFoundError("Post")
|
|
}
|
|
|
|
if err := post.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreatePostFindByIdResultFromEntity(*post)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *PostService) FindAllByUserName(query queries.PostFindAllByUserNameQuery) (*queries.PostFindAllByUserNameResult, error) {
|
|
posts, err := s.repo.FindAllByUserName(query.UserName)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
for _, p := range posts {
|
|
if err := p.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
}
|
|
|
|
result := mapper.CreatePostFindAllByUserNameResult(posts)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *PostService) GetAll() (*queries.PostGetAllResult, error) {
|
|
posts, err := s.repo.GetAll()
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
for _, p := range posts {
|
|
if err := p.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
}
|
|
|
|
result := mapper.CreatePostGetAllResult(posts)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *PostService) GetAllOffset(offset int) (*queries.PostGetAllResult, error) {
|
|
if offset < 0 {
|
|
return nil, errors.NewValidationError("offset is less than 0")
|
|
}
|
|
|
|
posts, err := s.repo.GetAllOffset(offset)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreatePostGetAllResult(posts)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *PostService) Update(cmd commands.UpdatePostCommand) (*common.PostResult, error) {
|
|
post, err := s.repo.FindById(cmd.Id)
|
|
if err != nil {
|
|
return nil, errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if err := post.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
post.Title = cmd.Title
|
|
post.Description = cmd.Description
|
|
post.Content = cmd.Content
|
|
post.UpdatedAt = time.Now()
|
|
|
|
if err := post.Validate(); err != nil {
|
|
return nil, errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
result := mapper.CreatePostResultFromEntity(post)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *PostService) Delete(cmd commands.DeletePostCommand) error {
|
|
post, err := s.repo.FindById(cmd.Id)
|
|
if err != nil {
|
|
return errors.NewDBError(err.Error())
|
|
}
|
|
|
|
if err := post.Validate(); err != nil {
|
|
return errors.NewValidationError(err.Error())
|
|
}
|
|
|
|
err = s.repo.Delete(cmd.Id)
|
|
if err != nil {
|
|
return errors.NewDBError(err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|