-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (89 loc) · 2.79 KB
/
main.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
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"os"
"strconv"
"strings"
_ "github.com/go-sql-driver/mysql"
)
type Products struct {
quantity int64
price int64
isRefrigirator bool
}
func main() {
_ = RunWebPortal()
}
func RunWebPortal() error {
http.HandleFunc("/", rootHandler)
return http.ListenAndServe("localhost", nil)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat("app.txt"); os.IsNotExist(err) {
_, err := os.Create("app.txt")
if err != nil {
panic(err)
}
}
file, _ := os.OpenFile("app.txt", os.O_RDWR|os.O_APPEND, 0660)
productName := r.URL.Query().Get("name")
quantity := r.URL.Query().Get("quantity")
price := r.URL.Query().Get("price")
selectedType := r.URL.Query().Get("type")
success := true
if selectedType == "normal" {
_, _ = file.WriteString("Product is of type: normal \n")
if idx := strings.Index(productName, "_"); idx != -1 {
productName = productName + "_"
result := productName[:idx]
productName = strings.ReplaceAll(productName, result, "")
productName = strings.TrimLeft(productName, "_")
pos := strings.Index(productName, "_")
id, _ := strconv.ParseInt(productName[:pos], 0, 64)
db, err := sql.Open("mysql", "snappmarket:snappmarket@/snappmarket")
if err != nil {
log.Fatal(err)
}
defer db.Close()
row := db.QueryRow("select * from products where id = ?", id)
product := Products{}
err = row.Scan(&product.price, &product.quantity)
updateResult, err := db.Exec("update products set `quantity` = ? and price = ? where id = ?", quantity, price, id)
}
} else if selectedType == "refrigerator" {
_, _ = file.WriteString("Product is of type: refrigerator \n")
if idx := strings.Index(productName, "_"); idx != -1 {
productName = productName + "_"
result := productName[:idx]
productName = strings.ReplaceAll(productName, result, "")
productName = strings.TrimLeft(productName, "_")
pos := strings.Index(productName, "_")
id, _ := strconv.ParseInt(productName[:pos], 0, 64)
db, err := sql.Open("mysql", "snappmarket:snappmarket@/snappmarket")
if err != nil {
log.Fatal(err)
}
defer db.Close()
row := db.QueryRow("select * from products where id = ?", id)
product := Products{}
err = row.Scan(&product.price, &product.quantity)
updateResult, err := db.Exec("update products set `quantity` = ? and price = ? and isRefrigirator = ? where id = ?", quantity, price, true, id)
}
}
if err := recover(); err != nil {
success = false
}
statusCode := http.StatusInternalServerError
statusText := "error"
if success {
statusCode = http.StatusOK
statusText = "ok"
}
data := make(map[interface{}]interface{})
data["status"] = statusText
w.WriteHeader(statusCode)
_ = json.NewEncoder(w).Encode(data)
}