add_thing_notification.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package notification
  2. //go:generate mkdir -p mocks
  3. //go:generate rm -rf ./mocks/*_minimock.go
  4. //go:generate minimock -i ThingNotificationRepository -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "context"
  7. "database/sql"
  8. "github.com/go-playground/validator/v10"
  9. "github.com/gofiber/fiber/v2"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/repositories"
  16. )
  17. type (
  18. ThingNotificationRepository interface {
  19. Add(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) error
  20. Update(ctx context.Context, req models.UpdateThingNotificationRequest, tx *sql.Tx) error
  21. Delete(ctx context.Context, thingID int, tx *sql.Tx) error
  22. Get(ctx context.Context, thingID int) (*models.ThingNotification, error)
  23. GetExpired(ctx context.Context) ([]models.ExtThingNotification, error)
  24. }
  25. )
  26. // @Router /api/v1/things/notifications [post]
  27. // @Param data body dto.AddThingNotificationRequest true "Request body"
  28. // @Success 200 {object} dto.ThingNotificationResponse
  29. // @Failure 400 {object} dto.ErrorResponse
  30. // @Failure 500 {object} dto.ErrorResponse
  31. // @Summary Add thing notification
  32. // @Tags Notifications
  33. // @security APIKey
  34. // @Accept json
  35. // @Produce json
  36. func AddThingNotificationHandler(
  37. thingNotificationRepository ThingNotificationRepository,
  38. ) fiber.Handler {
  39. return func(fctx *fiber.Ctx) error {
  40. ctx := fctx.Context()
  41. req := dto.AddThingNotificationRequest{}
  42. if err := fctx.BodyParser(&req); err != nil {
  43. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  44. }
  45. var validate = validator.New()
  46. if err := validate.Struct(req); err != nil {
  47. return fctx.Status(fiber.StatusBadRequest).JSON(factory.CreateValidateErrorResponse(err))
  48. }
  49. dbReq, err := mappers.ToAddThingNotificationRequest(req)
  50. if err != nil {
  51. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  52. }
  53. if err = thingNotificationRepository.Add(ctx, *dbReq, nil); err != nil {
  54. if repositories.IsFKViolationError(err) || repositories.IsDuplicateKeyError(err) {
  55. return fiber.NewError(fiber.StatusBadRequest, "")
  56. }
  57. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  58. }
  59. res, err := thingNotificationRepository.Get(ctx, req.ThingID)
  60. if err != nil {
  61. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  62. }
  63. res = helpers.ApplyLocation(fctx, res)
  64. return fctx.JSON(mappers.ToThingNotificationResponse(*res))
  65. }
  66. }