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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateCategory is the API handler to create a new category.
|
|
|
|
func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-22 03:14:45 +01:00
|
|
|
category, err := payload.DecodeCategoryPayload(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
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
category.UserID = ctx.UserID()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := category.ValidateCategoryCreation(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.store.CreateCategory(category)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to create this category"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Created(category)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCategory is the API handler to update a category.
|
|
|
|
func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-22 03:14:45 +01:00
|
|
|
categoryID, err := request.IntegerParam("categoryID")
|
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
|
|
|
category, err := payload.DecodeCategoryPayload(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
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.store.UpdateCategory(category)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to update this category"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Created(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(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
categories, err := c.store.GetCategories(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 categories"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Standard(categories)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveCategory is the API handler to remove a category.
|
|
|
|
func (c *Controller) RemoveCategory(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
|
|
|
categoryID, err := request.IntegerParam("categoryID")
|
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.CategoryExists(userID, categoryID) {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().NotFound(errors.New("Category not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.store.RemoveCategory(userID, categoryID); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to remove this category"))
|
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
|
|
|
}
|