Added errors for services
This commit is contained in:
parent
ab4b53fd40
commit
c3c3d65d32
12 changed files with 217 additions and 86 deletions
|
|
@ -3,13 +3,11 @@ package services
|
|||
import (
|
||||
"58team_blog/internal/application/commands"
|
||||
"58team_blog/internal/application/common"
|
||||
ie "58team_blog/internal/application/errors"
|
||||
"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"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -26,12 +24,12 @@ func CreatePostService(repo repository.PostRepository) PostService {
|
|||
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, err
|
||||
return nil, errors.NewValidationError("Invalid input data " + err.Error())
|
||||
}
|
||||
|
||||
post, err := s.repo.Create(&entity)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Db error: %s", err)
|
||||
return nil, errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
result := mapper.CreatePostResultFromEntity(post)
|
||||
|
|
@ -42,15 +40,15 @@ func (s *PostService) Create(cmd commands.CreatePostCommand) (*common.PostResult
|
|||
func (s *PostService) FindById(query queries.PostFindByIdQuery) (*queries.PostFindByIdResult, error) {
|
||||
post, err := s.repo.FindById(query.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
if post == nil {
|
||||
return nil, ie.NewNotFoundError("Post")
|
||||
return nil, errors.NewNotFoundError("Post")
|
||||
}
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewValidationError(err.Error())
|
||||
}
|
||||
|
||||
result := mapper.CreatePostFindByIdResultFromEntity(*post)
|
||||
|
|
@ -61,12 +59,12 @@ func (s *PostService) FindById(query queries.PostFindByIdQuery) (*queries.PostFi
|
|||
func (s *PostService) FindAllByUserName(query queries.PostFindAllByUserNameQuery) (*queries.PostFindAllByUserNameResult, error) {
|
||||
posts, err := s.repo.FindAllByUserName(query.UserName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
for _, p := range posts {
|
||||
if err := p.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewValidationError(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -78,12 +76,12 @@ func (s *PostService) FindAllByUserName(query queries.PostFindAllByUserNameQuery
|
|||
func (s *PostService) GetAll() (*queries.PostGetAllResult, error) {
|
||||
posts, err := s.repo.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
for _, p := range posts {
|
||||
if err := p.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewValidationError(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -94,13 +92,12 @@ func (s *PostService) GetAll() (*queries.PostGetAllResult, error) {
|
|||
|
||||
func (s *PostService) GetAllOffset(offset int) (*queries.PostGetAllResult, error) {
|
||||
if offset < 0 {
|
||||
return nil, errors.New("offset is less than 0")
|
||||
return nil, errors.NewValidationError("offset is less than 0")
|
||||
}
|
||||
|
||||
posts, err := s.repo.GetAllOffset(offset)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
result := mapper.CreatePostGetAllResult(posts)
|
||||
|
|
@ -111,11 +108,11 @@ func (s *PostService) GetAllOffset(offset int) (*queries.PostGetAllResult, error
|
|||
func (s *PostService) Update(cmd commands.UpdatePostCommand) (*common.PostResult, error) {
|
||||
post, err := s.repo.FindById(cmd.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewValidationError(err.Error())
|
||||
}
|
||||
|
||||
post.Title = cmd.Title
|
||||
|
|
@ -124,7 +121,7 @@ func (s *PostService) Update(cmd commands.UpdatePostCommand) (*common.PostResult
|
|||
post.UpdatedAt = time.Now()
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.NewValidationError(err.Error())
|
||||
}
|
||||
|
||||
result := mapper.CreatePostResultFromEntity(post)
|
||||
|
|
@ -135,16 +132,16 @@ func (s *PostService) Update(cmd commands.UpdatePostCommand) (*common.PostResult
|
|||
func (s *PostService) Delete(cmd commands.DeletePostCommand) error {
|
||||
post, err := s.repo.FindById(cmd.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return err
|
||||
return errors.NewValidationError(err.Error())
|
||||
}
|
||||
|
||||
err = s.repo.Delete(cmd.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.NewDBError(err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue