|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package endpoints
-
- import (
- "encoding/json"
- "net/http"
-
- "github.com/imosed/signet/auth"
- . "github.com/imosed/signet/data"
- "github.com/rs/zerolog/log"
- )
-
- type CreateQueueRequest struct {
- Name string `json:"name"`
- }
-
- type CreateQueueResponse struct {
- ID uint `json:"id"`
- }
-
- func CreateQueue(w http.ResponseWriter, r *http.Request) {
- var req CreateQueueRequest
- err := json.NewDecoder(r.Body).Decode(&req)
- if err != nil {
- log.Error().Err(err).Msg("Could not decode body in CreateQueue call")
- return
- }
-
- var claims *auth.Claims
- claims, err = auth.GetUserClaims(r)
- if err != nil {
- log.Error().Err(err).Msg("Could not determine if user is authenticated")
- return
- }
-
- if claims == nil {
- w.WriteHeader(401)
- return
- }
- if claims.Privileges > Admin {
- w.WriteHeader(403)
- return
- }
-
- var resp CreateQueueResponse
- var specificQueue Queue
- Db.Table("queues").First(&specificQueue, "name = ?", req.Name)
-
- if specificQueue.ID != 0 {
- resp.ID = specificQueue.ID
- } else {
- queue := Queue{
- Name: req.Name,
- }
-
- Db.Create(&queue)
-
- resp.ID = queue.ID
- }
-
- err = json.NewEncoder(w).Encode(resp)
- if err != nil {
- log.Error().Err(err).Msg("Could not deliver response in CreateQueue call")
- }
- }
|