Skip to content

Commit

Permalink
Merge pull request #21 from RECYTHNG/develop
Browse files Browse the repository at this point in the history
fix(article): update response for invalid category
  • Loading branch information
sawalreverr authored Jun 21, 2024
2 parents b595f97 + 786923b commit 83e12cd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
1 change: 1 addition & 0 deletions internal/article/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type ArticleUsecase interface {

// Waste Category & Content Category
GetAllCategories() (*CategoriesResponse, error)
CategoryValidation(wasteCategories []string, contentCategories []string) ([]uint, []uint, error)
}

type ArticleHandler interface {
Expand Down
5 changes: 5 additions & 0 deletions internal/article/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (h *articleHandler) NewArticle(c echo.Context) error {

response, err := h.usecase.NewArticle(request, authorID)
if err != nil {
if errors.Is(pkg.ErrCategoryArticleNotFound, err) {
return helper.ErrorHandler(c, http.StatusBadRequest, err.Error())
}
return helper.ErrorHandler(c, http.StatusInternalServerError, err.Error())
}

Expand All @@ -56,6 +59,8 @@ func (h *articleHandler) UpdateArticle(c echo.Context) error {
if err := h.usecase.Update(articleID, request); err != nil {
if errors.Is(pkg.ErrArticleNotFound, err) {
return helper.ErrorHandler(c, http.StatusNotFound, err.Error())
} else if errors.Is(pkg.ErrCategoryArticleNotFound, err) {
return helper.ErrorHandler(c, http.StatusBadRequest, err.Error())
}

return helper.ErrorHandler(c, http.StatusInternalServerError, err.Error())
Expand Down
106 changes: 58 additions & 48 deletions internal/article/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,20 @@ func (u *articleUsecase) NewArticle(article art.ArticleInput, authorId string) (
AuthorID: authorId,
}

createdArticle, err := u.articleRepo.Create(newArticle)
wasteIDs, contentIDs, err := u.CategoryValidation(article.WasteCategories, article.ContentCategories)
if err != nil {
return nil, err
}

for _, section := range article.Sections {
section.ArticleID = createdArticle.ID
if err := u.articleRepo.CreateSection(section); err != nil {
_ = u.articleRepo.Delete(createdArticle.ID)
return nil, err
}
createdArticle, err := u.articleRepo.Create(newArticle)
if err != nil {
return nil, err
}

for _, categoryName := range article.WasteCategories {
categoryID, err := u.articleRepo.FindCategoryByName(categoryName, "waste")
if err != nil {
_ = u.articleRepo.Delete(createdArticle.ID)
return nil, err
}

for i := range article.WasteCategories {
articleCategory := art.ArticleCategories{
ArticleID: createdArticle.ID,
WasteCategoryID: categoryID,
WasteCategoryID: wasteIDs[i],
}

if err := u.articleRepo.CreateArticleCategory(articleCategory); err != nil {
Expand All @@ -63,16 +54,10 @@ func (u *articleUsecase) NewArticle(article art.ArticleInput, authorId string) (
}
}

for _, categoryName := range article.ContentCategories {
categoryID, err := u.articleRepo.FindCategoryByName(categoryName, "content")
if err != nil {
_ = u.articleRepo.Delete(createdArticle.ID)
return nil, err
}

for i := range article.ContentCategories {
articleCategory := art.ArticleCategories{
ArticleID: createdArticle.ID,
ContentCategoryID: int(categoryID),
ContentCategoryID: int(contentIDs[i]),
}

if err := u.articleRepo.CreateArticleCategory(articleCategory); err != nil {
Expand All @@ -81,6 +66,14 @@ func (u *articleUsecase) NewArticle(article art.ArticleInput, authorId string) (
}
}

for _, section := range article.Sections {
section.ArticleID = createdArticle.ID
if err := u.articleRepo.CreateSection(section); err != nil {
_ = u.articleRepo.Delete(createdArticle.ID)
return nil, err
}
}

articleFound, _ := u.articleRepo.FindByID(createdArticle.ID)
return u.GetArticleDetail(*articleFound), nil
}
Expand Down Expand Up @@ -147,6 +140,11 @@ func (u *articleUsecase) Update(articleID string, article art.ArticleInput) erro
return err
}

wasteIDs, contentIDs, err := u.CategoryValidation(article.WasteCategories, article.ContentCategories)
if err != nil {
return err
}

articleToUpdate := art.Article{
ID: articleFound.ID,
Title: article.Title,
Expand All @@ -157,53 +155,43 @@ func (u *articleUsecase) Update(articleID string, article art.ArticleInput) erro
UpdatedAt: time.Now(),
}

if err := u.articleRepo.DeleteAllSection(articleID); err != nil {
return err
}

for _, section := range article.Sections {
section.ArticleID = articleID
if err := u.articleRepo.CreateSection(section); err != nil {
return err
}
}

if err := u.articleRepo.DeleteAllArticleCategory(articleID); err != nil {
return err
}

for _, wasteCategoryName := range article.WasteCategories {
wasteCategoryID, err := u.articleRepo.FindCategoryByName(wasteCategoryName, "waste")
if err != nil {
return err
}

for i := range article.WasteCategories {
articleCategory := art.ArticleCategories{
ArticleID: articleID,
WasteCategoryID: wasteCategoryID,
WasteCategoryID: wasteIDs[i],
}

if err := u.articleRepo.CreateArticleCategory(articleCategory); err != nil {
return err
}
}

for _, contentCategoryName := range article.ContentCategories {
contentCategoryID, err := u.articleRepo.FindCategoryByName(contentCategoryName, "content")
if err != nil {
return err
}

for i := range article.ContentCategories {
articleCategory := art.ArticleCategories{
ArticleID: articleID,
ContentCategoryID: int(contentCategoryID),
ContentCategoryID: int(contentIDs[i]),
}

if err := u.articleRepo.CreateArticleCategory(articleCategory); err != nil {
return err
}
}

if err := u.articleRepo.DeleteAllSection(articleID); err != nil {
return err
}

for _, section := range article.Sections {
section.ArticleID = articleID
if err := u.articleRepo.CreateSection(section); err != nil {
return err
}
}

return u.articleRepo.Update(articleToUpdate)
}

Expand Down Expand Up @@ -336,3 +324,25 @@ func (uc *articleUsecase) GetAllCategories() (*art.CategoriesResponse, error) {
ContentCategories: *contentCategories,
}, nil
}

func (uc *articleUsecase) CategoryValidation(wasteCategories []string, contentCategories []string) ([]uint, []uint, error) {
var wasteCategoriesID []uint
for _, wasteCategory := range wasteCategories {
categoryID, err := uc.articleRepo.FindCategoryByName(wasteCategory, "waste")
if err != nil {
return nil, nil, pkg.ErrCategoryArticleNotFound
}
wasteCategoriesID = append(wasteCategoriesID, categoryID)
}

var contentCategoriesID []uint
for _, contentCategory := range contentCategories {
categoryID, err := uc.articleRepo.FindCategoryByName(contentCategory, "content")
if err != nil {
return nil, nil, pkg.ErrCategoryArticleNotFound
}
contentCategoriesID = append(contentCategoriesID, categoryID)
}

return wasteCategoriesID, contentCategoriesID, nil
}

0 comments on commit 83e12cd

Please sign in to comment.