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.
 
 
 
 

92 line
2.6 KiB

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