mirror of
https://github.com/miniflux/v2.git
synced 2025-08-06 17:41:00 +00:00
Refactor HTTP response builder
This commit is contained in:
parent
ddfe969d6c
commit
1f58b37a5e
94 changed files with 1701 additions and 644 deletions
46
api/feed.go
46
api/feed.go
|
@ -16,29 +16,29 @@ import (
|
|||
func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
|
||||
feedInfo, err := decodeFeedCreationPayload(r.Body)
|
||||
if err != nil {
|
||||
json.BadRequest(w, err)
|
||||
json.BadRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if feedInfo.FeedURL == "" {
|
||||
json.BadRequest(w, errors.New("The feed_url is required"))
|
||||
json.BadRequest(w, r, errors.New("The feed_url is required"))
|
||||
return
|
||||
}
|
||||
|
||||
if feedInfo.CategoryID <= 0 {
|
||||
json.BadRequest(w, errors.New("The category_id is required"))
|
||||
json.BadRequest(w, r, errors.New("The category_id is required"))
|
||||
return
|
||||
}
|
||||
|
||||
userID := request.UserID(r)
|
||||
|
||||
if c.store.FeedURLExists(userID, feedInfo.FeedURL) {
|
||||
json.BadRequest(w, errors.New("This feed_url already exists"))
|
||||
json.BadRequest(w, r, errors.New("This feed_url already exists"))
|
||||
return
|
||||
}
|
||||
|
||||
if !c.store.CategoryExists(userID, feedInfo.CategoryID) {
|
||||
json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
|
||||
json.BadRequest(w, r, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
|
|||
feedInfo.Password,
|
||||
)
|
||||
if err != nil {
|
||||
json.ServerError(w, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
|
|||
FeedID int64 `json:"feed_id"`
|
||||
}
|
||||
|
||||
json.Created(w, &result{FeedID: feed.ID})
|
||||
json.Created(w, r, &result{FeedID: feed.ID})
|
||||
}
|
||||
|
||||
// RefreshFeed is the API handler to refresh a feed.
|
||||
|
@ -69,17 +69,17 @@ func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) {
|
|||
userID := request.UserID(r)
|
||||
|
||||
if !c.store.FeedExists(userID, feedID) {
|
||||
json.NotFound(w, errors.New("Unable to find this feed"))
|
||||
json.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err := c.feedHandler.RefreshFeed(userID, feedID)
|
||||
if err != nil {
|
||||
json.ServerError(w, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
json.NoContent(w)
|
||||
json.NoContent(w, r)
|
||||
}
|
||||
|
||||
// UpdateFeed is the API handler that is used to update a feed.
|
||||
|
@ -87,7 +87,7 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
|
|||
feedID := request.RouteInt64Param(r, "feedID")
|
||||
feedChanges, err := decodeFeedModificationPayload(r.Body)
|
||||
if err != nil {
|
||||
json.BadRequest(w, err)
|
||||
json.BadRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -95,41 +95,41 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
originalFeed, err := c.store.FeedByID(userID, feedID)
|
||||
if err != nil {
|
||||
json.NotFound(w, errors.New("Unable to find this feed"))
|
||||
json.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if originalFeed == nil {
|
||||
json.NotFound(w, errors.New("Feed not found"))
|
||||
json.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
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"))
|
||||
json.BadRequest(w, r, 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, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
originalFeed, err = c.store.FeedByID(userID, feedID)
|
||||
if err != nil {
|
||||
json.ServerError(w, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
json.Created(w, originalFeed)
|
||||
json.Created(w, r, originalFeed)
|
||||
}
|
||||
|
||||
// GetFeeds is the API handler that get all feeds that belongs to the given user.
|
||||
func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) {
|
||||
feeds, err := c.store.Feeds(request.UserID(r))
|
||||
if err != nil {
|
||||
json.ServerError(w, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -141,12 +141,12 @@ func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) {
|
|||
feedID := request.RouteInt64Param(r, "feedID")
|
||||
feed, err := c.store.FeedByID(request.UserID(r), feedID)
|
||||
if err != nil {
|
||||
json.ServerError(w, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if feed == nil {
|
||||
json.NotFound(w, errors.New("Feed not found"))
|
||||
json.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -159,14 +159,14 @@ func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) {
|
|||
userID := request.UserID(r)
|
||||
|
||||
if !c.store.FeedExists(userID, feedID) {
|
||||
json.NotFound(w, errors.New("Feed not found"))
|
||||
json.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.store.RemoveFeed(userID, feedID); err != nil {
|
||||
json.ServerError(w, err)
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
json.NoContent(w)
|
||||
json.NoContent(w, r)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue