-
Notifications
You must be signed in to change notification settings - Fork 1
/
CtrProduct.go
76 lines (63 loc) · 1.66 KB
/
CtrProduct.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
package Controllers
import (
"github.com/CloudWise-OpenSource/GoCrab/Api"
"github.com/SeasX/SeasConfig/Models"
)
type ProductControl struct {
GoCrab.Controller
}
//get all input params in uri
func (ctr *ProductControl) getParams(getExcess bool) (pid string, name string, description string) {
pid = ctr.Ctx.Input.Params[":PID"]
if getExcess {
name = ctr.GetString("name")
description = ctr.GetString("description")
} else {
name = ""
description = ""
}
return pid, name, description
}
//create new product
func (ctr *ProductControl) Post() {
pid, name, description := ctr.getParams(true)
Models.PutProduct(pid, name, description)
ctr.RESTSuccess(pid, "Create Product Successful")
}
//get product name
func (ctr *ProductControl) Get() {
pid, _, _ := ctr.getParams(false)
if product, err := Models.GetProduct(pid); err != nil {
ctr.RESTFaild(pid, err.Error())
} else {
ctr.RESTSuccess(product, nil)
}
}
//update product info
func (ctr *ProductControl) Put() {
pid, name, description := ctr.getParams(true)
Models.PutProduct(pid, name, description)
ctr.RESTSuccess(pid, "Update Product Successful")
}
//delete product by pid
func (ctr *ProductControl) Delete() {
pid, _, _ := ctr.getParams(false)
if err := Models.ExistsProduct(pid); err != nil {
ctr.RESTFaild(pid, err.Error())
} else {
if Models.DeleteProduct(pid) {
ctr.RESTSuccess(pid, "Delete Product Successful")
} else {
ctr.RESTFaild(pid, "Delete Product unsuccessful")
}
}
}
//exists product by pid
func (ctr *ProductControl) Head() {
pid, _, _ := ctr.getParams(false)
if err := Models.ExistsProduct(pid); err != nil {
ctr.RESTHeadNotFound()
} else {
ctr.RESTHeadSuccess()
}
}