refac: mappers, file names

This commit is contained in:
Floriansylvain
2025-12-22 00:06:17 +01:00
parent e0a6706d4e
commit b50efae8a8
24 changed files with 171 additions and 190 deletions
+1 -1
View File
@@ -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"
-46
View File
@@ -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,
}
}
+16
View File
@@ -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"`
}
@@ -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,
}
}
-63
View File
@@ -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,
}
}
+18
View File
@@ -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"`
}
@@ -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,
}
}
@@ -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,
}
}
@@ -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,
}
}
@@ -1,8 +1,9 @@
package models
import (
"gorm.io/gorm"
"time"
"gorm.io/gorm"
)
type User struct {
@@ -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 {
@@ -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 {
@@ -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{}
@@ -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,
})
}
+10 -7
View File
@@ -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
}