-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_status.go
88 lines (74 loc) · 1.88 KB
/
get_status.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
package handlers
import (
"encoding/json"
"errors"
"net/http"
"github.com/kaus19/online_offline_tracker/api"
"github.com/kaus19/online_offline_tracker/internal/tools"
"github.com/go-redis/redis"
"github.com/gorilla/schema"
log "github.com/sirupsen/logrus"
)
var ErrUnauthorized = errors.New("invalid username or token")
var database *redis.Client
var err error
func InitiateDb() {
database, err = tools.NewDatabase()
if err != nil {
log.Error(err)
return
}
}
func GetUserStatus(w http.ResponseWriter, r *http.Request) {
var params = api.StatusParams{}
var decoder *schema.Decoder = schema.NewDecoder()
err = decoder.Decode(¶ms, r.URL.Query())
if err != nil {
log.Error(err)
api.InternalErrorHandler(w)
return
}
var userDetails *tools.UserDetails = tools.GetUserDetails(database, params.Username)
if userDetails == nil {
log.Error(err)
api.InternalErrorHandler(w)
return
}
var response = api.StatusResponse{
Status: (*userDetails).Status,
Code: http.StatusOK,
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Error(err)
api.InternalErrorHandler(w)
return
}
}
func GetAllStatus(w http.ResponseWriter, r *http.Request) {
var allUserDetails *[]tools.UserDetails = tools.GetAllStatus(database)
if allUserDetails == nil {
log.Error(err)
api.InternalErrorHandler(w)
return
}
var allResponses []api.UserDetailsResponse
var allResponse api.UserDetailsResponse
for _, v := range *allUserDetails {
allResponse.Username = v.Username
allResponse.Status = v.Status
allResponses = append(allResponses, allResponse)
}
var response = api.AllStatusResponse{
AllDetails: allResponses,
Code: http.StatusOK,
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Error(err)
api.InternalErrorHandler(w)
return
}
}