Added authorization
This commit is contained in:
parent
c3c3d65d32
commit
b96dd39795
50 changed files with 685 additions and 410 deletions
|
|
@ -7,4 +7,6 @@ type CreatePostCommand struct {
|
|||
Title string
|
||||
Description string
|
||||
Content string
|
||||
Category string
|
||||
Tags []string
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type CreatePostsCommand struct {
|
||||
PostId uuid.UUID
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package commands
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type DeletePostsCommand struct {
|
||||
Id uuid.UUID
|
||||
}
|
||||
|
|
@ -14,6 +14,8 @@ type PostResult struct {
|
|||
Content string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Category string
|
||||
Tags []string
|
||||
}
|
||||
|
||||
type PostResultList struct {
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
package common
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type PostsResult struct {
|
||||
Id uuid.UUID
|
||||
UserId uuid.UUID
|
||||
PostId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsResultList struct {
|
||||
Result []*PostsResult
|
||||
}
|
||||
15
internal/application/errors/read_file_error.go
Normal file
15
internal/application/errors/read_file_error.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package errors
|
||||
|
||||
type ReadFileError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func NewReadFileError(msg string) *ReadFileError {
|
||||
return &ReadFileError{
|
||||
msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ReadFileError) Error() string {
|
||||
return "Read file error: " + e.msg
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
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
|
||||
}
|
||||
|
|
@ -15,6 +15,8 @@ func CreatePostResultFromEntity(entity *entities.Post) *common.PostResult {
|
|||
Content: entity.Content,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Category: entity.Category,
|
||||
Tags: entity.Tags,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
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),
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostsFindAllByUserIdQuery struct {
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsFindAllByUserIdResult struct {
|
||||
Result *common.PostsResultList
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostsFindByPostIdQuery struct {
|
||||
PostId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsFindByPostIdResult struct {
|
||||
Result *common.PostsResult
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"58team_blog/internal/application/common"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PostsFindByUserIdQuery struct {
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type PostsFindByUserIdResult struct {
|
||||
Result *common.PostsResult
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package queries
|
||||
|
||||
import "58team_blog/internal/application/common"
|
||||
|
||||
type PostsGetAllResult struct {
|
||||
Result *common.PostsResultList
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ type ImagesService struct {
|
|||
repo repository.ImagesRepository
|
||||
}
|
||||
|
||||
func NewImagesService(repo repository.ImagesRepository) ImagesService {
|
||||
func CreateImagesService(repo repository.ImagesRepository) ImagesService {
|
||||
return ImagesService{
|
||||
repo: repo,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ 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)
|
||||
entity, err := entities.CreatePost(cmd.UserId, cmd.Title, cmd.Description, cmd.Content, cmd.Category, cmd.Tags)
|
||||
if err != nil {
|
||||
return nil, errors.NewValidationError("Invalid input data " + err.Error())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,141 +0,0 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue