refac: repository updates, image attachment and deletes report the domain not-found error for an unknown id, the same for articles, users and images, and the test fakes stop returning GORM errors

This commit is contained in:
fsylvain committed 2026-09-23 22:09:06 +02:00
1 parent 4278d96882
commit 6a2ca07b66
14 files changed
+553 -66

No files matched your search

@@ -2,6 +2,7 @@ package persistence
import (
domain "RenewCMS/internal/domain/article"
imageDomain "RenewCMS/internal/domain/image"
"RenewCMS/internal/infrastructure/persistence/mappers"
entity "RenewCMS/internal/infrastructure/persistence/models"
@@ -89,7 +90,7 @@ func (a *ArticleRepository) UpdateBody(id uint32, body string) (domain.Article,
var localArticle entity.Article
err := a.db.Model(&entity.Article{}).First(&localArticle, id).Error
if err != nil {
return domain.Article{}, err
return domain.Article{}, mapNotFoundErr(err, domain.ErrArticleNotFound)
}
localArticle.Body = body
@@ -107,7 +108,7 @@ func (a *ArticleRepository) UpdateIsOnline(id uint32, isOnline bool) (domain.Art
var localArticle entity.Article
err := a.db.Model(&entity.Article{}).First(&localArticle, id).Error
if err != nil {
return domain.Article{}, err
return domain.Article{}, mapNotFoundErr(err, domain.ErrArticleNotFound)
}
localArticle.IsOnline = isOnline
@@ -120,20 +121,21 @@ func (a *ArticleRepository) UpdateIsOnline(id uint32, isOnline bool) (domain.Art
}
func (a *ArticleRepository) Delete(id uint32) error {
return a.db.Delete(&entity.Article{}, id).Error
result := a.db.Delete(&entity.Article{}, id)
return deleteOrNotFound(result, domain.ErrArticleNotFound)
}
func (a *ArticleRepository) AddImage(articleId uint32, imageId uint32) error {
var localArticle entity.Article
err := a.db.Model(&entity.Article{}).First(&localArticle, articleId).Error
if err != nil {
return err
return mapNotFoundErr(err, domain.ErrArticleNotFound)
}
var localImage entity.Image
err = a.db.Model(&entity.Image{}).First(&localImage, imageId).Error
if err != nil {
return err
return mapNotFoundErr(err, imageDomain.ErrImageNotFound)
}
err = a.db.Model(&localArticle).Association("Images").Append(&localImage)