/
product.go
144 lines (117 loc) · 3.46 KB
/
product.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
package v1
import (
"strconv"
"github.com/labstack/echo"
"github.com/trykafito/kafito/internal/product"
"github.com/trykafito/kafito/internal/user"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type productForm struct {
Title string `json:"title" form:"title"`
Description string `json:"description" form:"description"`
Informations map[string]interface{} `json:"informations" form:"informations"`
Price float64 `json:"price" form:"price"`
Quantity int `json:"quantity" form:"quantity"`
Thumbnail primitive.ObjectID `json:"thumbnail" form:"thumbnail"`
}
func productToJSON(p product.Product) M {
return M{
"id": p.ID.Hex(),
"created_by": p.CreatedBy,
"title": p.Title,
"description": p.Description,
"informations": p.Informations,
"price": p.Price,
"quantity": p.Quantity,
"thumbnail": p.Thumbnail,
"created_at": p.CreatedAt,
}
}
func addProduct(ctx echo.Context) error {
u := ctx.Get("user").(*user.User)
form := new(productForm)
if err := ctx.Bind(form); err != nil {
return ctx.JSON(400, echo.Map{"error": err.Error()})
}
p := &product.Product{
CreatedBy: u.ID,
Title: form.Title,
Description: form.Description,
Informations: form.Informations,
Price: form.Price,
Quantity: form.Quantity,
Thumbnail: form.Thumbnail,
}
if err := p.Save(); err != nil {
return ctx.JSON(500, echo.Map{"error": err.Error()})
}
return ctx.JSON(200, echo.Map{
"message": "product created successfully",
"product": productToJSON(*p),
})
}
func editProduct(ctx echo.Context) error {
filter := bson.M{}
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
return ctx.JSON(404, echo.Map{"error": err.Error()})
}
filter["_id"] = id
form := new(productForm)
if err := ctx.Bind(form); err != nil {
return ctx.JSON(400, echo.Map{"error": err.Error()})
}
p, err := product.FindOne(filter)
if err != nil {
return ctx.JSON(404, echo.Map{"error": err.Error()})
}
p.Title = form.Title
p.Description = form.Description
p.Informations = form.Informations
p.Price = form.Price
p.Quantity = form.Quantity
p.Thumbnail = form.Thumbnail
if err := p.Save(); err != nil {
return ctx.JSON(500, echo.Map{"error": err.Error()})
}
return ctx.JSON(200, echo.Map{
"message": "product updated successfully",
"product": productToJSON(*p),
})
}
func getProduct(ctx echo.Context) error {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
return ctx.JSON(400, echo.Map{"error": err.Error()})
}
p, err := product.FindOne(bson.M{"_id": id})
if err != nil {
return ctx.JSON(404, echo.Map{"error": err.Error()})
}
return ctx.JSON(200, echo.Map{
"product": productToJSON(*p),
})
}
func listProducts(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["title"] = bson.M{"$regex": q, "$options": "i"}
}
products, err := product.Find(filter, page, limit, ctx.Get("sort").(bson.D)...)
if err != nil {
return ctx.JSON(404, echo.Map{"error": err.Error()})
}
count := product.Count(filter)
result := []M{}
for _, p := range products {
result = append(result, productToJSON(p))
}
return ctx.JSON(200, echo.Map{
"products": result,
"page": page,
"total_count": count,
})
}