-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.go
46 lines (34 loc) · 1.06 KB
/
user.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
package handlers
import (
"encoding/json"
"net/http"
"github.com/bhuvansingla/iitk-coin/account"
"github.com/bhuvansingla/iitk-coin/auth"
"github.com/bhuvansingla/iitk-coin/errors"
)
type GetNameResponse struct {
RollNo string `json:"rollNo"`
Name string `json:"name"`
}
func GetNameByRollNo(w http.ResponseWriter, r *http.Request) error {
if r.Method != "GET" {
return errors.NewHTTPError(nil, http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
}
queriedRollNo := r.URL.Query().Get("rollNo")
if err := account.ValidateRollNo(queriedRollNo); err != nil {
return errors.NewHTTPError(err, http.StatusBadRequest, "invalid rollNo")
}
_, err := auth.GetRollNoFromRequest(r)
if err != nil {
return errors.NewHTTPError(err, http.StatusBadRequest, "invalid cookie")
}
name, err := account.GetNameByRollNo(queriedRollNo)
if err != nil{
return errors.NewHTTPError(err, http.StatusBadRequest, "could not find username")
}
json.NewEncoder(w).Encode(&GetNameResponse{
Name: name,
RollNo: queriedRollNo,
})
return nil
}