-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
75 lines (65 loc) · 1.75 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
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
package handler
import (
"encoding/json"
"fmt"
"net/http"
"github.com/GDSC-KMUTT/totp-session/service"
"github.com/GDSC-KMUTT/totp-session/types"
"github.com/GDSC-KMUTT/totp-session/utils"
)
type userHandler struct {
service service.UserService
}
func NewUserHandler(userSerivice service.UserService) userHandler {
return userHandler{service: userSerivice}
}
func (h userHandler) SignUp(w http.ResponseWriter, r *http.Request) {
// Check if the request method is POST
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Set the response header to application/json
w.Header().Set("Content-Type", "application/json")
var response []byte
var body types.SignIn
err := utils.Parse(r, &body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Call signup service
token, base64, err := h.service.SignUp(body.Email, body.Password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create a response
response, _ = json.Marshal(map[string]any{"success": true, "token": token, "image": base64})
fmt.Fprint(w, response)
return
}
func (h userHandler) SignIn(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, r.Body)
}
func (h userHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
users, err := h.service.ListUsers()
if err != nil {
panic(err.Error())
}
response, err := json.Marshal(map[string]any{"users": users})
if err != nil {
panic(err)
}
fmt.Fprint(w, response)
}