-
Notifications
You must be signed in to change notification settings - Fork 0
/
students.go
56 lines (51 loc) · 1.44 KB
/
students.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
package handler
import (
"fmt"
"github.com/2-of-Clubs/2ofclubs-server/app/model"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"net/http"
)
func CreateStudent(db *gorm.DB, w http.ResponseWriter, r *http.Request) {
// Check if content type is application/json?
s := model.NewStudent()
p := ExtractPersonInfo(r)
pass, isHashed := Hash(p.Password)
p.Password = pass
s.Person = p
found := RecordExists(db, model.ColumnUsername, s.Username, s)
if !found && isHashed {
fmt.Println(s.Username)
if tp, err := GetTokenPair(s.Username, 5, 60*24); err == nil {
db.Create(&s)
c := GenerateCookie(model.RefreshToken, tp.RefreshToken)
http.SetCookie(w, c)
WriteData(tp.AccessToken, http.StatusOK, w)
}
}
}
func GetStudent(db *gorm.DB, w http.ResponseWriter, r *http.Request) {
status := http.StatusOK
data := ""
vars := mux.Vars(r)
username := vars[model.ColumnUsername]
if ValidateUserReq(username, r) {
s := model.NewStudent()
ss := model.NewStatus()
// Defaults will be overridden when obtaining data and being inserted into struct except for null
found := RecordExists(db, model.ColumnUsername, username, s)
if !found {
ss.Message = model.Failure
} else {
ss.Data = s
}
data = ParseJSON(ss)
} else {
data = http.StatusText(http.StatusForbidden)
status = http.StatusForbidden
}
WriteData(data, status, w)
}
func UpdateStudent(db *gorm.DB, w http.ResponseWriter, r *http.Request) {
fmt.Println("Update Student")
}