miniflux/api/category.go

111 lines
2.5 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.
2018-08-25 06:51:50 +02:00
package api // import "miniflux.app/api"
2017-11-20 06:10:04 +01:00
import (
"errors"
"net/http"
2017-11-28 06:30:04 +01:00
2018-08-25 06:51:50 +02:00
"miniflux.app/http/context"
"miniflux.app/http/request"
"miniflux.app/http/response/json"
2017-11-20 06:10:04 +01:00
)
// CreateCategory is the API handler to create a new category.
func (c *Controller) CreateCategory(w http.ResponseWriter, r *http.Request) {
category, err := decodeCategoryPayload(r.Body)
2017-11-20 06:10:04 +01:00
if err != nil {
json.BadRequest(w, err)
2017-11-20 06:10:04 +01:00
return
}
ctx := context.New(r)
userID := ctx.UserID()
2017-12-25 03:04:34 +01:00
category.UserID = userID
2017-11-20 06:10:04 +01:00
if err := category.ValidateCategoryCreation(); err != nil {
json.BadRequest(w, err)
2017-12-25 03:04:34 +01:00
return
}
if c, err := c.store.CategoryByTitle(userID, category.Title); err != nil || c != nil {
json.BadRequest(w, errors.New("This category already exists"))
2017-11-20 06:10:04 +01:00
return
}
err = c.store.CreateCategory(category)
if err != nil {
json.ServerError(w, err)
2017-11-20 06:10:04 +01:00
return
}
json.Created(w, category)
2017-11-20 06:10:04 +01:00
}
// UpdateCategory is the API handler to update a category.
func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) {
categoryID, err := request.IntParam(r, "categoryID")
2017-11-20 06:10:04 +01:00
if err != nil {
json.BadRequest(w, err)
2017-11-20 06:10:04 +01:00
return
}
category, err := decodeCategoryPayload(r.Body)
2017-11-20 06:10:04 +01:00
if err != nil {
json.BadRequest(w, err)
2017-11-20 06:10:04 +01:00
return
}
ctx := context.New(r)
2017-11-22 03:37:08 +01:00
category.UserID = ctx.UserID()
2017-11-20 06:10:04 +01:00
category.ID = categoryID
if err := category.ValidateCategoryModification(); err != nil {
json.BadRequest(w, err)
2017-11-20 06:10:04 +01:00
return
}
err = c.store.UpdateCategory(category)
if err != nil {
json.ServerError(w, err)
2017-11-20 06:10:04 +01:00
return
}
json.Created(w, category)
2017-11-20 06:10:04 +01:00
}
// GetCategories is the API handler to get a list of categories for a given user.
func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) {
ctx := context.New(r)
2017-11-28 06:30:04 +01:00
categories, err := c.store.Categories(ctx.UserID())
2017-11-20 06:10:04 +01:00
if err != nil {
json.ServerError(w, err)
2017-11-20 06:10:04 +01:00
return
}
json.OK(w, r, categories)
2017-11-20 06:10:04 +01:00
}
// RemoveCategory is the API handler to remove a category.
func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) {
ctx := context.New(r)
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
categoryID, err := request.IntParam(r, "categoryID")
2017-11-20 06:10:04 +01:00
if err != nil {
json.BadRequest(w, err)
2017-11-20 06:10:04 +01:00
return
}
if !c.store.CategoryExists(userID, categoryID) {
json.NotFound(w, errors.New("Category not found"))
2017-11-20 06:10:04 +01:00
return
}
if err := c.store.RemoveCategory(userID, categoryID); err != nil {
json.ServerError(w, err)
2017-11-20 06:10:04 +01:00
return
}
json.NoContent(w)
2017-11-20 06:10:04 +01:00
}