/
app.go
240 lines (198 loc) · 5.75 KB
/
app.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/rs/cors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type App struct {
DB *gorm.DB
r *mux.Router
}
func (a *App) Initialize(dbDriver string, dbURI string) {
db, err := gorm.Open(sqlite.Open(dbURI), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
a.DB = db
// Migrate the schema.
a.DB.AutoMigrate(&ClientDetails{})
a.DB.AutoMigrate(&UserDetails{})
fmt.Println("initiated db")
a.r = mux.NewRouter()
a.initializeRoutes()
}
func (a *App) CreateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var s ClientDetails
err := json.NewDecoder(r.Body).Decode(&s)
if err != nil {
sendErr(w, http.StatusBadRequest, err.Error())
return
}
x1 := rand.NewSource(time.Now().UnixNano())
y1 := rand.New(x1)
s.TrackingID = strconv.Itoa(y1.Intn(1000000000))
s.CreatedAt = time.Now()
s.UpdatedAt = time.Now()
min := 1
max := 5
temp := rand.Intn(max-min) + min
s.Status = strconv.Itoa(temp)
a.DB.Save(&s)
resp := make(map[string]string)
resp["message"] = "Status Created"
resp["trackingID"] = s.TrackingID
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(jsonResp)
}
func (a *App) getClient(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
temp := r.URL.Query().Get("trackingID")
var client ClientDetails
err := a.DB.Where("tracking_id = ?", temp).First(&client).Error
if err != nil {
w.WriteHeader(http.StatusBadRequest)
resp := make(map[string]string)
resp["error"] = "Tracking ID not found"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
return
}
resp := make(map[string]string)
resp["firstname"] = client.FirstName
resp["lastname"] = client.LastName
resp["phone_number"] = client.PhoneNumber
resp["email"] = client.Email
resp["registration_number"] = client.RegistrationNumber
resp["service_type"] = client.ServiceType
resp["appointment_date"] = client.AppointmentDate
resp["tracking_id"] = client.TrackingID
resp["status"] = client.Status
resp["created_at"] = client.CreatedAt.String()
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func (a *App) deleteClient(w http.ResponseWriter, r *http.Request) {
trackingID := r.URL.Query().Get("trackingID")
var client ClientDetails
err := a.DB.Where("tracking_id = ?", trackingID).Delete(&client).Error
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
resp := make(map[string]string)
resp["message"] = "Status Deleted"
resp["trackingID"] = trackingID
w.WriteHeader(http.StatusAccepted)
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func (a *App) updateClient(w http.ResponseWriter, r *http.Request) {
var s ClientDetails
e := json.NewDecoder(r.Body).Decode(&s)
if e != nil {
sendErr(w, http.StatusBadRequest, e.Error())
return
}
var client ClientDetails
err := a.DB.Model(&client).Where("tracking_id = ?", s.TrackingID).Update("appointment_date", s.AppointmentDate).Error
if err != nil {
log.Fatalf("Error happened in find. Err: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
resp := make(map[string]string)
resp["message"] = "Status Updated"
resp["trackingID"] = s.TrackingID
w.WriteHeader(http.StatusAccepted)
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func (a *App) createUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var u UserDetails
err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
sendErr(w, http.StatusBadRequest, err.Error())
return
}
a.DB.Save(&u)
resp := make(map[string]string)
resp["message"] = "Status Created"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(jsonResp)
}
func (a *App) checkUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
uname := r.URL.Query().Get("firstname")
pass := r.URL.Query().Get("password")
var user UserDetails
err := a.DB.Where("first_name = ?", uname).First(&user).Error
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
resp := make(map[string]string)
resp["error"] = "username not found"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
return
}
resp := make(map[string]string)
if user.Password == pass {
resp["status"] = "valid"
} else {
resp["status"] = "invalid"
w.WriteHeader(http.StatusBadRequest)
}
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func sendErr(w http.ResponseWriter, code int, message string) {
resp, _ := json.Marshal(map[string]string{"error": message})
http.Error(w, string(resp), code)
}
func (a *App) Run(addr string) {
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:4200", "http://localhost:4200/AppointmentForm"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "DELETE", "POST", "PUT"},
})
handler := c.Handler(a.r)
log.Fatal(http.ListenAndServe(":8080", handler))
}