-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsaleproduct.go
132 lines (109 loc) · 3.16 KB
/
saleproduct.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
)
// saleProduct
// SaleProductGet .
func SaleProductGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
params := r.URL.Query()
limitOffset := " "
if _, ok := params["limit"]; ok {
limitOffset += " limit " + params["limit"][0]
delete(params, "limit")
}
offset := defaultOffset
if _, ok := params["offset"]; ok {
limitOffset += " offset " + params["offset"][0]
offset = params["offset"][0]
delete(params, "offset")
}
orderBy := " "
if _, ok := params["orderby"]; ok {
orderBy += " order by " + params["orderby"][0]
delete(params, "orderby")
if _, ok := params["sortby"]; ok {
orderBy += " " + params["sortby"][0] + " "
delete(params, "sortby")
} else {
orderBy += " asc "
}
} else {
orderBy += " order by created_date_time desc "
}
resp := " * "
if _, ok := params["resp"]; ok {
resp = " " + params["resp"][0] + " "
delete(params, "resp")
}
where := ""
init := false
for key, val := range params {
if init {
where += " and "
}
where += " `" + key + "` = '" + val[0] + "' "
init = true
}
SQLQuery := " from `" + saleProductTable + "`"
if strings.Compare(where, "") != 0 {
SQLQuery += " where " + where
}
SQLQuery += orderBy
SQLQuery += limitOffset
data, status, ok := selectProcess("select " + resp + SQLQuery)
w.Header().Set("Status", status)
if ok {
response["data"] = data
pagination := map[string]string{}
if len(where) > 0 {
count, _, _ := selectProcess("select count(*) as ctn from `" + saleProductTable + "` where " + where)
pagination["total_count"] = count[0]["ctn"]
} else {
count, _, _ := selectProcess("select count(*) as ctn from `" + saleProductTable + "`")
pagination["total_count"] = count[0]["ctn"]
}
pagination["count"] = strconv.Itoa(len(data))
pagination["offset"] = offset
response["pagination"] = pagination
response["meta"] = setMeta(status, "ok", "")
} else {
response["meta"] = setMeta(status, "", dialogType)
}
w.WriteHeader(getHTTPStatusCode(response["meta"].(map[string]string)["status"]))
json.NewEncoder(w).Encode(response)
}
// SaleProductUpdate .
func SaleProductUpdate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
body := map[string]string{}
// file upload
r.ParseMultipartForm(32 << 20)
for key, value := range r.Form {
body[key] = value[0]
}
for _, field := range saleProductRequiredFields {
if _, ok := body[field]; ok {
if len(body[field]) == 0 {
SetReponseStatus(w, r, statusCodeBadRequest, field+" required", dialogType, response)
return
}
}
}
body["modified_date_time"] = time.Now().UTC().String()
status, ok := updateSQL(saleProductTable, r.URL.Query(), body)
w.Header().Set("Status", status)
if ok {
response["meta"] = setMeta(status, "SaleProduct Row updated", dialogType)
} else {
response["meta"] = setMeta(status, "", dialogType)
}
w.WriteHeader(getHTTPStatusCode(response["meta"].(map[string]string)["status"]))
json.NewEncoder(w).Encode(response)
}