Add users and grades display
This commit is contained in:
parent
42b15a1546
commit
c9d64640e2
13 changed files with 432 additions and 17 deletions
12
users.go
12
users.go
|
@ -2,8 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
@ -17,23 +17,23 @@ func init() {
|
|||
func(u User, _ []byte) HTTPResponse {
|
||||
return APIResponse{u}
|
||||
})))
|
||||
router.PUT("/api/users/:uid", apiHandler(userHandler(updateUser)))
|
||||
router.PUT("/api/users/:uid", apiHandler(userHandler(updateUser), adminRestricted))
|
||||
router.DELETE("/api/users/:uid", apiHandler(userHandler(
|
||||
func(u User, _ []byte) HTTPResponse {
|
||||
return formatApiResponse(u.Delete())
|
||||
})))
|
||||
}), adminRestricted))
|
||||
}
|
||||
|
||||
func userHandler(f func(User, []byte) HTTPResponse) func(httprouter.Params, []byte) HTTPResponse {
|
||||
return func(ps httprouter.Params, body []byte) HTTPResponse {
|
||||
if uid, err := strconv.Atoi(string(ps.ByName("uid"))); err != nil {
|
||||
if user, err := getUserByLogin(ps.ByName("uid")); err != nil {
|
||||
return APIErrorResponse{err:err}
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return f(user, body)
|
||||
}
|
||||
} else if user, err := getUser(uid); err != nil {
|
||||
return APIErrorResponse{err:err}
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return f(user, body)
|
||||
}
|
||||
|
@ -140,7 +140,7 @@ func ClearUsers() (int64, error) {
|
|||
func updateUser(current User, body []byte) HTTPResponse {
|
||||
var new User
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err:err}
|
||||
return APIErrorResponse{err: err}
|
||||
}
|
||||
|
||||
current.Login = new.Login
|
||||
|
|
Reference in a new issue