Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

118 rader
3.1 KiB

  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log/slog"
  6. "net/http"
  7. "strconv"
  8. "github.com/AFASystems/presence/internal/pkg/api/response"
  9. "github.com/AFASystems/presence/internal/pkg/model"
  10. "github.com/AFASystems/presence/internal/pkg/validation"
  11. "github.com/gorilla/mux"
  12. "gorm.io/gorm"
  13. )
  14. func GatewayAddController(db *gorm.DB, context context.Context) http.HandlerFunc {
  15. return func(w http.ResponseWriter, r *http.Request) {
  16. var gateway model.Gateway
  17. if err := json.NewDecoder(r.Body).Decode(&gateway); err != nil {
  18. response.BadRequest(w, "invalid request body")
  19. return
  20. }
  21. if err := validation.Struct(&gateway); err != nil {
  22. response.BadRequest(w, err.Error())
  23. return
  24. }
  25. if err := db.WithContext(context).Create(&gateway).Error; err != nil {
  26. response.InternalError(w, "failed to create gateway", err)
  27. return
  28. }
  29. response.JSON(w, http.StatusCreated, map[string]string{"status": "created"})
  30. }
  31. }
  32. func GatewayListController(db *gorm.DB, context context.Context) http.HandlerFunc {
  33. return func(w http.ResponseWriter, r *http.Request) {
  34. query := r.URL.Query()
  35. lStr := query.Get("limit")
  36. if lStr == "" {
  37. lStr = "100"
  38. }
  39. limit, err := strconv.Atoi(lStr)
  40. if err != nil {
  41. slog.Error("invalid limit parameter", "value", lStr)
  42. response.BadRequest(w, "invalid limit parameter")
  43. return
  44. }
  45. oStr := query.Get("offset")
  46. if oStr == "" {
  47. oStr = "0"
  48. }
  49. offset, err := strconv.Atoi(oStr)
  50. if err != nil {
  51. slog.Error("invalid offset parameter", "value", oStr)
  52. response.BadRequest(w, "invalid offset parameter")
  53. return
  54. }
  55. var gateways []model.Gateway
  56. if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&gateways).Error; err != nil {
  57. response.InternalError(w, "failed to list gateways", err)
  58. return
  59. }
  60. response.JSON(w, http.StatusOK, gateways)
  61. }
  62. }
  63. func GatewayDeleteController(db *gorm.DB, context context.Context) http.HandlerFunc {
  64. return func(w http.ResponseWriter, r *http.Request) {
  65. id := mux.Vars(r)["id"]
  66. res := db.WithContext(context).Delete(&model.Gateway{}, "id = ?", id)
  67. if res.RowsAffected == 0 {
  68. response.NotFound(w, "gateway not found")
  69. return
  70. }
  71. if res.Error != nil {
  72. response.InternalError(w, "failed to delete gateway", res.Error)
  73. return
  74. }
  75. response.JSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  76. }
  77. }
  78. func GatewayUpdateController(db *gorm.DB, context context.Context) http.HandlerFunc {
  79. return func(w http.ResponseWriter, r *http.Request) {
  80. id := mux.Vars(r)["id"]
  81. if err := db.WithContext(context).First(&model.Gateway{}, "id = ?", id).Error; err != nil {
  82. response.NotFound(w, "gateway not found")
  83. return
  84. }
  85. var gateway model.Gateway
  86. if err := json.NewDecoder(r.Body).Decode(&gateway); err != nil {
  87. response.BadRequest(w, "invalid request body")
  88. return
  89. }
  90. if err := validation.Struct(&gateway); err != nil {
  91. response.BadRequest(w, err.Error())
  92. return
  93. }
  94. if err := db.WithContext(context).Save(&gateway).Error; err != nil {
  95. response.InternalError(w, "failed to update gateway", err)
  96. return
  97. }
  98. response.JSON(w, http.StatusOK, map[string]string{"status": "updated"})
  99. }
  100. }