Initial commit
This commit is contained in:
commit
c0cb826917
63 changed files with 2069 additions and 0 deletions
72
internal/application/services/images_service.go
Normal file
72
internal/application/services/images_service.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
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
|
||||
}
|
||||
130
internal/application/services/post_service.go
Normal file
130
internal/application/services/post_service.go
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
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"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PostService struct {
|
||||
repo repository.PostRepository
|
||||
postsService PostsService
|
||||
}
|
||||
|
||||
func CreatePostService(repo repository.PostRepository, postsService PostsService) PostService {
|
||||
return PostService{
|
||||
repo: repo,
|
||||
postsService: postsService,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
post, err := s.repo.Create(&entity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
|
||||
for _, p := range posts {
|
||||
if err := p.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result := mapper.CreatePostFindAllByUserNameResult(posts)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PostService) GetAll() (*queries.PostGetAllResult, error) {
|
||||
posts, err := s.repo.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, p := range posts {
|
||||
if err := p.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post.Title = cmd.Title
|
||||
post.Description = cmd.Description
|
||||
post.Content = cmd.Content
|
||||
post.UpdatedAt = time.Now()
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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 err
|
||||
}
|
||||
|
||||
if err := post.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.repo.Delete(cmd.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
141
internal/application/services/posts_service.go
Normal file
141
internal/application/services/posts_service.go
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
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"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PostsService struct {
|
||||
repo repository.PostsRepository
|
||||
}
|
||||
|
||||
func CreatePostsService(repo repository.PostsRepository) PostsService {
|
||||
return PostsService{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PostsService) Create(cmd commands.CreatePostsCommand) (*common.PostsResult, error) {
|
||||
if user, err := s.repo.FindByPostId(cmd.PostId); user != nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, errors.New("Posts already exists")
|
||||
}
|
||||
|
||||
entity, err := entities.CreatePosts(cmd.UserId, cmd.PostId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := mapper.CreatePostsResultFromEntity(&entity)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PostsService) FindByUserId(query queries.PostsFindByUserIdQuery) (*queries.PostsFindByUserIdResult, error) {
|
||||
entity, err := s.repo.FindByUserId(query.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if entity == nil {
|
||||
return nil, errors.New("Posts not found")
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := mapper.CreatePostsFindByUserIdResultFromEntity(entity)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PostsService) FindByPostId(query queries.PostsFindByPostIdQuery) (*queries.PostsFindByPostIdResult, error) {
|
||||
entity, err := s.repo.FindByPostId(query.PostId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if entity == nil {
|
||||
return nil, errors.New("Posts not found")
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := mapper.CreatePostsFindByPostIdResultFromEntity(entity)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PostsService) FindAllByUserId(query queries.PostsFindByUserIdQuery) (*queries.PostsFindAllByUserIdResult, error) {
|
||||
entities, err := s.repo.FindAllByUserId(query.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if entities == nil {
|
||||
return nil, fmt.Errorf("No posts owned by user: %s", query.UserId.String())
|
||||
}
|
||||
|
||||
for _, e := range entities {
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result := mapper.CreatePostsFindAllByUserIdResultFromEntity(entities)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PostsService) GetAll() (*queries.PostsGetAllResult, error) {
|
||||
entities, err := s.repo.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, e := range entities {
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result := mapper.CreatePostsGetAllResultFromEntity(entities)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PostsService) Delete(cmd commands.DeletePostsCommand) error {
|
||||
entity, err := s.repo.FindById(cmd.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if entity == nil {
|
||||
return fmt.Errorf("Posts row not found: %s", cmd.Id)
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.repo.Delete(cmd.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
145
internal/application/services/user_service.go
Normal file
145
internal/application/services/user_service.go
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
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"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
repo repository.UsersRepository
|
||||
}
|
||||
|
||||
func NewUserService(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, err
|
||||
}
|
||||
|
||||
if user != nil {
|
||||
return nil, fmt.Errorf("User: %s already exists", user.UserName)
|
||||
}
|
||||
}
|
||||
|
||||
// Create new user
|
||||
user, err := entities.CreateUser(cmd.Username, cmd.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := user.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entity, err := s.repo.Create(&user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := mapper.CreateUserFindByNameResultFromEntity(entity)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *UserService) GetAll() (*queries.UserGetAllResult, error) {
|
||||
entityList, err := s.repo.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, e := range entityList {
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cmd.Username != entity.UserName {
|
||||
return nil, errors.New("You cannot change user name")
|
||||
}
|
||||
|
||||
entity.Password = cmd.Password
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.repo.Update(entity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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 err
|
||||
}
|
||||
|
||||
if err := entity.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.repo.Delete(entity.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue