mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 19:53:21 +02:00
clean: Article to Post in domain and related use cases
Renamed the 'Article' domain to 'Post' in order to better reflect the purpose and utility of the object. The change involved renaming all related use cases, classes, and other references from 'Article' to 'Post' and updating all functions and data types to work with Posts instead of Articles.
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
package gateways
|
||||
|
||||
import (
|
||||
entity "GohCMS2/adapters/secondary/gateways/models"
|
||||
domain "GohCMS2/domain/article"
|
||||
"GohCMS2/domain/gateways"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ArticleRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewArticleRepository(db *gorm.DB) *ArticleRepository {
|
||||
return &ArticleRepository{db}
|
||||
}
|
||||
|
||||
func mapArticleToDomain(article entity.Article) domain.Article {
|
||||
return domain.FromDb(article.ID, article.Title, article.Body, article.CreatedAt, article.UpdatedAt)
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) Get(id uint32) (domain.Article, error) {
|
||||
var article entity.Article
|
||||
err := a.db.Model(&entity.Article{}).First(&article, id).Error
|
||||
if err != nil {
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
return mapArticleToDomain(article), nil
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) Create(article domain.Article) (domain.Article, error) {
|
||||
creationResult := a.db.Create(&entity.Article{
|
||||
Title: article.Title,
|
||||
Body: article.Body,
|
||||
})
|
||||
if creationResult.Error != nil {
|
||||
return domain.Article{}, creationResult.Error
|
||||
}
|
||||
|
||||
var createdArticle entity.Article
|
||||
creationResult.Scan(&createdArticle)
|
||||
|
||||
return mapArticleToDomain(createdArticle), nil
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) GetAll() []domain.Article {
|
||||
var articles []entity.Article
|
||||
err := a.db.Model(&entity.Article{}).Find(&articles).Error
|
||||
if err != nil {
|
||||
return []domain.Article{}
|
||||
}
|
||||
|
||||
var domainArticles = make([]domain.Article, 0)
|
||||
for _, article := range articles {
|
||||
domainArticles = append(domainArticles, mapArticleToDomain(article))
|
||||
}
|
||||
|
||||
return domainArticles
|
||||
}
|
||||
|
||||
var _ gateways.IArticleRepository = &ArticleRepository{}
|
||||
+1
-1
@@ -5,7 +5,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
type Post struct {
|
||||
gorm.Model
|
||||
ID uint32 `gorm:"primary_key;auto_increment;not_null"`
|
||||
Title string
|
||||
@@ -0,0 +1,62 @@
|
||||
package gateways
|
||||
|
||||
import (
|
||||
entity "GohCMS2/adapters/secondary/gateways/models"
|
||||
"GohCMS2/domain/gateways"
|
||||
domain "GohCMS2/domain/post"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PostRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPostRepository(db *gorm.DB) *PostRepository {
|
||||
return &PostRepository{db}
|
||||
}
|
||||
|
||||
func mapPostToDomain(post entity.Post) domain.Post {
|
||||
return domain.FromDb(post.ID, post.Title, post.Body, post.CreatedAt, post.UpdatedAt)
|
||||
}
|
||||
|
||||
func (a *PostRepository) Get(id uint32) (domain.Post, error) {
|
||||
var article entity.Post
|
||||
err := a.db.Model(&entity.Post{}).First(&article, id).Error
|
||||
if err != nil {
|
||||
return domain.Post{}, err
|
||||
}
|
||||
|
||||
return mapPostToDomain(article), nil
|
||||
}
|
||||
|
||||
func (a *PostRepository) Create(article domain.Post) (domain.Post, error) {
|
||||
creationResult := a.db.Create(&entity.Post{
|
||||
Title: article.Title,
|
||||
Body: article.Body,
|
||||
})
|
||||
if creationResult.Error != nil {
|
||||
return domain.Post{}, creationResult.Error
|
||||
}
|
||||
|
||||
var createdArticle entity.Post
|
||||
creationResult.Scan(&createdArticle)
|
||||
|
||||
return mapPostToDomain(createdArticle), nil
|
||||
}
|
||||
|
||||
func (a *PostRepository) GetAll() []domain.Post {
|
||||
var articles []entity.Post
|
||||
err := a.db.Model(&entity.Post{}).Find(&articles).Error
|
||||
if err != nil {
|
||||
return []domain.Post{}
|
||||
}
|
||||
|
||||
var domainArticles = make([]domain.Post, 0)
|
||||
for _, article := range articles {
|
||||
domainArticles = append(domainArticles, mapPostToDomain(article))
|
||||
}
|
||||
|
||||
return domainArticles
|
||||
}
|
||||
|
||||
var _ gateways.IArticleRepository = &PostRepository{}
|
||||
@@ -16,6 +16,7 @@
|
||||
<div class="container mt-3 text-black-50">
|
||||
<h1>Accueil</h1>
|
||||
<p>Bienvenue sur GohCMS !</p>
|
||||
<button class="btn btn-outline-primary">Posts »</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user