From 55fad7ea275fc04bde3db6de8a502ab32a499a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 13 Sep 2020 21:38:11 -0700 Subject: [PATCH] Avoid duplication between get feed entries and get entries API endpoints --- api/entry.go | 62 +++++++--------------------------- storage/entry_query_builder.go | 2 +- 2 files changed, 13 insertions(+), 51 deletions(-) diff --git a/api/entry.go b/api/entry.go index 4ceeadca..8eadb8a8 100644 --- a/api/entry.go +++ b/api/entry.go @@ -58,59 +58,14 @@ func (h *handler) getEntry(w http.ResponseWriter, r *http.Request) { func (h *handler) getFeedEntries(w http.ResponseWriter, r *http.Request) { feedID := request.RouteInt64Param(r, "feedID") - - status := request.QueryStringParam(r, "status", "") - if status != "" { - if err := model.ValidateEntryStatus(status); err != nil { - json.BadRequest(w, r, err) - return - } - } - - order := request.QueryStringParam(r, "order", model.DefaultSortingOrder) - if err := model.ValidateEntryOrder(order); err != nil { - json.BadRequest(w, r, err) - return - } - - direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection) - if err := model.ValidateDirection(direction); err != nil { - json.BadRequest(w, r, err) - return - } - - limit := request.QueryIntParam(r, "limit", 100) - offset := request.QueryIntParam(r, "offset", 0) - if err := model.ValidateRange(offset, limit); err != nil { - json.BadRequest(w, r, err) - return - } - - builder := h.store.NewEntryQueryBuilder(request.UserID(r)) - builder.WithFeedID(feedID) - builder.WithStatus(status) - builder.WithOrder(order) - builder.WithDirection(direction) - builder.WithOffset(offset) - builder.WithLimit(limit) - configureFilters(builder, r) - - entries, err := builder.GetEntries() - if err != nil { - json.ServerError(w, r, err) - return - } - - count, err := builder.CountEntries() - if err != nil { - json.ServerError(w, r, err) - return - } - - json.OK(w, r, &entriesResponse{Total: count, Entries: entries}) + h.findEntries(w, r, feedID) } func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) { + h.findEntries(w, r, 0) +} + +func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int64) { statuses := request.QueryStringParamList(r, "status") for _, status := range statuses { if err := model.ValidateEntryStatus(status); err != nil { @@ -145,7 +100,14 @@ func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) { return } + feedID = request.QueryInt64Param(r, "feed_id", feedID) + if feedID > 0 && !h.store.FeedExists(userID, feedID) { + json.BadRequest(w, r, errors.New("Invalid feed ID")) + return + } + builder := h.store.NewEntryQueryBuilder(userID) + builder.WithFeedID(feedID) builder.WithCategoryID(categoryID) builder.WithStatuses(statuses) builder.WithOrder(order) diff --git a/storage/entry_query_builder.go b/storage/entry_query_builder.go index c1f1defd..d7ab042f 100644 --- a/storage/entry_query_builder.go +++ b/storage/entry_query_builder.go @@ -96,7 +96,7 @@ func (e *EntryQueryBuilder) WithEntryID(entryID int64) *EntryQueryBuilder { // WithFeedID filter by feed ID. func (e *EntryQueryBuilder) WithFeedID(feedID int64) *EntryQueryBuilder { - if feedID != 0 { + if feedID > 0 { e.conditions = append(e.conditions, fmt.Sprintf("e.feed_id = $%d", len(e.args)+1)) e.args = append(e.args, feedID) }