add_thing_notification.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "git.dmitriygnatenko.ru/dima/go-common/logger"
  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/location"
  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) error
  20. Update(ctx context.Context, req models.UpdateThingNotificationRequest) error
  21. Delete(ctx context.Context, id uint64) error
  22. Get(ctx context.Context, id uint64) (*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. logger.Info(ctx, err.Error())
  44. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  45. }
  46. var validate = validator.New()
  47. if err := validate.Struct(req); err != nil {
  48. logger.Info(ctx, err.Error())
  49. return fctx.Status(fiber.StatusBadRequest).JSON(factory.CreateValidateErrorResponse(err))
  50. }
  51. dbReq, err := mappers.ToAddThingNotificationRequest(req)
  52. if err != nil {
  53. logger.Info(ctx, err.Error())
  54. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  55. }
  56. if err = thingNotificationRepository.Add(ctx, *dbReq); err != nil {
  57. if repositories.IsFKViolationError(err) || repositories.IsDuplicateKeyError(err) {
  58. logger.Info(ctx, err.Error())
  59. return fiber.NewError(fiber.StatusBadRequest, "")
  60. }
  61. logger.Error(ctx, err.Error())
  62. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  63. }
  64. res, err := thingNotificationRepository.Get(ctx, req.ThingID)
  65. if err != nil {
  66. logger.Error(ctx, err.Error())
  67. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  68. }
  69. res = location.ApplyLocation(fctx, res)
  70. return fctx.JSON(mappers.ToThingNotificationResponse(*res))
  71. }
  72. }