miniflux-v2/server/api/controller/feed.go

139 lines
3.6 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"errors"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// CreateFeed is the API handler to create a new feed.
func (c *Controller) CreateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedURL, categoryID, err := payload.DecodeFeedCreationPayload(request.Body())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL)
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to create this feed"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Created(feed)
2017-11-20 06:10:04 +01:00
}
// RefreshFeed is the API handler to refresh a feed.
func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
err = c.feedHandler.RefreshFeed(userID, feedID)
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to refresh this feed"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().NoContent()
2017-11-20 06:10:04 +01:00
}
// UpdateFeed is the API handler that is used to update a feed.
func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:14:45 +01:00
newFeed, err := payload.DecodeFeedModificationPayload(request.Body())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
originalFeed, err := c.store.GetFeedById(userID, feedID)
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Unable to find this feed"))
2017-11-20 06:10:04 +01:00
return
}
if originalFeed == nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Feed not found"))
2017-11-20 06:10:04 +01:00
return
}
originalFeed.Merge(newFeed)
if err := c.store.UpdateFeed(originalFeed); err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to update this feed"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Created(originalFeed)
2017-11-20 06:10:04 +01:00
}
// GetFeeds is the API handler that get all feeds that belongs to the given user.
func (c *Controller) GetFeeds(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
feeds, err := c.store.GetFeeds(ctx.UserID())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch feeds from the database"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(feeds)
2017-11-20 06:10:04 +01:00
}
// GetFeed is the API handler to get a feed.
func (c *Controller) GetFeed(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
feed, err := c.store.GetFeedById(userID, feedID)
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch this feed"))
2017-11-20 06:10:04 +01:00
return
}
if feed == nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Feed not found"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(feed)
2017-11-20 06:10:04 +01:00
}
// RemoveFeed is the API handler to remove a feed.
func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
if !c.store.FeedExists(userID, feedID) {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Feed not found"))
2017-11-20 06:10:04 +01:00
return
}
if err := c.store.RemoveFeed(userID, feedID); err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to remove this feed"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().NoContent()
2017-11-20 06:10:04 +01:00
}