API endpoints.
This commit is contained in:
parent
1e283e7cd6
commit
e1685bd32b
8 changed files with 129 additions and 11 deletions
42
api/users.go
Normal file
42
api/users.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func serveUser(w http.ResponseWriter, r *http.Request) error {
|
||||
id, err := strconv.ParseInt(mux.Vars(r)["ID"], 10, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := store.Users.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, user)
|
||||
}
|
||||
|
||||
func serveUsers(w http.ResponseWriter, r *http.Request) error {
|
||||
var opt models.UserListOptions
|
||||
if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
users, err := store.Users.List(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if users == nil {
|
||||
users = []*models.User{}
|
||||
}
|
||||
|
||||
return writeJSON(w, users)
|
||||
}
|
Reference in a new issue