Checkpoint
This commit is contained in:
72
admin/api_group.go
Normal file
72
admin/api_group.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.maze.io/maze/styx/dataset"
|
||||
)
|
||||
|
||||
func (a *Admin) apiGroups(w http.ResponseWriter, r *http.Request) {
|
||||
groups, err := a.Storage.Groups()
|
||||
if err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
a.jsonResponse(w, r, groups)
|
||||
}
|
||||
|
||||
func (a *Admin) apiGroup(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
group, err := a.Storage.GroupByID(id)
|
||||
if err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
a.jsonResponse(w, r, group)
|
||||
}
|
||||
|
||||
func (a *Admin) apiGroupCreate(w http.ResponseWriter, r *http.Request) {
|
||||
var request struct {
|
||||
dataset.Group
|
||||
ID int64 `json:"id"` // mask, not used
|
||||
CreatedAt time.Time `json:"created_at"` // mask, not used
|
||||
UpdatedAt time.Time `json:"updated_at"` // mask, not used
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
if err := a.Storage.SaveGroup(&request.Group); err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
a.jsonResponse(w, r, request.Group, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (a *Admin) apiGroupUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (a *Admin) apiGroupDelete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
group, err := a.Storage.GroupByID(id)
|
||||
if err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
if err = a.Storage.DeleteGroup(group); err != nil {
|
||||
a.handleAPIError(w, r, err)
|
||||
return
|
||||
}
|
||||
a.jsonResponse(w, r, nil)
|
||||
}
|
Reference in New Issue
Block a user