miniflux/server/api/controller/entry.go

157 lines
4.1 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/model"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// GetEntry is the API handler to get a single feed entry.
func (c *Controller) GetEntry(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
entryID, err := request.IntegerParam("entryID")
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:37:08 +01:00
builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
2017-11-20 06:10:04 +01:00
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
2017-11-20 06:10:04 +01:00
return
}
if entry == nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Entry not found"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(entry)
2017-11-20 06:10:04 +01:00
}
// GetFeedEntries is the API handler to get all feed entries.
func (c *Controller) GetFeedEntries(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
status := request.QueryStringParam("status", "")
2017-11-20 06:10:04 +01:00
if status != "" {
if err := model.ValidateEntryStatus(status); 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
order := request.QueryStringParam("order", "id")
2017-11-20 06:10:04 +01:00
if err := model.ValidateEntryOrder(order); 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
direction := request.QueryStringParam("direction", "desc")
2017-11-20 06:10:04 +01:00
if err := model.ValidateDirection(direction); 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
limit := request.QueryIntegerParam("limit", 100)
offset := request.QueryIntegerParam("offset", 0)
2017-11-20 06:10:04 +01:00
2017-11-22 03:37:08 +01:00
builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
2017-11-20 06:10:04 +01:00
builder.WithFeedID(feedID)
builder.WithStatus(status)
builder.WithOrder(model.DefaultSortingOrder)
builder.WithDirection(model.DefaultSortingDirection)
builder.WithOffset(offset)
builder.WithLimit(limit)
entries, err := builder.GetEntries()
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch the list of entries"))
2017-11-20 06:10:04 +01:00
return
}
count, err := builder.CountEntries()
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to count the number of entries"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(&payload.EntriesResponse{Total: count, Entries: entries})
2017-11-20 06:10:04 +01:00
}
// SetEntryStatus is the API handler to change the status of an entry.
func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-20 06:10:04 +01:00
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
entryID, err := request.IntegerParam("entryID")
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
status, err := payload.DecodeEntryStatusPayload(request.Body())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(errors.New("Invalid JSON payload"))
2017-11-20 06:10:04 +01:00
return
}
if err := model.ValidateEntryStatus(status); 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:37:08 +01:00
builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
2017-11-20 06:10:04 +01:00
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
2017-11-20 06:10:04 +01:00
return
}
if entry == nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Entry not found"))
2017-11-20 06:10:04 +01:00
return
}
if err := c.store.SetEntriesStatus(userID, []int64{entry.ID}, status); err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to change entry status"))
2017-11-20 06:10:04 +01:00
return
}
entry, err = builder.GetEntry()
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(entry)
2017-11-20 06:10:04 +01:00
}