mirror of
https://github.com/miniflux/v2.git
synced 2025-08-26 18:21:01 +00:00
Improve API
This commit is contained in:
parent
747da03e4c
commit
71bf7e4358
15 changed files with 40 additions and 63 deletions
|
@ -6,6 +6,7 @@ package api
|
|||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/miniflux/miniflux2/model"
|
||||
"github.com/miniflux/miniflux2/server/api/payload"
|
||||
"github.com/miniflux/miniflux2/server/core"
|
||||
|
@ -99,23 +100,11 @@ func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, re
|
|||
response.JSON().Standard(&payload.EntriesResponse{Total: count, Entries: entries})
|
||||
}
|
||||
|
||||
// SetEntryStatus is the API handler to change the status of an entry.
|
||||
// SetEntryStatus is the API handler to change the status of entries.
|
||||
func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, response *core.Response) {
|
||||
userID := ctx.UserID()
|
||||
|
||||
feedID, err := request.IntegerParam("feedID")
|
||||
if err != nil {
|
||||
response.JSON().BadRequest(err)
|
||||
return
|
||||
}
|
||||
|
||||
entryID, err := request.IntegerParam("entryID")
|
||||
if err != nil {
|
||||
response.JSON().BadRequest(err)
|
||||
return
|
||||
}
|
||||
|
||||
status, err := payload.DecodeEntryStatusPayload(request.Body())
|
||||
entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body())
|
||||
if err != nil {
|
||||
response.JSON().BadRequest(errors.New("Invalid JSON payload"))
|
||||
return
|
||||
|
@ -126,31 +115,10 @@ func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, re
|
|||
return
|
||||
}
|
||||
|
||||
builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
|
||||
builder.WithFeedID(feedID)
|
||||
builder.WithEntryID(entryID)
|
||||
|
||||
entry, err := builder.GetEntry()
|
||||
if err != nil {
|
||||
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
|
||||
if err := c.store.SetEntriesStatus(userID, entryIDs, status); err != nil {
|
||||
response.JSON().ServerError(errors.New("Unable to change entries status"))
|
||||
return
|
||||
}
|
||||
|
||||
if entry == nil {
|
||||
response.JSON().NotFound(errors.New("Entry not found"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.store.SetEntriesStatus(userID, []int64{entry.ID}, status); err != nil {
|
||||
response.JSON().ServerError(errors.New("Unable to change entry status"))
|
||||
return
|
||||
}
|
||||
|
||||
entry, err = builder.GetEntry()
|
||||
if err != nil {
|
||||
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
|
||||
return
|
||||
}
|
||||
|
||||
response.JSON().Standard(entry)
|
||||
response.JSON().NoContent()
|
||||
}
|
||||
|
|
|
@ -7,8 +7,9 @@ package payload
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/miniflux/miniflux2/model"
|
||||
"io"
|
||||
|
||||
"github.com/miniflux/miniflux2/model"
|
||||
)
|
||||
|
||||
type EntriesResponse struct {
|
||||
|
@ -41,18 +42,19 @@ func DecodeURLPayload(data io.Reader) (string, error) {
|
|||
return p.URL, nil
|
||||
}
|
||||
|
||||
func DecodeEntryStatusPayload(data io.Reader) (string, error) {
|
||||
func DecodeEntryStatusPayload(data io.Reader) ([]int64, string, error) {
|
||||
type payload struct {
|
||||
Status string `json:"status"`
|
||||
EntryIDs []int64 `json:"entry_ids"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
var p payload
|
||||
decoder := json.NewDecoder(data)
|
||||
if err := decoder.Decode(&p); err != nil {
|
||||
return "", fmt.Errorf("invalid JSON payload: %v", err)
|
||||
return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
|
||||
}
|
||||
|
||||
return p.Status, nil
|
||||
return p.EntryIDs, p.Status, nil
|
||||
}
|
||||
|
||||
func DecodeFeedCreationPayload(data io.Reader) (string, int64, error) {
|
||||
|
|
|
@ -62,7 +62,7 @@ func getRoutes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Han
|
|||
|
||||
router.Handle("/v1/feeds/{feedID}/entries", apiHandler.Use(apiController.GetFeedEntries)).Methods("GET")
|
||||
router.Handle("/v1/feeds/{feedID}/entries/{entryID}", apiHandler.Use(apiController.GetEntry)).Methods("GET")
|
||||
router.Handle("/v1/feeds/{feedID}/entries/{entryID}", apiHandler.Use(apiController.SetEntryStatus)).Methods("PUT")
|
||||
router.Handle("/v1/entries", apiHandler.Use(apiController.SetEntryStatus)).Methods("PUT")
|
||||
|
||||
router.Handle("/stylesheets/{name}.css", uiHandler.Use(uiController.Stylesheet)).Name("stylesheet").Methods("GET")
|
||||
router.Handle("/js", uiHandler.Use(uiController.Javascript)).Name("javascript").Methods("GET")
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2017-11-24 16:04:49.314940117 -0800 PST m=+0.003107235
|
||||
// 2017-11-24 21:40:33.326996526 -0800 PST m=+0.007911995
|
||||
|
||||
package static
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2017-11-24 16:04:49.315340301 -0800 PST m=+0.003507419
|
||||
// 2017-11-24 21:40:33.330122316 -0800 PST m=+0.011037785
|
||||
|
||||
package static
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2017-11-24 16:04:49.316027642 -0800 PST m=+0.004194760
|
||||
// 2017-11-24 21:40:33.333049571 -0800 PST m=+0.013965040
|
||||
|
||||
package static
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2017-11-24 16:04:49.318279667 -0800 PST m=+0.006446785
|
||||
// 2017-11-24 21:40:33.353262943 -0800 PST m=+0.034178412
|
||||
|
||||
package template
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2017-11-24 16:04:49.316644027 -0800 PST m=+0.004811145
|
||||
// 2017-11-24 21:40:33.335450873 -0800 PST m=+0.016366342
|
||||
|
||||
package template
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue