|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 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
- }
-
- var resp CreateQueueResponse
- if claims != nil && claims.Privileges <= Admin {
- 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
- }
- } else {
- w.WriteHeader(403)
- return
- }
-
- err = json.NewEncoder(w).Encode(resp)
- if err != nil {
- log.Error().Err(err).Msg("Could not deliver response in CreateQueue call")
- }
- }
|