mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
refac: better error handling & moved IsOnline logic to cleaner place
This commit is contained in:
@@ -3,9 +3,12 @@ package article
|
||||
import (
|
||||
"RenewCMS/api/pkg/apperr"
|
||||
validator "RenewCMS/api/pkg/validator"
|
||||
domainArticle "RenewCMS/internal/domain/article"
|
||||
domain "RenewCMS/internal/domain/article"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -40,26 +43,35 @@ func (h *Handler) GetArticle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
localArticle, err := h.GetUseCase.GetArticle(uint32(id))
|
||||
if err != nil || !localArticle.IsOnline {
|
||||
http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", http.StatusNotFound)
|
||||
article, err := h.GetUseCase.GetOnlineArticle(uint32(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrArticleNotFound) {
|
||||
http.Error(w, "Article not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
articleJson, _ := json.Marshal(localArticle)
|
||||
_, _ = w.Write(articleJson)
|
||||
articleJson, err := json.Marshal(article)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := w.Write(articleJson); err != nil {
|
||||
fmt.Printf("Failed to write response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) PostArticle(w http.ResponseWriter, r *http.Request) {
|
||||
var localArticle PostArticle
|
||||
err := json.NewDecoder(r.Body).Decode(&localArticle)
|
||||
if err != nil {
|
||||
if err := json.NewDecoder(r.Body).Decode(&localArticle); err != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = validator.Validate.Struct(localArticle)
|
||||
if err != nil {
|
||||
if err := validator.Validate.Struct(localArticle); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -69,24 +81,38 @@ func (h *Handler) PostArticle(w http.ResponseWriter, r *http.Request) {
|
||||
Body: localArticle.Body,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Failed to create article", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
articleJson, _ := json.Marshal(createdArticle)
|
||||
|
||||
_, _ = w.Write(articleJson)
|
||||
articleJson, err := json.Marshal(createdArticle)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
if _, err := w.Write(articleJson); err != nil {
|
||||
log.Printf("Failed to write response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ListArticles(w http.ResponseWriter, _ *http.Request) {
|
||||
articles := h.ListUseCase.ListArticles()
|
||||
onlineArticles := make([]domainArticle.Article, 0)
|
||||
for _, localArticle := range articles {
|
||||
if localArticle.IsOnline {
|
||||
onlineArticles = append(onlineArticles, localArticle)
|
||||
}
|
||||
articles, err := h.ListUseCase.ListOnlineArticles()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to retrieve articles", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
articlesJson, err := json.Marshal(articles)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := w.Write(articlesJson); err != nil {
|
||||
log.Printf("Failed to write response: %v", err)
|
||||
}
|
||||
articlesJson, _ := json.Marshal(onlineArticles)
|
||||
_, _ = w.Write(articlesJson)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteArticle(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -96,11 +122,10 @@ func (h *Handler) DeleteArticle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = h.DeleteUseCase.DeleteArticle(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
if err := h.DeleteUseCase.DeleteArticle(uint32(id)); err != nil {
|
||||
http.Error(w, "Failed to delete article", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = w.Write([]byte("article deleted"))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,15 @@ package article
|
||||
|
||||
import (
|
||||
domain "RenewCMS/internal/domain/image"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrArticleNotFound = errors.New("article not found")
|
||||
ErrArticleOffline = errors.New("article is offline")
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
ID uint32 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package article
|
||||
|
||||
type Filters struct {
|
||||
IsOnline *bool
|
||||
}
|
||||
|
||||
type Repository interface {
|
||||
FindByFilters(filters Filters) ([]Article, error)
|
||||
Get(id uint32) (Article, error)
|
||||
GetByName(name string) (Article, error)
|
||||
GetAll() []Article
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
domainArticle "RenewCMS/internal/domain/article"
|
||||
domain "RenewCMS/internal/domain/article"
|
||||
"RenewCMS/internal/infrastructure/persistence/mappers"
|
||||
entity "RenewCMS/internal/infrastructure/persistence/models"
|
||||
|
||||
@@ -16,33 +16,51 @@ func NewArticleRepository(db *gorm.DB) *ArticleRepository {
|
||||
return &ArticleRepository{db}
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) Get(id uint32) (domainArticle.Article, error) {
|
||||
func (a *ArticleRepository) FindByFilters(filters domain.Filters) ([]domain.Article, error) {
|
||||
query := a.db.Model(&entity.Article{})
|
||||
|
||||
if filters.IsOnline != nil {
|
||||
query = query.Where("is_online = ?", *filters.IsOnline)
|
||||
}
|
||||
|
||||
var entityArticles []entity.Article
|
||||
err := query.Find(&entityArticles).Error
|
||||
|
||||
domainArticles := make([]domain.Article, 0, len(entityArticles))
|
||||
for _, entityArticle := range entityArticles {
|
||||
domainArticles = append(domainArticles, mappers.ArticleToDomain(entityArticle))
|
||||
}
|
||||
|
||||
return domainArticles, err
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) Get(id uint32) (domain.Article, error) {
|
||||
var article entity.Article
|
||||
err := a.db.Model(&entity.Article{}).Preload("Images").First(&article, id).Error
|
||||
if err != nil {
|
||||
return domainArticle.Article{}, err
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
return mappers.ArticleToDomain(article), nil
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) GetByName(name string) (domainArticle.Article, error) {
|
||||
func (a *ArticleRepository) GetByName(name string) (domain.Article, error) {
|
||||
var article entity.Article
|
||||
err := a.db.Model(&entity.Article{}).Where("title = ?", name).First(&article).Error
|
||||
if err != nil {
|
||||
return domainArticle.Article{}, err
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
return mappers.ArticleToDomain(article), nil
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) Create(article domainArticle.Article) (domainArticle.Article, error) {
|
||||
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 domainArticle.Article{}, creationResult.Error
|
||||
return domain.Article{}, creationResult.Error
|
||||
}
|
||||
|
||||
var createdArticle entity.Article
|
||||
@@ -51,14 +69,14 @@ func (a *ArticleRepository) Create(article domainArticle.Article) (domainArticle
|
||||
return mappers.ArticleToDomain(createdArticle), nil
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) GetAll() []domainArticle.Article {
|
||||
func (a *ArticleRepository) GetAll() []domain.Article {
|
||||
var articles []entity.Article
|
||||
err := a.db.Model(&entity.Article{}).Find(&articles).Error
|
||||
if err != nil {
|
||||
return []domainArticle.Article{}
|
||||
return []domain.Article{}
|
||||
}
|
||||
|
||||
var domainArticles = make([]domainArticle.Article, 0)
|
||||
var domainArticles = make([]domain.Article, 0)
|
||||
for _, article := range articles {
|
||||
domainArticles = append(domainArticles, mappers.ArticleToDomain(article))
|
||||
}
|
||||
@@ -66,17 +84,17 @@ func (a *ArticleRepository) GetAll() []domainArticle.Article {
|
||||
return domainArticles
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) UpdateBody(id uint32, body string) (domainArticle.Article, error) {
|
||||
func (a *ArticleRepository) UpdateBody(id uint32, body string) (domain.Article, error) {
|
||||
var localArticle entity.Article
|
||||
err := a.db.Model(&entity.Article{}).First(&localArticle, id).Error
|
||||
if err != nil {
|
||||
return domainArticle.Article{}, err
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
localArticle.Body = body
|
||||
err = a.db.Save(&localArticle).Error
|
||||
if err != nil {
|
||||
return domainArticle.Article{}, err
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
newArticle := mappers.ArticleToDomain(localArticle)
|
||||
@@ -84,17 +102,17 @@ func (a *ArticleRepository) UpdateBody(id uint32, body string) (domainArticle.Ar
|
||||
return newArticle, nil
|
||||
}
|
||||
|
||||
func (a *ArticleRepository) UpdateIsOnline(id uint32, isOnline bool) (domainArticle.Article, error) {
|
||||
func (a *ArticleRepository) UpdateIsOnline(id uint32, isOnline bool) (domain.Article, error) {
|
||||
var localArticle entity.Article
|
||||
err := a.db.Model(&entity.Article{}).First(&localArticle, id).Error
|
||||
if err != nil {
|
||||
return domainArticle.Article{}, err
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
localArticle.IsOnline = isOnline
|
||||
err = a.db.Save(&localArticle).Error
|
||||
if err != nil {
|
||||
return domainArticle.Article{}, err
|
||||
return domain.Article{}, err
|
||||
}
|
||||
|
||||
return mappers.ArticleToDomain(localArticle), nil
|
||||
@@ -125,4 +143,4 @@ func (a *ArticleRepository) AddImage(articleId uint32, imageId uint32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ domainArticle.Repository = &ArticleRepository{}
|
||||
var _ domain.Repository = &ArticleRepository{}
|
||||
|
||||
@@ -10,8 +10,21 @@ func NewGetArticleUseCase(articleRepository article.Repository) *GetArticleUseCa
|
||||
return &GetArticleUseCase{articleRepository}
|
||||
}
|
||||
|
||||
func (g *GetArticleUseCase) GetArticle(id uint32) (article.Article, error) {
|
||||
return g.articleRepository.Get(id)
|
||||
func (u *GetArticleUseCase) GetOnlineArticle(id uint32) (article.Article, error) {
|
||||
art, err := u.articleRepository.Get(id)
|
||||
if err != nil {
|
||||
return article.Article{}, err
|
||||
}
|
||||
|
||||
if !art.IsOnline {
|
||||
return article.Article{}, article.ErrArticleNotFound
|
||||
}
|
||||
|
||||
return art, nil
|
||||
}
|
||||
|
||||
func (u *GetArticleUseCase) GetArticle(id uint32) (article.Article, error) {
|
||||
return u.articleRepository.Get(id)
|
||||
}
|
||||
|
||||
func (g *GetArticleUseCase) GetArticleByName(name string) (article.Article, error) {
|
||||
|
||||
@@ -10,6 +10,13 @@ func NewListArticlesUseCase(articleRepository article.Repository) *ListArticlesU
|
||||
return &ListArticlesUseCase{articleRepository}
|
||||
}
|
||||
|
||||
func (g *ListArticlesUseCase) ListArticles() []article.Article {
|
||||
return g.articleRepository.GetAll()
|
||||
func (u *ListArticlesUseCase) ListOnlineArticles() ([]article.Article, error) {
|
||||
online := true
|
||||
return u.articleRepository.FindByFilters(article.Filters{
|
||||
IsOnline: &online,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ListArticlesUseCase) ListAllArticles() ([]article.Article, error) {
|
||||
return u.articleRepository.FindByFilters(article.Filters{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user