Skip to content

Commit

Permalink
fix(article): improve article creation error mgt
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Apr 25, 2019
1 parent f99a2fb commit 085b65a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
8 changes: 2 additions & 6 deletions pkg/api/articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ func articles(conf *config.Config) http.Handler {
return
}

articles, err := service.Lookup().CreateArticles(ctx, articlesForm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
articles := service.Lookup().CreateArticles(ctx, articlesForm)

// TODO filters some attributes

Expand All @@ -35,7 +31,7 @@ func articles(conf *config.Config) http.Handler {
return
}
w.Header().Set("Content-Type", "application/json")
status := http.StatusAccepted
status := http.StatusNoContent
if len(articles.Errors) == 0 && len(articles.Articles) > 0 {
status = http.StatusCreated
} else if len(articles.Errors) > 0 {
Expand Down
9 changes: 2 additions & 7 deletions pkg/service/articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"context"
"errors"
"fmt"
"time"

"github.com/ncarlier/readflow/pkg/tooling"
Expand Down Expand Up @@ -82,7 +81,7 @@ func (reg *Registry) CreateArticle(ctx context.Context, data model.ArticleForm,
}

// CreateArticles creates new articles
func (reg *Registry) CreateArticles(ctx context.Context, data []model.ArticleForm) (*model.Articles, error) {
func (reg *Registry) CreateArticles(ctx context.Context, data []model.ArticleForm) *model.Articles {
result := model.Articles{}
for _, art := range data {
article, err := reg.CreateArticle(ctx, art, ArticleCreationOptions{
Expand All @@ -95,11 +94,7 @@ func (reg *Registry) CreateArticles(ctx context.Context, data []model.ArticleFor
result.Articles = append(result.Articles, article)
}
}
var err error
if len(result.Errors) > 0 {
err = fmt.Errorf("Errors when creating articles")
}
return &result, err
return &result
}

// CountArticles count articles
Expand Down

0 comments on commit 085b65a

Please sign in to comment.