update_thing_notification.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package notification
  2. import (
  3. "database/sql"
  4. "errors"
  5. "git.dmitriygnatenko.ru/dima/go-common/logger"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/gofiber/fiber/v2"
  8. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers/location"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  12. )
  13. // @Router /api/v1/things/notifications/{thingId} [put]
  14. // @Param thingId path int true "Thing ID"
  15. // @Param data body dto.UpdateThingNotificationRequest true "Request body"
  16. // @Success 200 {object} dto.ThingNotificationResponse
  17. // @Failure 400 {object} dto.ErrorResponse
  18. // @Failure 500 {object} dto.ErrorResponse
  19. // @Summary Update thing notification
  20. // @Tags Notifications
  21. // @security APIKey
  22. // @Accept json
  23. // @Produce json
  24. func UpdateThingNotificationHandler(
  25. thingNotificationRepository ThingNotificationRepository,
  26. ) fiber.Handler {
  27. return func(fctx *fiber.Ctx) error {
  28. ctx := fctx.Context()
  29. id, err := fctx.ParamsInt("thingId")
  30. if err != nil {
  31. logger.Info(ctx, err.Error())
  32. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  33. }
  34. req := dto.UpdateThingNotificationRequest{}
  35. if err = fctx.BodyParser(&req); err != nil {
  36. logger.Info(ctx, err.Error())
  37. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  38. }
  39. var validate = validator.New()
  40. if err = validate.Struct(req); err != nil {
  41. logger.Info(ctx, err.Error())
  42. return fctx.Status(fiber.StatusBadRequest).JSON(factory.CreateValidateErrorResponse(err))
  43. }
  44. _, err = thingNotificationRepository.Get(ctx, uint64(id))
  45. if err != nil {
  46. if errors.Is(err, sql.ErrNoRows) {
  47. logger.Info(ctx, err.Error())
  48. return fiber.NewError(fiber.StatusBadRequest, "")
  49. }
  50. logger.Error(ctx, err.Error())
  51. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  52. }
  53. dbReq, err := mappers.ToUpdateThingNotificationRequest(uint64(id), req)
  54. if err != nil {
  55. logger.Info(ctx, err.Error())
  56. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  57. }
  58. if err = thingNotificationRepository.Update(ctx, *dbReq); err != nil {
  59. logger.Error(ctx, err.Error())
  60. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  61. }
  62. res, err := thingNotificationRepository.Get(ctx, uint64(id))
  63. if err != nil {
  64. logger.Error(ctx, err.Error())
  65. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  66. }
  67. res = location.ApplyLocation(fctx, res)
  68. return fctx.JSON(mappers.ToThingNotificationResponse(*res))
  69. }
  70. }