The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.2 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/imosed/signet/auth"
  6. . "github.com/imosed/signet/data"
  7. "github.com/rs/zerolog/log"
  8. )
  9. type CreateQueueRequest struct {
  10. Name string `json:"name"`
  11. }
  12. type CreateQueueResponse struct {
  13. ID uint `json:"id"`
  14. }
  15. func CreateQueue(w http.ResponseWriter, r *http.Request) {
  16. var req CreateQueueRequest
  17. err := json.NewDecoder(r.Body).Decode(&req)
  18. if err != nil {
  19. log.Error().Err(err).Msg("Could not decode body in CreateQueue call")
  20. return
  21. }
  22. var claims *auth.Claims
  23. claims, err = auth.GetUserClaims(r)
  24. if err != nil {
  25. log.Error().Err(err).Msg("Could not determine if user is authenticated")
  26. return
  27. }
  28. if claims == nil {
  29. w.WriteHeader(401)
  30. return
  31. }
  32. if claims.Privileges > Admin {
  33. w.WriteHeader(403)
  34. return
  35. }
  36. var resp CreateQueueResponse
  37. var specificQueue Queue
  38. Db.Table("queues").First(&specificQueue, "name = ?", req.Name)
  39. if specificQueue.ID != 0 {
  40. resp.ID = specificQueue.ID
  41. } else {
  42. queue := Queue{
  43. Name: req.Name,
  44. }
  45. Db.Create(&queue)
  46. resp.ID = queue.ID
  47. }
  48. err = json.NewEncoder(w).Encode(resp)
  49. if err != nil {
  50. log.Error().Err(err).Msg("Could not deliver response in CreateQueue call")
  51. }
  52. }