update_thing_notification.go 2.6 KB

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