diff --git a/api/dependecyInjection.go b/api/dependecyInjection.go index 76a1b05..538c1e0 100644 --- a/api/dependecyInjection.go +++ b/api/dependecyInjection.go @@ -6,8 +6,8 @@ import ( domainMail "RenewCMS/internal/domain/mail" domainUser "RenewCMS/internal/domain/user" "RenewCMS/internal/infrastructure/mailer" - "RenewCMS/internal/infrastructure/persistence" "RenewCMS/internal/infrastructure/persistence/models" + persistence "RenewCMS/internal/infrastructure/persistence/repositories" "RenewCMS/internal/infrastructure/useCases" "os" "path/filepath" diff --git a/api/handlers/article/article.go b/api/handlers/article/articleHandler.go similarity index 100% rename from api/handlers/article/article.go rename to api/handlers/article/articleHandler.go diff --git a/api/handlers/auth/auth.go b/api/handlers/auth/authHandler.go similarity index 100% rename from api/handlers/auth/auth.go rename to api/handlers/auth/authHandler.go diff --git a/api/handlers/image/image.go b/api/handlers/image/imageHandler.go similarity index 100% rename from api/handlers/image/image.go rename to api/handlers/image/imageHandler.go diff --git a/internal/domain/article/article.go b/internal/domain/article/article.go deleted file mode 100644 index 8389aee..0000000 --- a/internal/domain/article/article.go +++ /dev/null @@ -1,46 +0,0 @@ -package article - -import ( - domain "RenewCMS/internal/domain/image" - "time" -) - -type Article struct { - ID uint32 `json:"id"` - Title string `json:"title"` - Body string `json:"body"` - Images []*domain.Image `json:"images"` - IsOnline bool `json:"is_online"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func FromApi( - title string, - body string, -) Article { - return Article{ - Title: title, - Body: body, - } -} - -func FromDb( - id uint32, - title string, - body string, - images []*domain.Image, - isOnline bool, - createdAt time.Time, - updatedAt time.Time, -) Article { - return Article{ - ID: id, - Title: title, - Body: body, - Images: images, - IsOnline: isOnline, - CreatedAt: createdAt, - UpdatedAt: updatedAt, - } -} diff --git a/internal/domain/article/articleDomain.go b/internal/domain/article/articleDomain.go new file mode 100644 index 0000000..c6b1dc2 --- /dev/null +++ b/internal/domain/article/articleDomain.go @@ -0,0 +1,16 @@ +package article + +import ( + domain "RenewCMS/internal/domain/image" + "time" +) + +type Article struct { + ID uint32 `json:"id"` + Title string `json:"title"` + Body string `json:"body"` + Images []*domain.Image `json:"images"` + IsOnline bool `json:"is_online"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} diff --git a/internal/domain/article/repository.go b/internal/domain/article/articleRepositoryInterface.go similarity index 100% rename from internal/domain/article/repository.go rename to internal/domain/article/articleRepositoryInterface.go diff --git a/internal/domain/image/image.go b/internal/domain/image/imageDomain.go similarity index 51% rename from internal/domain/image/image.go rename to internal/domain/image/imageDomain.go index 2591776..c3d740a 100644 --- a/internal/domain/image/image.go +++ b/internal/domain/image/imageDomain.go @@ -11,13 +11,3 @@ type Image struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } - -func FromDB(id uint32, path string, articleId uint32, createdAt time.Time, updatedAt time.Time) Image { - return Image{ - ID: id, - Path: path, - ArticleID: articleId, - CreatedAt: createdAt, - UpdatedAt: updatedAt, - } -} diff --git a/internal/domain/image/repository.go b/internal/domain/image/imageRepositoryInterface.go similarity index 100% rename from internal/domain/image/repository.go rename to internal/domain/image/imageRepositoryInterface.go diff --git a/internal/domain/mail/repository.go b/internal/domain/mail/mailRepositoryInterface.go similarity index 100% rename from internal/domain/mail/repository.go rename to internal/domain/mail/mailRepositoryInterface.go diff --git a/internal/domain/user/user.go b/internal/domain/user/user.go deleted file mode 100644 index ddb3689..0000000 --- a/internal/domain/user/user.go +++ /dev/null @@ -1,63 +0,0 @@ -package user - -import ( - "time" -) - -type User struct { - ID uint32 `json:"id"` - Username string `json:"username"` - Password string `json:"password"` - PasswordResetCode string `json:"password_reset_code"` - Email string `json:"email"` - IsVerified bool `json:"is_verified"` - VerificationCode string `json:"verification_code"` - VerificationExpiration time.Time `json:"verification_expiration"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func FromApi( - username string, - password string, - passwordResetCode string, - email string, - verificationCode string, -) User { - expiration := time.Now().Add(2 * time.Hour) - return User{ - Username: username, - Password: password, - PasswordResetCode: passwordResetCode, - Email: email, - IsVerified: false, - VerificationCode: verificationCode, - VerificationExpiration: expiration, - } -} - -func FromDb( - id uint32, - username string, - password string, - passwordResetCode string, - email string, - isVerified bool, - verificationCode string, - verificationExpiration time.Time, - createdAt time.Time, - updatedAt time.Time, -) User { - return User{ - ID: id, - Username: username, - Password: password, - PasswordResetCode: passwordResetCode, - Email: email, - IsVerified: isVerified, - VerificationCode: verificationCode, - VerificationExpiration: verificationExpiration, - CreatedAt: createdAt, - UpdatedAt: updatedAt, - } -} diff --git a/internal/domain/user/userDomain.go b/internal/domain/user/userDomain.go new file mode 100644 index 0000000..ba3e50f --- /dev/null +++ b/internal/domain/user/userDomain.go @@ -0,0 +1,18 @@ +package user + +import ( + "time" +) + +type User struct { + ID uint32 `json:"id"` + Username string `json:"username"` + Password string `json:"password"` + PasswordResetCode string `json:"password_reset_code"` + Email string `json:"email"` + IsVerified bool `json:"is_verified"` + VerificationCode string `json:"verification_code"` + VerificationExpiration time.Time `json:"verification_expiration"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} diff --git a/internal/domain/user/repository.go b/internal/domain/user/userRepositoryInterface.go similarity index 100% rename from internal/domain/user/repository.go rename to internal/domain/user/userRepositoryInterface.go diff --git a/internal/infrastructure/persistence/mappers/articleMapper.go b/internal/infrastructure/persistence/mappers/articleMapper.go new file mode 100644 index 0000000..1df395b --- /dev/null +++ b/internal/infrastructure/persistence/mappers/articleMapper.go @@ -0,0 +1,42 @@ +package mappers + +import ( + "RenewCMS/internal/domain/article" + "RenewCMS/internal/domain/image" + "RenewCMS/internal/infrastructure/persistence/models" +) + +func ArticleToDomain(m models.Article) article.Article { + domainImages := make([]*image.Image, len(m.Images)) + for i, img := range m.Images { + domainImg := ImageToDomain(*img) + domainImages[i] = &domainImg + } + + return article.Article{ + ID: m.ID, + Title: m.Title, + Body: m.Body, + Images: domainImages, + IsOnline: m.IsOnline, + CreatedAt: m.CreatedAt, + UpdatedAt: m.UpdatedAt, + } +} + +func ArticleToModel(a article.Article) models.Article { + modelImages := make([]*models.Image, len(a.Images)) + for i, img := range a.Images { + modelImage := ImageToModel(*img) + modelImages[i] = &modelImage + } + + return models.Article{ + Title: a.Title, + Body: a.Body, + Images: modelImages, + IsOnline: a.IsOnline, + CreatedAt: a.CreatedAt, + UpdatedAt: a.UpdatedAt, + } +} diff --git a/internal/infrastructure/persistence/mappers/imageMapper.go b/internal/infrastructure/persistence/mappers/imageMapper.go new file mode 100644 index 0000000..ca85a8b --- /dev/null +++ b/internal/infrastructure/persistence/mappers/imageMapper.go @@ -0,0 +1,25 @@ +package mappers + +import ( + "RenewCMS/internal/domain/image" + "RenewCMS/internal/infrastructure/persistence/models" +) + +func ImageToDomain(imageModel models.Image) image.Image { + return image.Image{ + ID: imageModel.ID, + Path: imageModel.Path, + ArticleID: imageModel.ArticleID, + CreatedAt: imageModel.CreatedAt, + UpdatedAt: imageModel.UpdatedAt, + } +} + +func ImageToModel(imageDomain image.Image) models.Image { + return models.Image{ + Path: imageDomain.Path, + ArticleID: imageDomain.ArticleID, + CreatedAt: imageDomain.CreatedAt, + UpdatedAt: imageDomain.UpdatedAt, + } +} diff --git a/internal/infrastructure/persistence/mappers/userMapper.go b/internal/infrastructure/persistence/mappers/userMapper.go new file mode 100644 index 0000000..d08b675 --- /dev/null +++ b/internal/infrastructure/persistence/mappers/userMapper.go @@ -0,0 +1,35 @@ +package mappers + +import ( + "RenewCMS/internal/domain/user" + "RenewCMS/internal/infrastructure/persistence/models" +) + +func UserToDomain(m models.User) user.User { + return user.User{ + ID: m.ID, + Username: m.Username, + Password: m.Password, + PasswordResetCode: m.PasswordResetCode, + Email: m.Email, + IsVerified: m.IsVerified, + VerificationCode: m.VerificationCode, + VerificationExpiration: m.VerificationExpiration, + CreatedAt: m.CreatedAt, + UpdatedAt: m.UpdatedAt, + } +} + +func UserToModel(a user.User) models.User { + return models.User{ + Username: a.Username, + Password: a.Password, + PasswordResetCode: a.PasswordResetCode, + Email: a.Email, + IsVerified: a.IsVerified, + VerificationCode: a.VerificationCode, + VerificationExpiration: a.VerificationExpiration, + CreatedAt: a.CreatedAt, + UpdatedAt: a.UpdatedAt, + } +} diff --git a/internal/infrastructure/persistence/models/article.go b/internal/infrastructure/persistence/models/articleModel.go similarity index 100% rename from internal/infrastructure/persistence/models/article.go rename to internal/infrastructure/persistence/models/articleModel.go diff --git a/internal/infrastructure/persistence/models/image.go b/internal/infrastructure/persistence/models/imageModel.go similarity index 100% rename from internal/infrastructure/persistence/models/image.go rename to internal/infrastructure/persistence/models/imageModel.go diff --git a/internal/infrastructure/persistence/models/user.go b/internal/infrastructure/persistence/models/userModel.go similarity index 99% rename from internal/infrastructure/persistence/models/user.go rename to internal/infrastructure/persistence/models/userModel.go index 141a836..e58d852 100644 --- a/internal/infrastructure/persistence/models/user.go +++ b/internal/infrastructure/persistence/models/userModel.go @@ -1,8 +1,9 @@ package models import ( - "gorm.io/gorm" "time" + + "gorm.io/gorm" ) type User struct { diff --git a/internal/infrastructure/persistence/articleRepository.go b/internal/infrastructure/persistence/repositories/articleRepository.go similarity index 78% rename from internal/infrastructure/persistence/articleRepository.go rename to internal/infrastructure/persistence/repositories/articleRepository.go index b0a32cc..30f67bf 100644 --- a/internal/infrastructure/persistence/articleRepository.go +++ b/internal/infrastructure/persistence/repositories/articleRepository.go @@ -2,7 +2,7 @@ package persistence import ( domainArticle "RenewCMS/internal/domain/article" - domainImage "RenewCMS/internal/domain/image" + "RenewCMS/internal/infrastructure/persistence/mappers" entity "RenewCMS/internal/infrastructure/persistence/models" "gorm.io/gorm" @@ -16,24 +16,6 @@ func NewArticleRepository(db *gorm.DB) *ArticleRepository { return &ArticleRepository{db} } -func mapArticleToDomain(article entity.Article) domainArticle.Article { - domainImages := make([]*domainImage.Image, len(article.Images)) - for i, img := range article.Images { - dImg := domainImage.FromDB(img.ID, img.Path, img.ArticleID, img.CreatedAt, img.UpdatedAt) - domainImages[i] = &dImg - } - - return domainArticle.FromDb( - article.ID, - article.Title, - article.Body, - domainImages, - article.IsOnline, - article.CreatedAt, - article.UpdatedAt, - ) -} - func (a *ArticleRepository) Get(id uint32) (domainArticle.Article, error) { var article entity.Article err := a.db.Model(&entity.Article{}).Preload("Images").First(&article, id).Error @@ -41,7 +23,7 @@ func (a *ArticleRepository) Get(id uint32) (domainArticle.Article, error) { return domainArticle.Article{}, err } - return mapArticleToDomain(article), nil + return mappers.ArticleToDomain(article), nil } func (a *ArticleRepository) GetByName(name string) (domainArticle.Article, error) { @@ -51,7 +33,7 @@ func (a *ArticleRepository) GetByName(name string) (domainArticle.Article, error return domainArticle.Article{}, err } - return mapArticleToDomain(article), nil + return mappers.ArticleToDomain(article), nil } func (a *ArticleRepository) Create(article domainArticle.Article) (domainArticle.Article, error) { @@ -66,7 +48,7 @@ func (a *ArticleRepository) Create(article domainArticle.Article) (domainArticle var createdArticle entity.Article creationResult.Scan(&createdArticle) - return mapArticleToDomain(createdArticle), nil + return mappers.ArticleToDomain(createdArticle), nil } func (a *ArticleRepository) GetAll() []domainArticle.Article { @@ -78,7 +60,7 @@ func (a *ArticleRepository) GetAll() []domainArticle.Article { var domainArticles = make([]domainArticle.Article, 0) for _, article := range articles { - domainArticles = append(domainArticles, mapArticleToDomain(article)) + domainArticles = append(domainArticles, mappers.ArticleToDomain(article)) } return domainArticles @@ -97,7 +79,7 @@ func (a *ArticleRepository) UpdateBody(id uint32, body string) (domainArticle.Ar return domainArticle.Article{}, err } - newArticle := mapArticleToDomain(localArticle) + newArticle := mappers.ArticleToDomain(localArticle) return newArticle, nil } @@ -115,7 +97,7 @@ func (a *ArticleRepository) UpdateIsOnline(id uint32, isOnline bool) (domainArti return domainArticle.Article{}, err } - return mapArticleToDomain(localArticle), nil + return mappers.ArticleToDomain(localArticle), nil } func (a *ArticleRepository) Delete(id uint32) error { diff --git a/internal/infrastructure/persistence/imageRepository.go b/internal/infrastructure/persistence/repositories/imageRepository.go similarity index 89% rename from internal/infrastructure/persistence/imageRepository.go rename to internal/infrastructure/persistence/repositories/imageRepository.go index de72464..7984308 100644 --- a/internal/infrastructure/persistence/imageRepository.go +++ b/internal/infrastructure/persistence/repositories/imageRepository.go @@ -2,6 +2,7 @@ package persistence import ( domain "RenewCMS/internal/domain/image" + "RenewCMS/internal/infrastructure/persistence/mappers" entity "RenewCMS/internal/infrastructure/persistence/models" "errors" "mime/multipart" @@ -27,16 +28,6 @@ var contentTypeExtensions = map[string]string{ "image/svg+xml": ".svg", } -func mapImageToDomain(image entity.Image) domain.Image { - return domain.FromDB( - image.ID, - image.Path, - image.ArticleID, - image.CreatedAt, - image.UpdatedAt, - ) -} - func (i ImageRepository) Create(file multipart.File, fileHeader multipart.FileHeader) (domain.Image, error) { uploadDir := os.Getenv("UPLOAD_DIR") @@ -68,7 +59,7 @@ func (i ImageRepository) Create(file multipart.File, fileHeader multipart.FileHe var createdImage entity.Image newImage.Scan(&createdImage) - return mapImageToDomain(createdImage), nil + return mappers.ImageToDomain(createdImage), nil } func (i ImageRepository) Delete(id uint32) error { diff --git a/internal/infrastructure/persistence/userRepository.go b/internal/infrastructure/persistence/repositories/userRepository.go similarity index 84% rename from internal/infrastructure/persistence/userRepository.go rename to internal/infrastructure/persistence/repositories/userRepository.go index a545a7e..decde19 100644 --- a/internal/infrastructure/persistence/userRepository.go +++ b/internal/infrastructure/persistence/repositories/userRepository.go @@ -2,6 +2,7 @@ package persistence import ( domainUser "RenewCMS/internal/domain/user" + "RenewCMS/internal/infrastructure/persistence/mappers" entity "RenewCMS/internal/infrastructure/persistence/models" "golang.org/x/crypto/bcrypt" @@ -16,20 +17,6 @@ func NewUserRepository(db *gorm.DB) *UserRepository { return &UserRepository{db} } -func mapUserToDomain(user entity.User) domainUser.User { - return domainUser.FromDb( - user.ID, - user.Username, - user.Password, - user.PasswordResetCode, - user.Email, - user.IsVerified, - user.VerificationCode, - user.VerificationExpiration, - user.CreatedAt, user.UpdatedAt, - ) -} - func (u *UserRepository) Get(id uint32) (domainUser.User, error) { var localUser entity.User err := u.db.Model(&entity.User{}).First(&localUser, id).Error @@ -37,7 +24,7 @@ func (u *UserRepository) Get(id uint32) (domainUser.User, error) { return domainUser.User{}, err } - return mapUserToDomain(localUser), nil + return mappers.UserToDomain(localUser), nil } func (u *UserRepository) Create(user domainUser.User) (domainUser.User, error) { @@ -58,7 +45,7 @@ func (u *UserRepository) Create(user domainUser.User) (domainUser.User, error) { var createdUser entity.User creationResult.Scan(&createdUser) - return mapUserToDomain(createdUser), + return mappers.UserToDomain(createdUser), nil } @@ -72,7 +59,7 @@ func (u *UserRepository) GetAll() []domainUser.User { var domainUsers []domainUser.User for _, localUser := range users { - domainUsers = append(domainUsers, mapUserToDomain(localUser)) + domainUsers = append(domainUsers, mappers.UserToDomain(localUser)) } return domainUsers @@ -85,7 +72,7 @@ func (u *UserRepository) GetByUsername(username string) (domainUser.User, error) return domainUser.User{}, err } - return mapUserToDomain(localUser), nil + return mappers.UserToDomain(localUser), nil } func (u *UserRepository) GetByEmail(email string) (domainUser.User, error) { @@ -95,7 +82,7 @@ func (u *UserRepository) GetByEmail(email string) (domainUser.User, error) { return domainUser.User{}, err } - return mapUserToDomain(localUser), nil + return mappers.UserToDomain(localUser), nil } func (u *UserRepository) UpdateVerificationStatus(userId uint32, isVerified bool) (domainUser.User, error) { @@ -111,7 +98,7 @@ func (u *UserRepository) UpdateVerificationStatus(userId uint32, isVerified bool return domainUser.User{}, err } - return mapUserToDomain(localUser), nil + return mappers.UserToDomain(localUser), nil } func (u *UserRepository) UpdatePassword(userId uint32, password string) (domainUser.User, error) { @@ -128,7 +115,7 @@ func (u *UserRepository) UpdatePassword(userId uint32, password string) (domainU return domainUser.User{}, err } - return mapUserToDomain(localUser), nil + return mappers.UserToDomain(localUser), nil } func (u *UserRepository) UpdatePasswordResetCode(userId uint32, code string) (domainUser.User, error) { @@ -145,7 +132,7 @@ func (u *UserRepository) UpdatePasswordResetCode(userId uint32, code string) (do return domainUser.User{}, err } - return mapUserToDomain(localUser), nil + return mappers.UserToDomain(localUser), nil } var _ domainUser.Repository = &UserRepository{} diff --git a/internal/infrastructure/useCases/CreateArticle.go b/internal/infrastructure/useCases/CreateArticle.go index 3b94cc2..dd65eac 100644 --- a/internal/infrastructure/useCases/CreateArticle.go +++ b/internal/infrastructure/useCases/CreateArticle.go @@ -18,8 +18,8 @@ func NewCreateArticleUseCase(articleRepository article.Repository) *CreateArticl } func (g *CreateArticleUseCase) CreateArticle(createArticle CreateArticleCommand) (article.Article, error) { - return g.articleRepository.Create(article.FromApi( - createArticle.Title, - createArticle.Body, - )) + return g.articleRepository.Create(article.Article{ + Title: createArticle.Title, + Body: createArticle.Body, + }) } diff --git a/internal/infrastructure/useCases/CreateUser.go b/internal/infrastructure/useCases/CreateUser.go index c1487ed..10ede94 100644 --- a/internal/infrastructure/useCases/CreateUser.go +++ b/internal/infrastructure/useCases/CreateUser.go @@ -2,6 +2,7 @@ package useCases import ( "RenewCMS/internal/domain/user" + "time" "github.com/google/uuid" ) @@ -23,13 +24,15 @@ func NewCreateUserUseCase(userRepository user.Repository) *CreateUserUseCase { func (g *CreateUserUseCase) CreateUser(createUser CreateUserCommand) (user.User, error) { rawUuid := uuid.NewString() - createdUser, err := g.userRepository.Create(user.FromApi( - createUser.Username, - createUser.Password, - "", - createUser.Email, - rawUuid, - )) + createdUser, err := g.userRepository.Create(user.User{ + Username: createUser.Username, + Password: createUser.Password, + PasswordResetCode: "", + Email: createUser.Email, + IsVerified: false, + VerificationCode: rawUuid, + VerificationExpiration: time.Now().Add(2 * time.Hour), + }) if err != nil { return user.User{}, err }