update_thing.go 2.8 KB

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