aboutsummaryrefslogtreecommitdiffhomepage
path: root/api/category.go
blob: 67c7e5230d1fef8f0ad5e57a02e660347318cc28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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/miniflux/http/handler"
)

// CreateCategory is the API handler to create a new category.
func (c *Controller) CreateCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
	userID := ctx.UserID()
	category, err := decodeCategoryPayload(request.Body())
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	category.UserID = userID
	if err := category.ValidateCategoryCreation(); err != nil {
		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"))
		return
	}

	err = c.store.CreateCategory(category)
	if err != nil {
		response.JSON().ServerError(errors.New("Unable to create this category"))
		return
	}

	response.JSON().Created(category)
}

// UpdateCategory is the API handler to update a category.
func (c *Controller) UpdateCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
	categoryID, err := request.IntegerParam("categoryID")
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	category, err := decodeCategoryPayload(request.Body())
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	category.UserID = ctx.UserID()
	category.ID = categoryID
	if err := category.ValidateCategoryModification(); err != nil {
		response.JSON().BadRequest(err)
		return
	}

	err = c.store.UpdateCategory(category)
	if err != nil {
		response.JSON().ServerError(errors.New("Unable to update this category"))
		return
	}

	response.JSON().Created(category)
}

// GetCategories is the API handler to get a list of categories for a given user.
func (c *Controller) GetCategories(ctx *handler.Context, request *handler.Request, response *handler.Response) {
	categories, err := c.store.Categories(ctx.UserID())
	if err != nil {
		response.JSON().ServerError(errors.New("Unable to fetch categories"))
		return
	}

	response.JSON().Standard(categories)
}

// RemoveCategory is the API handler to remove a category.
func (c *Controller) RemoveCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
	userID := ctx.UserID()
	categoryID, err := request.IntegerParam("categoryID")
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	if !c.store.CategoryExists(userID, categoryID) {
		response.JSON().NotFound(errors.New("Category not found"))
		return
	}

	if err := c.store.RemoveCategory(userID, categoryID); err != nil {
		response.JSON().ServerError(errors.New("Unable to remove this category"))
		return
	}

	response.JSON().NoContent()
}