-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
135 lines (125 loc) · 2.85 KB
/
api.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/mamoss-oss/chirpy/internal/db"
"github.com/mamoss-oss/chirpy/internal/helpers"
)
func API_PostChirp(w http.ResponseWriter, r *http.Request) {
db, err := db.NewDB("./database.json")
if err != nil {
respondWithError(w, 400, err.Error())
return
}
message, err := validate_chirp(r)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
chirp, err := db.CreateChirp(message)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
respondWithJSON(w, 201, chirp)
}
func API_GetChirps(w http.ResponseWriter, r *http.Request) {
db, err := db.NewDB("./database.json")
if err != nil {
respondWithError(w, 400, err.Error())
return
}
chirps, err := db.GetChirps()
if err != nil {
respondWithError(w, 400, err.Error())
return
}
respondWithJSON(w, 200, chirps)
}
func API_Get_Single_Chirp(w http.ResponseWriter, r *http.Request) {
db, err := db.NewDB("./database.json")
if err != nil {
respondWithError(w, 400, err.Error())
return
}
dbStructure, err := db.LoadDB()
if err != nil {
respondWithError(w, 400, err.Error())
return
}
chirp_id_str := chi.URLParam(r, "id")
chirp_id_int, err := strconv.Atoi(chirp_id_str)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
data, ok := dbStructure.Chirps[chirp_id_int]
if !ok {
respondWithError(w, 404, "not found")
return
}
respondWithJSON(w, 200, data)
}
func API_Create_User(w http.ResponseWriter, r *http.Request) {
db, err := db.NewDB("./database.json")
if err != nil {
respondWithError(w, 400, err.Error())
return
}
email, password, err := helpers.Get_email_password(r)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
user, err := db.CreateUser(email, password)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
user_no_pass := struct {
ID int `json:"id"`
Email string `json:"email"`
}{
ID: user.ID,
Email: user.Email,
}
respondWithJSON(w, 201, user_no_pass)
}
func API_User_Login(w http.ResponseWriter, r *http.Request) {
database, err := db.NewDB("./database.json")
if err != nil {
respondWithError(w, 400, err.Error())
return
}
database_loaded, err := database.LoadDB()
if err != nil {
respondWithError(w, 400, err.Error())
return
}
email, password, err := helpers.Get_email_password(r)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
user, err := db.FindByMail(&database_loaded, email)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
password_ok := db.ComparePasswords(user.Password, password)
if password_ok {
user_no_pass := struct {
ID int `json:"id"`
Email string `json:"email"`
}{
ID: user.ID,
Email: user.Email,
}
respondWithJSON(w, 200, user_no_pass)
return
} else {
respondWithError(w, 401, "Unauthorized")
return
}
}