-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
57 lines (45 loc) · 1.3 KB
/
query.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
package query
import (
"encoding/json"
"net/http"
"github.com/WeisswurstSystems/WWM-BB/user"
"github.com/WeisswurstSystems/WWM-BB/user/usecase/authenticate"
"github.com/WeisswurstSystems/WWM-BB/wwm"
)
type QueryHandler struct {
Store user.ReadStore
authenticate.AuthenticateUseCase
}
// FindAll Users if authenticated with Basic Authentication. The Users are represented with PublicUser(s).
func (ch *QueryHandler) FindAll(w http.ResponseWriter, req *http.Request) error {
var e user.Login
var ok bool
e.Mail, e.Password, ok = req.BasicAuth()
if !ok {
return wwm.Error{Message: "Unauthenticated", Code: http.StatusUnauthorized}
}
_, err := ch.AuthenticateUseCase.Authenticate(e)
if err != nil {
return err
}
users, err := ch.Store.FindAll()
if err != nil {
return err
}
var results []user.PublicUser
for _, u := range users {
results = append(results, u.PublicUser())
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(results)
}
func (ch *QueryHandler) Identity(w http.ResponseWriter, req *http.Request) error {
var e user.Login
e.Mail, e.Password, _ = req.BasicAuth()
results, err := ch.AuthenticateUseCase.Authenticate(e)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(results)
}