-
Notifications
You must be signed in to change notification settings - Fork 0
/
Users.go
46 lines (41 loc) · 1.55 KB
/
Users.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 main
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type User struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Firstname string `json:"firstname,omitempty" bson:"firstname,omitempty"`
Email string `json:"email,omitempty" bson:"email,omitempty"`
Pass string `json:"pass,omitempty" bson:"pass,omitempty"`
}
func CreateUserEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
var salt = genRandomSalt(saltSize)
var user User
_ = json.NewDecoder(request.Body).Decode(&user)
user.Pass = hashPassword(user.Pass, salt)
collection := client.Database("Insta").Collection("user")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
result, _ := collection.InsertOne(ctx, user)
json.NewEncoder(response).Encode(result)
}
func GetUserEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
params := mux.Vars(request)
id, _ := primitive.ObjectIDFromHex(params["id"])
var user User
collection := client.Database("Insta").Collection("user")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err := collection.FindOne(ctx, User{ID: id}).Decode(&user)
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
return
}
json.NewEncoder(response).Encode(user)
}