-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_user.go
More file actions
72 lines (56 loc) · 1.77 KB
/
create_user.go
File metadata and controls
72 lines (56 loc) · 1.77 KB
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
package main
import (
"encoding/json"
"net/http"
)
// MessagePost: POST
func CreateUser(w http.ResponseWriter, r *http.Request) {
// We need to use to use the struct model to map the json data to
type ResponseBody struct {
Token string `json:"token"`
User string `json:"user"`
}
var jsonResponse UserSignup
// We decode the incoming data and convert it to a json
json.NewDecoder(r.Body).Decode(&jsonResponse)
println("Message to Server", jsonResponse.Name) // simply print the email
// Check if user already exists
if RedisClient.HExists("users", jsonResponse.Name).Val() == true {
println("a user does indeed exist")
println(RedisClient.HExists("users", jsonResponse.Name).Val())
// send Json telling him name already exists bro
json.NewEncoder(w).Encode("User " + jsonResponse.Name + " already exists choose another name")
} else {
println("no user exist go ahead and create that mother fucka")
// Marshal json aka stringify the Json
userJson, err := json.Marshal(jsonResponse)
if err != nil {
println(err)
return
}
errset := RedisClient.HSet("users", jsonResponse.Name, userJson).Err()
if errset != nil {
panic(errset)
}
json.NewEncoder(w).Encode(ResponseBody{
Token: CreateToken(jsonResponse.Name),
})
}
}
// // check if name in redis if so tell him no go
// if jsonResponse.Name == redisJson.Name {
// json.NewEncoder(w).Encode("User " + redisJson.Name + " already exists choose another name")
// } else {
// }
// redisUser, err := RedisClient.HGet("users", jsonResponse.Name).Result()
// if err != nil {
// println(err)
// }
// fmt.Println(redisUser)
// // Unmarshal the Json from Redis
// var redisJson UserSignup
// bytes := []byte(redisUser)
// err2 := json.Unmarshal(bytes, &redisJson)
// if err2 != nil {
// panic(err2)
// }