/
category.go
217 lines (188 loc) · 5.47 KB
/
category.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package v1
import (
"net/http"
"strconv"
"github.com/Gavazn/Gavazn/internal/category"
"github.com/Gavazn/Gavazn/internal/media"
"github.com/Gavazn/Gavazn/internal/user"
"github.com/labstack/echo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type categoryForm struct {
Parent primitive.ObjectID `json:"parent" form:"parent"`
Name string `json:"name" form:"name"`
Thumbnail primitive.ObjectID `json:"thumbnail" form:"thumbnail"`
}
func categoryToJSON(c category.Category) bson.M {
u, _ := user.FindOne(bson.M{"_id": c.User})
parent, _ := category.FindOne(bson.M{"_id": c.Parent})
t, _ := media.FindOne(bson.M{"_id": c.Thumbnail})
return bson.M{
"id": c.ID.Hex(),
"user": u,
"parent": parent,
"name": c.Name,
"thumbnail": t,
"created_at": c.CreatedAt,
}
}
/**
* @api {post} /api/v1/categories add category
* @apiVersion 1.0.0
* @apiName addCategory
* @apiGroup Category
*
* @apiParam {String} parent parent
* @apiParam {String} name name
* @apiParam {String} thumbnail thumbnail id
*
* @apiSuccess {String} message success message.
* @apiSuccess {Object} category category model
*
* @apiError {String} error api error message
*/
func addCategory(ctx echo.Context) error {
u := ctx.Get("user").(*user.User)
form := new(categoryForm)
if err := ctx.Bind(form); err != nil {
return ctx.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
c := &category.Category{
User: u.ID,
Parent: form.Parent,
Name: form.Name,
Thumbnail: form.Thumbnail,
}
if err := c.Save(); err != nil {
return ctx.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()})
}
return ctx.JSON(http.StatusOK, echo.Map{
"message": "category created successfully",
"category": categoryToJSON(*c),
})
}
/**
* @api {put} /api/v1/categories/:id edit category
* @apiVersion 1.0.0
* @apiName editCategory
* @apiGroup Category
*
* @apiParam {String} parent parent
* @apiParam {String} name name
* @apiParam {String} thumbnail thumbnail id
*
* @apiSuccess {String} message success message.
* @apiSuccess {Object} category category model
*
* @apiError {String} error api error message
*/
func editCategory(ctx echo.Context) error {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
return ctx.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
form := new(categoryForm)
if err := ctx.Bind(form); err != nil {
return ctx.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
c, err := category.FindOne(bson.M{"_id": id})
if err != nil {
return ctx.JSON(http.StatusNotFound, echo.Map{"error": err.Error()})
}
c.Parent = form.Parent
c.Name = form.Name
c.Thumbnail = form.Thumbnail
if err := c.Save(); err != nil {
return ctx.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()})
}
return ctx.JSON(http.StatusOK, echo.Map{
"message": "category updated successfully",
"category": categoryToJSON(*c),
})
}
/**
* @api {get} /api/v1/categories/:id get a category
* @apiVersion 1.0.0
* @apiName getCategory
* @apiGroup Category
*
* @apiSuccess {Object} category category model
*
* @apiError {String} error api error message
*/
func getCategory(ctx echo.Context) error {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
return ctx.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
c, err := category.FindOne(bson.M{"_id": id})
if err != nil {
return ctx.JSON(http.StatusNotFound, echo.Map{"error": err.Error()})
}
return ctx.JSON(http.StatusOK, echo.Map{
"category": categoryToJSON(*c),
})
}
/**
* @api {delete} /api/v1/categories/:id remove category
* @apiVersion 1.0.0
* @apiName removeCategory
* @apiGroup Category
*
* @apiSuccess {String} message success message.
*
* @apiError {String} error api error message
*/
func removeCategory(ctx echo.Context) error {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
return ctx.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
c, err := category.FindOne(bson.M{"_id": id})
if err != nil {
return ctx.JSON(http.StatusNotFound, echo.Map{"error": err.Error()})
}
if err := c.Delete(); err != nil {
return ctx.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()})
}
return ctx.JSON(http.StatusOK, echo.Map{
"message": "category removed successfully",
})
}
/**
* @api {get} /api/v1/categories list of categories
* @apiVersion 1.0.0
* @apiName listCategories
* @apiGroup Category
*
* @apiParam {String} q query search
* @apiParam {Number} page list page
* @apiParam {Number} limit list limit
* @apiParam {String} sort sort list example -created,title,...
*
* @apiSuccess {Number} page page number
* @apiSuccess {Number} total_count total number of results
* @apiSuccess {Object[]} categories array of category model
*
* @apiError {String} error api error message
*/
func listCategories(ctx echo.Context) error {
filter := bson.M{}
page, _ := strconv.Atoi(ctx.QueryParam("page"))
limit, _ := strconv.Atoi(ctx.QueryParam("limit"))
if q := ctx.QueryParam("q"); q != "" {
filter["$text"] = bson.M{"$search": q}
}
categories := category.Find(filter, page, limit, ctx.Get("sort").(bson.D)...)
count := category.Count(filter)
response := []bson.M{}
for _, c := range categories {
response = append(response, categoryToJSON(c))
}
return ctx.JSON(http.StatusOK, echo.Map{
"categories": response,
"page": page,
"total_count": count,
})
}