1
0
Fork 0
miniflux/api/category.go

126 lines
2.9 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05: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 00:51:50 -04:00
package api // import "miniflux.app/api"
2017-11-20 00:10:04 -05:00
import (
2021-01-04 01:28:04 -05:00
json_parser "encoding/json"
"net/http"
2020-11-25 14:46:05 -05:00
"time"
2017-11-28 00:30:04 -05:00
2018-08-25 00:51:50 -04:00
"miniflux.app/http/request"
"miniflux.app/http/response/json"
"miniflux.app/model"
2021-01-04 01:28:04 -05:00
"miniflux.app/validator"
2017-11-20 00:10:04 -05:00
)
func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
userID := request.UserID(r)
2021-01-04 01:28:04 -05:00
var categoryRequest model.CategoryRequest
if err := json_parser.NewDecoder(r.Body).Decode(&categoryRequest); err != nil {
2018-10-07 21:42:43 -04:00
json.BadRequest(w, r, err)
2017-12-24 21:04:34 -05:00
return
}
2021-01-04 01:28:04 -05:00
if validationErr := validator.ValidateCategoryCreation(h.store, userID, &categoryRequest); validationErr != nil {
json.BadRequest(w, r, validationErr.Error())
2017-11-20 00:10:04 -05:00
return
}
2021-01-04 01:28:04 -05:00
category, err := h.store.CreateCategory(userID, &categoryRequest)
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2018-10-07 21:42:43 -04:00
json.Created(w, r, category)
2017-11-20 00:10:04 -05:00
}
func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
userID := request.UserID(r)
categoryID := request.RouteInt64Param(r, "categoryID")
2017-11-20 00:10:04 -05:00
category, err := h.store.Category(userID, categoryID)
if err != nil {
json.ServerError(w, r, err)
return
}
if category == nil {
json.NotFound(w, r)
return
}
2021-01-04 01:28:04 -05:00
var categoryRequest model.CategoryRequest
if err := json_parser.NewDecoder(r.Body).Decode(&categoryRequest); err != nil {
2018-10-07 21:42:43 -04:00
json.BadRequest(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2021-01-04 01:28:04 -05:00
if validationErr := validator.ValidateCategoryModification(h.store, userID, category.ID, &categoryRequest); validationErr != nil {
json.BadRequest(w, r, validationErr.Error())
2017-11-20 00:10:04 -05:00
return
}
2021-01-04 01:28:04 -05:00
categoryRequest.Patch(category)
err = h.store.UpdateCategory(category)
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2018-10-07 21:42:43 -04:00
json.Created(w, r, category)
2017-11-20 00:10:04 -05:00
}
2020-11-25 14:46:05 -05:00
func (h *handler) markCategoryAsRead(w http.ResponseWriter, r *http.Request) {
userID := request.UserID(r)
categoryID := request.RouteInt64Param(r, "categoryID")
category, err := h.store.Category(userID, categoryID)
if err != nil {
json.ServerError(w, r, err)
return
}
if category == nil {
json.NotFound(w, r)
return
}
if err = h.store.MarkCategoryAsRead(userID, categoryID, time.Now()); err != nil {
json.ServerError(w, r, err)
return
}
json.NoContent(w, r)
}
func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) {
categories, err := h.store.Categories(request.UserID(r))
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
json.OK(w, r, categories)
2017-11-20 00:10:04 -05:00
}
func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) {
2018-09-03 17:26:40 -04:00
userID := request.UserID(r)
categoryID := request.RouteInt64Param(r, "categoryID")
2017-11-20 00:10:04 -05:00
2021-01-04 01:28:04 -05:00
if !h.store.CategoryIDExists(userID, categoryID) {
2018-10-07 21:42:43 -04:00
json.NotFound(w, r)
2017-11-20 00:10:04 -05:00
return
}
if err := h.store.RemoveCategory(userID, categoryID); err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2018-10-07 21:42:43 -04:00
json.NoContent(w, r)
2017-11-20 00:10:04 -05:00
}