72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package services
|
|
|
|
import (
|
|
"58team_blog/internal/application/commands"
|
|
"58team_blog/internal/application/common"
|
|
"58team_blog/internal/application/mapper"
|
|
"58team_blog/internal/application/queries"
|
|
"58team_blog/internal/domain/entities"
|
|
"58team_blog/internal/domain/repository"
|
|
"fmt"
|
|
)
|
|
|
|
type ImagesService struct {
|
|
repo repository.ImagesRepository
|
|
}
|
|
|
|
func NewImagesService(repo repository.ImagesRepository) ImagesService {
|
|
return ImagesService{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *ImagesService) Create(cmd commands.CreateImageCommand) (*common.ImageResult, error) {
|
|
entity, err := entities.CreateImage(cmd.Path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := s.repo.Create(&entity); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := mapper.CreateImageResultFromEntity(&entity)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *ImagesService) FindById(query queries.ImageFindByIdQuery) (*queries.ImageFindByIdResult, error) {
|
|
entity, err := s.repo.FindById(query.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := mapper.CreateImageFindByIdResultFromEntity(entity)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *ImagesService) Delete(cmd commands.DeleteImageCommand) error {
|
|
entity, err := s.repo.FindById(cmd.Id)
|
|
if err != nil {
|
|
return fmt.Errorf("Image delete error: %s", err)
|
|
}
|
|
|
|
if err := entity.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.repo.Delete(entity.Id); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|