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"
|
2017-11-28 06:30:04 +01:00
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/http/handler"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateCategory is the API handler to create a new category.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) CreateCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-12-25 03:04:34 +01:00
|
|
|
userID := ctx.UserID()
|
2018-01-03 07:04:48 +01:00
|
|
|
category, err := 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-12-25 03:04:34 +01:00
|
|
|
category.UserID = userID
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := category.ValidateCategoryCreation(); err != nil {
|
2017-12-25 03:04:34 +01:00
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c, err := c.store.CategoryByTitle(userID, category.Title); err != nil || c != nil {
|
|
|
|
response.JSON().BadRequest(errors.New("This category already exists"))
|
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.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) UpdateCategory(ctx *handler.Context, request *handler.Request, response *handler.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
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
category, err := 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.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) GetCategories(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
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 {
|
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.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) RemoveCategory(ctx *handler.Context, request *handler.Request, response *handler.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
|
|
|
}
|