-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
254 lines (212 loc) · 6.95 KB
/
handlers.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
)
func getUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var users []User
db.Find(&users)
json.NewEncoder(w).Encode(users)
}
func getUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var user User
params := mux.Vars(r)
db.First(&user, params["userid"])
if user.ID == 0 {
json.NewEncoder(w).Encode("NO SUCH USER")
return
}
json.NewEncoder(w).Encode(user)
}
func getUsersByRole(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var users []User
params := mux.Vars(r)
db.Find(&users, "role = ?", params["role"])
json.NewEncoder(w).Encode(users)
}
func addUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var user User
json.NewDecoder(r.Body).Decode(&user)
db.Create(&user)
json.NewEncoder(w).Encode(user)
}
func delUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
id := params["userid"]
var cnt int
err := db.Raw("SELECT COUNT(*) FROM users WHERE id = ?", id).Scan(&cnt).Error
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if cnt == 0 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("User Not Found!"))
return
}
err = db.Exec("DELETE FROM users WHERE id = ?", id).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error deleting user"))
return
}
json.NewEncoder(w).Encode("User Deleted")
}
func punchIn(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var user UserAttd
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
userID := user.UserId
currentDate := time.Now().Format("2006-01-02")
// Check if the user has punched in for the current date
var existingUser UserAttd
result := db.First(&existingUser, "user_id = ? AND date = ?", userID, currentDate)
if result.Error != nil { // If no record found, create a new row
result = db.Create(&user)
if result.Error != nil {
http.Error(w, "Unable to Punch In", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode("Punched In")
} else { // If a record found, update the existing row
if existingUser.Active {
json.NewEncoder(w).Encode("Already Punched In")
return
} else {
existingUser.Active = true
// Update the existing user's activity status
result = db.Save(&existingUser)
if result.Error != nil {
http.Error(w, "Unable to Punch In", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode("Punched In")
}
}
}
func punchOut(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var user UserAttd
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
userID := user.UserId
currentDate := time.Now().Format("2006-01-02")
// Check if the user has punched in for the current date
var existingUser UserAttd
result := db.First(&existingUser, "user_id = ? AND date = ?", userID, currentDate)
if result.Error != nil { // If no record found, create a new row
json.NewEncoder(w).Encode("You are Not Punched In")
} else { // If a record found, update the existing row
if !existingUser.Active {
json.NewEncoder(w).Encode("You are Not Punched In")
return
} else {
existingUser.Active = false
// Update the existing user's activity status
result = db.Save(&existingUser)
if result.Error != nil {
http.Error(w, "Unable to Punch Out", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode("Punched Out")
}
}
}
func changePassword(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var u User
var user User
json.NewDecoder(r.Body).Decode(&u)
newPassword := u.Password // todo - hash password
db.First(&user, "id = ?", params["userid"])
db.Model(&user).Updates(map[string]interface{}{
"Password": newPassword,
})
json.NewEncoder(w).Encode(user)
}
func getUserAttendance(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
userID := params["userid"]
month := params["month"]
year := params["year"]
var days []time.Time
db.Model(&UserAttd{}).Where("user_id = ? AND EXTRACT(MONTH FROM date) = ? AND EXTRACT(YEAR FROM date) = ?", userID, month, year).Pluck("date", &days)
json.NewEncoder(w).Encode(days)
}
func getClassAttendance(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
class := params["class"]
month := params["month"]
year := params["year"]
m, _ := strconv.Atoi(month)
y, _ := strconv.Atoi(year)
startDate := time.Date(y, time.Month(m), 1, 0, 0, 0, 0, time.UTC)
endDate := startDate.AddDate(0, 1, -1)
startDay := startDate.Day()
endDay := endDate.Day()
mp := make(map[int]int)
for i := startDay; i <= endDay; i++ {
currDate := i
var cnt int
db.Raw("SELECT COUNT(*) FROM (SELECT * FROM (SELECT * FROM users INNER JOIN user_attds ON users.id = user_attds.user_id) where EXTRACT(DAY FROM date) = ? AND EXTRACT(MONTH FROM date) = ? AND EXTRACT(YEAR FROM date) = ? AND class = ?)", currDate, month, year, class).Scan(&cnt)
mp[currDate] = cnt
}
json.NewEncoder(w).Encode(mp)
}
func login(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var creds Credentials
err := json.NewDecoder(r.Body).Decode(&creds)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var expectedPassword string
db.Raw("select password from users where email = ?", creds.Username).Scan(&expectedPassword)
if expectedPassword != creds.Password {
w.WriteHeader(http.StatusUnauthorized)
return
}
expirationTime := time.Now().Add(time.Minute * 5)
claims := &Claims{
Username: creds.Username,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenStr, err := token.SignedString(jwtKey)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w,
&http.Cookie{
Name: "token",
Value: tokenStr,
Expires: expirationTime,
})
var u User
db.Raw("select * from users where email = ?", creds.Username).Scan(&u)
json.NewEncoder(w).Encode(u)
}