miniflux-v2/api/category.go

93 lines
2.0 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/request"
"miniflux.app/http/response/json"
2017-11-20 06:10:04 +01:00
)
func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
category, err := decodeCategoryPayload(r.Body)
2017-11-20 06:10:04 +01:00
if err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2018-09-03 23:26:40 +02:00
userID := request.UserID(r)
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 {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-12-25 03:04:34 +01:00
return
}
if c, err := h.store.CategoryByTitle(userID, category.Title); err != nil || c != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, errors.New("This category already exists"))
2017-11-20 06:10:04 +01:00
return
}
if err := h.store.CreateCategory(category); err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2018-10-08 03:42:43 +02:00
json.Created(w, r, category)
2017-11-20 06:10:04 +01:00
}
func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
categoryID := request.RouteInt64Param(r, "categoryID")
2017-11-20 06:10:04 +01:00
category, err := decodeCategoryPayload(r.Body)
2017-11-20 06:10:04 +01:00
if err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2018-09-03 23:26:40 +02:00
category.UserID = request.UserID(r)
2017-11-20 06:10:04 +01:00
category.ID = categoryID
if err := category.ValidateCategoryModification(); err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
err = h.store.UpdateCategory(category)
2017-11-20 06:10:04 +01:00
if err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2018-10-08 03:42:43 +02:00
json.Created(w, r, category)
2017-11-20 06:10:04 +01:00
}
func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) {
categories, err := h.store.Categories(request.UserID(r))
2017-11-20 06:10:04 +01:00
if err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
json.OK(w, r, categories)
2017-11-20 06:10:04 +01:00
}
func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) {
2018-09-03 23:26:40 +02:00
userID := request.UserID(r)
categoryID := request.RouteInt64Param(r, "categoryID")
2017-11-20 06:10:04 +01:00
if !h.store.CategoryExists(userID, categoryID) {
2018-10-08 03:42:43 +02:00
json.NotFound(w, r)
2017-11-20 06:10:04 +01:00
return
}
if err := h.store.RemoveCategory(userID, categoryID); err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2018-10-08 03:42:43 +02:00
json.NoContent(w, r)
2017-11-20 06:10:04 +01:00
}