-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiusers.go
48 lines (41 loc) · 1.2 KB
/
apiusers.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
package main
import (
"encoding/json"
"net/http"
)
// GetUsers from database.
func (srv *Shoutyface) GetUsers(w http.ResponseWriter, r *http.Request) {
users := srv.listUsers()
data, _ := json.Marshal(users)
w.Write([]byte(data))
}
// PostUser adds a user to the database.
func (srv *Shoutyface) PostUser(w http.ResponseWriter, r *http.Request) {
username := r.Header.Get("username")
email := r.Header.Get("email")
if username == "" || email == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err := srv.addUser(username, email)
if err != nil {
srv.E("Error adding user: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Error(w, http.StatusText(http.StatusCreated), http.StatusCreated)
}
// DeleteUser from database.
func (srv *Shoutyface) DeleteUser(w http.ResponseWriter, r *http.Request) {
username := r.Header.Get("username")
if username == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err := srv.rmUser(username)
if err != nil {
srv.E("Error deleting user: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}