Initial commit
This commit is contained in:
commit
c0cb826917
63 changed files with 2069 additions and 0 deletions
5
internal/application/commands/create_image_command.go
Normal file
5
internal/application/commands/create_image_command.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package commands
|
||||
|
||||
type CreateImageCommand struct {
|
||||
Path string
|
||||
}
|
||||
10
internal/application/commands/create_post_command.go
Normal file
10
internal/application/commands/create_post_command.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type CreatePostCommand struct {
|
||||
UserId uuid.UUID
|
||||
Title string
|
||||
Description string
|
||||
Content string
|
||||
}
|
||||
8
internal/application/commands/create_posts_command.go
Normal file
8
internal/application/commands/create_posts_command.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type CreatePostsCommand struct {
|
||||
PostId uuid.UUID
|
||||
UserId uuid.UUID
|
||||
}
|
||||
6
internal/application/commands/create_user_command.go
Normal file
6
internal/application/commands/create_user_command.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package commands
|
||||
|
||||
type CreateUserCommand struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
7
internal/application/commands/delete_image_command.go
Normal file
7
internal/application/commands/delete_image_command.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type DeleteImageCommand struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
7
internal/application/commands/delete_post_command.go
Normal file
7
internal/application/commands/delete_post_command.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type DeletePostCommand struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
7
internal/application/commands/delete_posts_command.go
Normal file
7
internal/application/commands/delete_posts_command.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type DeletePostsCommand struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
7
internal/application/commands/delete_user_command.go
Normal file
7
internal/application/commands/delete_user_command.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type DeleteUserCommand struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
10
internal/application/commands/update_post_command.go
Normal file
10
internal/application/commands/update_post_command.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type UpdatePostCommand struct {
|
||||
Id uuid.UUID
|
||||
Title string
|
||||
Description string
|
||||
Content string
|
||||
}
|
||||
9
internal/application/commands/update_user_command.go
Normal file
9
internal/application/commands/update_user_command.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type UpdateUserCommand struct {
|
||||
Id uuid.UUID
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
8
internal/application/common/image_result.go
Normal file
8
internal/application/common/image_result.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package common
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type ImageResult struct {
|
||||
Id uuid.UUID
|
||||
Path string
|
||||
}
|
||||
21
internal/application/common/post_result.go
Normal file
21
internal/application/common/post_result.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostResult struct {
|
||||
Id uuid.UUID
|
||||
UserId uuid.UUID
|
||||
Title string
|
||||
Description string
|
||||
Content string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type PostResultList struct {
|
||||
Result []*PostResult
|
||||
}
|
||||
13
internal/application/common/posts_result.go
Normal file
13
internal/application/common/posts_result.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package common
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type PostsResult struct {
|
||||
Id uuid.UUID
|
||||
UserId uuid.UUID
|
||||
PostId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsResultList struct {
|
||||
Result []*PostsResult
|
||||
}
|
||||
13
internal/application/common/user_result.go
Normal file
13
internal/application/common/user_result.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package common
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type UserResult struct {
|
||||
Id uuid.UUID
|
||||
UserName string
|
||||
Password string
|
||||
}
|
||||
|
||||
type UserResultList struct {
|
||||
Result []*UserResult
|
||||
}
|
||||
13
internal/application/interfaces/images_service.go
Normal file
13
internal/application/interfaces/images_service.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/commands"
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
)
|
||||
|
||||
type ImagesService interface {
|
||||
Create(commands.CreateImageCommand) (*common.ImageResult, error)
|
||||
FindById(queries.ImageFindByIdQuery) (*queries.ImageFindByIdResult, error)
|
||||
Delete(commands.DeleteImageCommand) error
|
||||
}
|
||||
16
internal/application/interfaces/post_service.go
Normal file
16
internal/application/interfaces/post_service.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/commands"
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
)
|
||||
|
||||
type PostService interface {
|
||||
Create(commands.CreatePostCommand) (*common.PostResult, error)
|
||||
FindById(queries.PostFindByIdQuery) (*queries.PostFindByIdResult, error)
|
||||
FindAllByUserName(queries.PostFindAllByUserNameQuery) (*queries.PostFindAllByUserNameResult, error)
|
||||
GetAll() (*queries.PostGetAllResult, error)
|
||||
Update(commands.UpdatePostCommand) (*common.PostResult, error)
|
||||
Delete(commands.DeletePostCommand) error
|
||||
}
|
||||
16
internal/application/interfaces/posts_service.go
Normal file
16
internal/application/interfaces/posts_service.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/commands"
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
)
|
||||
|
||||
type PostsService interface {
|
||||
Create(commands.CreatePostsCommand) (*common.PostsResult, error)
|
||||
FindByUserId(queries.PostsFindByUserIdQuery) (*queries.PostsFindByUserIdResult, error)
|
||||
FindByPostId(queries.PostsFindByPostIdQuery) (*queries.PostsFindByPostIdResult, error)
|
||||
FindAllByUserId(queries.PostsFindByUserIdQuery) (*queries.PostsFindAllByUserIdResult, error)
|
||||
GetAll() (queries.PostsGetAllResult, error)
|
||||
Delete(commands.DeletePostsCommand) error
|
||||
}
|
||||
16
internal/application/interfaces/user_service.go
Normal file
16
internal/application/interfaces/user_service.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/commands"
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
)
|
||||
|
||||
type UserService interface {
|
||||
Create(commands.CreateUserCommand) (*common.UserResult, error)
|
||||
FindById(queries.UserFindByIdQuery) (*queries.UserFindByIdResult, error)
|
||||
FindByName(queries.UserFindByNameQuery) (*queries.UserFindByNameResult, error)
|
||||
GetAll() (*queries.UserGetAllResult, error)
|
||||
Update(commands.UpdateUserCommand) (*common.UserResult, error)
|
||||
Delete(commands.DeleteUserCommand) error
|
||||
}
|
||||
20
internal/application/mapper/image_result.go
Normal file
20
internal/application/mapper/image_result.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
"58team_blog/internal/domain/entities"
|
||||
)
|
||||
|
||||
func CreateImageResultFromEntity(entity *entities.Images) *common.ImageResult {
|
||||
return &common.ImageResult{
|
||||
Id: entity.Id,
|
||||
Path: entity.Path,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateImageFindByIdResultFromEntity(entity *entities.Images) *queries.ImageFindByIdResult {
|
||||
return &queries.ImageFindByIdResult{
|
||||
Result: CreateImageResultFromEntity(entity),
|
||||
}
|
||||
}
|
||||
47
internal/application/mapper/post_result.go
Normal file
47
internal/application/mapper/post_result.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
"58team_blog/internal/domain/entities"
|
||||
)
|
||||
|
||||
func CreatePostResultFromEntity(entity *entities.Post) *common.PostResult {
|
||||
return &common.PostResult{
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
Title: entity.Title,
|
||||
Description: entity.Description,
|
||||
Content: entity.Content,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostResultListFromEntity(entity_list []*entities.Post) *common.PostResultList {
|
||||
var result common.PostResultList
|
||||
|
||||
for _, e := range entity_list {
|
||||
result.Result = append(result.Result, CreatePostResultFromEntity(e))
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
func CreatePostFindByIdResultFromEntity(entity entities.Post) *queries.PostFindByIdResult {
|
||||
return &queries.PostFindByIdResult{
|
||||
Result: CreatePostResultFromEntity(&entity),
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostFindAllByUserNameResult(entity []*entities.Post) *queries.PostFindAllByUserNameResult {
|
||||
return &queries.PostFindAllByUserNameResult{
|
||||
Result: CreatePostResultListFromEntity(entity),
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostGetAllResult(entity_list []*entities.Post) *queries.PostGetAllResult {
|
||||
return &queries.PostGetAllResult{
|
||||
Result: CreatePostResultListFromEntity(entity_list),
|
||||
}
|
||||
}
|
||||
48
internal/application/mapper/posts_result.go
Normal file
48
internal/application/mapper/posts_result.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
"58team_blog/internal/domain/entities"
|
||||
)
|
||||
|
||||
func CreatePostsResultFromEntity(entity *entities.Posts) *common.PostsResult {
|
||||
return &common.PostsResult{
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
PostId: entity.PostId,
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostsResultListFromEntityList(entity_list []*entities.Posts) *common.PostsResultList {
|
||||
var result common.PostsResultList
|
||||
for _, e := range entity_list {
|
||||
result.Result = append(result.Result, CreatePostsResultFromEntity(e))
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
func CreatePostsFindByUserIdResultFromEntity(entity *entities.Posts) *queries.PostsFindByUserIdResult {
|
||||
return &queries.PostsFindByUserIdResult{
|
||||
Result: CreatePostsResultFromEntity(entity),
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostsFindByPostIdResultFromEntity(entity *entities.Posts) *queries.PostsFindByPostIdResult {
|
||||
return &queries.PostsFindByPostIdResult{
|
||||
Result: CreatePostsResultFromEntity(entity),
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostsFindAllByUserIdResultFromEntity(entity_list []*entities.Posts) *queries.PostsFindAllByUserIdResult {
|
||||
return &queries.PostsFindAllByUserIdResult{
|
||||
Result: CreatePostsResultListFromEntityList(entity_list),
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePostsGetAllResultFromEntity(entity_list []*entities.Posts) *queries.PostsGetAllResult {
|
||||
return &queries.PostsGetAllResult{
|
||||
Result: CreatePostsResultListFromEntityList(entity_list),
|
||||
}
|
||||
}
|
||||
43
internal/application/mapper/user_result.go
Normal file
43
internal/application/mapper/user_result.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
"58team_blog/internal/application/queries"
|
||||
"58team_blog/internal/domain/entities"
|
||||
)
|
||||
|
||||
func CreateUserResultFromEntity(entity *entities.User) *common.UserResult {
|
||||
return &common.UserResult{
|
||||
Id: entity.Id,
|
||||
UserName: entity.UserName,
|
||||
Password: entity.Password,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateUserResultListFromEntity(entityList []*entities.User) *common.UserResultList {
|
||||
var result common.UserResultList
|
||||
|
||||
for _, e := range entityList {
|
||||
result.Result = append(result.Result, CreateUserResultFromEntity(e))
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
func CreateUserFindByIdResultFromEntity(entity *entities.User) *queries.UserFindByIdResult {
|
||||
return &queries.UserFindByIdResult{
|
||||
Result: CreateUserResultFromEntity(entity),
|
||||
}
|
||||
}
|
||||
|
||||
func CreateUserFindByNameResultFromEntity(entity *entities.User) *queries.UserFindByNameResult {
|
||||
return &queries.UserFindByNameResult{
|
||||
Result: CreateUserResultFromEntity(entity),
|
||||
}
|
||||
}
|
||||
|
||||
func CreateUserGetAllResultFromEntity(entity_list []*entities.User) *queries.UserGetAllResult {
|
||||
return &queries.UserGetAllResult{
|
||||
Result: CreateUserResultListFromEntity(entity_list),
|
||||
}
|
||||
}
|
||||
15
internal/application/queries/image_find_by_id.go
Normal file
15
internal/application/queries/image_find_by_id.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ImageFindByIdQuery struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
|
||||
type ImageFindByIdResult struct {
|
||||
Result *common.ImageResult
|
||||
}
|
||||
11
internal/application/queries/post_find_all_by_user_name.go
Normal file
11
internal/application/queries/post_find_all_by_user_name.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package queries
|
||||
|
||||
import "58team_blog/internal/application/common"
|
||||
|
||||
type PostFindAllByUserNameQuery struct {
|
||||
UserName string
|
||||
}
|
||||
|
||||
type PostFindAllByUserNameResult struct {
|
||||
Result *common.PostResultList
|
||||
}
|
||||
15
internal/application/queries/post_find_by_id.go
Normal file
15
internal/application/queries/post_find_by_id.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostFindByIdQuery struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
|
||||
type PostFindByIdResult struct {
|
||||
Result *common.PostResult
|
||||
}
|
||||
7
internal/application/queries/post_get_all.go
Normal file
7
internal/application/queries/post_get_all.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package queries
|
||||
|
||||
import "58team_blog/internal/application/common"
|
||||
|
||||
type PostGetAllResult struct {
|
||||
Result *common.PostResultList
|
||||
}
|
||||
15
internal/application/queries/posts_find_all_by_user_id.go
Normal file
15
internal/application/queries/posts_find_all_by_user_id.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostsFindAllByUserIdQuery struct {
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsFindAllByUserIdResult struct {
|
||||
Result *common.PostsResultList
|
||||
}
|
||||
15
internal/application/queries/posts_find_by_post_id.go
Normal file
15
internal/application/queries/posts_find_by_post_id.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostsFindByPostIdQuery struct {
|
||||
PostId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsFindByPostIdResult struct {
|
||||
Result *common.PostsResult
|
||||
}
|
||||
15
internal/application/queries/posts_find_by_user_id.go
Normal file
15
internal/application/queries/posts_find_by_user_id.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostsFindByUserIdQuery struct {
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsFindByUserIdResult struct {
|
||||
Result *common.PostsResult
|
||||
}
|
||||
7
internal/application/queries/posts_get_all.go
Normal file
7
internal/application/queries/posts_get_all.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package queries
|
||||
|
||||
import "58team_blog/internal/application/common"
|
||||
|
||||
type PostsGetAllResult struct {
|
||||
Result *common.PostsResultList
|
||||
}
|
||||
15
internal/application/queries/user_find_by_id.go
Normal file
15
internal/application/queries/user_find_by_id.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UserFindByIdQuery struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
|
||||
type UserFindByIdResult struct {
|
||||
Result *common.UserResult
|
||||
}
|
||||
11
internal/application/queries/user_find_by_name.go
Normal file
11
internal/application/queries/user_find_by_name.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package queries
|
||||
|
||||
import "58team_blog/internal/application/common"
|
||||
|
||||
type UserFindByNameQuery struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type UserFindByNameResult struct {
|
||||
Result *common.UserResult
|
||||
}
|
||||
7
internal/application/queries/user_get_all.go
Normal file
7
internal/application/queries/user_get_all.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package queries
|
||||
|
||||
import "58team_blog/internal/application/common"
|
||||
|
||||
type UserGetAllResult struct {
|
||||
Result *common.UserResultList
|
||||
}
|
||||
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