From 7039df9af1de1aea72e90d4aa9fa6a37d21e1be0 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 23 Jun 2018 16:16:54 -0700 Subject: Improve feed and user API updates with optional values --- api/feed.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'api/feed.go') diff --git a/api/feed.go b/api/feed.go index 351457f..b7c46ea 100644 --- a/api/feed.go +++ b/api/feed.go @@ -97,7 +97,7 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { return } - newFeed, err := decodeFeedModificationPayload(r.Body) + feedChanges, err := decodeFeedModificationPayload(r.Body) if err != nil { json.BadRequest(w, err) return @@ -106,11 +106,6 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { ctx := context.New(r) userID := ctx.UserID() - if newFeed.Category != nil && newFeed.Category.ID != 0 && !c.store.CategoryExists(userID, newFeed.Category.ID) { - json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user")) - return - } - originalFeed, err := c.store.FeedByID(userID, feedID) if err != nil { json.NotFound(w, errors.New("Unable to find this feed")) @@ -122,7 +117,13 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { return } - originalFeed.Merge(newFeed) + feedChanges.Update(originalFeed) + + if !c.store.CategoryExists(userID, originalFeed.Category.ID) { + json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user")) + return + } + if err := c.store.UpdateFeed(originalFeed); err != nil { json.ServerError(w, errors.New("Unable to update this feed")) return -- cgit v1.2.3