update_thing.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package thing
  2. import (
  3. "github.com/go-playground/validator/v10"
  4. "github.com/gofiber/fiber/v2"
  5. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  7. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  8. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  10. )
  11. // @Router /api/v1/things/{thingId} [put]
  12. // @Param thingId path int true "Thing ID"
  13. // @Param data body dto.UpdateThingRequest true "Request body"
  14. // @Success 200 {object} dto.ThingResponse
  15. // @Failure 400 {object} dto.ErrorResponse
  16. // @Failure 500 {object} dto.ErrorResponse
  17. // @Summary Update thing
  18. // @Tags Things
  19. // @security APIKey
  20. // @Accept json
  21. // @Produce json
  22. func UpdateThingHandler(
  23. thingRepository ThingRepository,
  24. placeThingRepository PlaceThingRepository,
  25. ) fiber.Handler {
  26. return func(fctx *fiber.Ctx) error {
  27. ctx := fctx.Context()
  28. id, err := fctx.ParamsInt("thingId")
  29. if err != nil {
  30. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  31. }
  32. req := dto.UpdateThingRequest{}
  33. if err = fctx.BodyParser(&req); err != nil {
  34. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  35. }
  36. var validate = validator.New()
  37. if err = validate.Struct(req); err != nil {
  38. return fctx.Status(fiber.StatusBadRequest).JSON(factory.CreateValidateErrorResponse(err))
  39. }
  40. thing, err := thingRepository.Get(ctx, id)
  41. if err != nil {
  42. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  43. }
  44. placeThing, err := placeThingRepository.GetByThingID(ctx, id)
  45. if err != nil {
  46. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  47. }
  48. tx, err := thingRepository.BeginTx(ctx, API.DefaultTxLevel)
  49. if err != nil {
  50. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  51. }
  52. if req.Title != thing.Title || req.Description != thing.Description {
  53. err = thingRepository.Update(ctx, mappers.ToUpdateThingRequest(id, req), tx)
  54. if err != nil {
  55. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  56. }
  57. }
  58. if placeThing.PlaceID != req.PlaceID {
  59. err = placeThingRepository.UpdatePlace(ctx, mappers.ToUpdatePlaceThingRequest(id, req.PlaceID), tx)
  60. if err != nil {
  61. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  62. }
  63. }
  64. if err = thingRepository.CommitTx(tx); err != nil {
  65. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  66. }
  67. res, err := thingRepository.Get(ctx, id)
  68. if err != nil {
  69. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  70. }
  71. res = helpers.ApplyLocation(fctx, res)
  72. return fctx.JSON(mappers.ToThingResponse(*res))
  73. }
  74. }