mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 19:53:21 +02:00
refac: mappers, file names
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,8 +1,9 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
+7
-25
@@ -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
-11
@@ -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 {
|
||||
+9
-22
@@ -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{}
|
||||
Reference in New Issue
Block a user