From a1ec819de751931d9f6e3f1fc967f1d5c9b7b7cb Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Jul 2024 14:06:49 +0300 Subject: [PATCH] added getting controllers for tariffs --- .../controller/tariff_external/controller.go | 25 ++++++++++++++++++- .../controller/tariff_internal/controller.go | 25 ++++++++++++++++--- internal/repository/tariff/tariff.go | 24 ++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/internal/controller/tariff_external/controller.go b/internal/controller/tariff_external/controller.go index 33665cc..7215f44 100644 --- a/internal/controller/tariff_external/controller.go +++ b/internal/controller/tariff_external/controller.go @@ -1,8 +1,11 @@ package tariff_external import ( + "errors" "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/bson/primitive" "go.uber.org/zap" + our_errors "hub_admin_backend_service/internal/errors" "hub_admin_backend_service/internal/repository/tariff" ) @@ -24,7 +27,27 @@ func NewTariffExternal(deps Deps) *TariffExternal { } func (t *TariffExternal) Get(ctx *fiber.Ctx) error { - return nil + tariffID := ctx.Params("id") + if tariffID == "" { + return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "url field id don't be empty"}) + } + + objID, err := primitive.ObjectIDFromHex(tariffID) + if err != nil { + return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "no valid object tariff id"}) + } + + result, err := t.repo.GetByID(ctx.Context(), objID) + if err != nil { + switch { + case errors.Is(err, our_errors.ErrNotFound): + return ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Tariff not found"}) + default: + return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()}) + } + } + + return ctx.Status(fiber.StatusOK).JSON(result) } func (t *TariffExternal) GetList(ctx *fiber.Ctx) error { diff --git a/internal/controller/tariff_internal/controller.go b/internal/controller/tariff_internal/controller.go index 8812505..a181a5d 100644 --- a/internal/controller/tariff_internal/controller.go +++ b/internal/controller/tariff_internal/controller.go @@ -1,8 +1,11 @@ package tariff_internal import ( + "errors" "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/bson/primitive" "go.uber.org/zap" + our_errors "hub_admin_backend_service/internal/errors" "hub_admin_backend_service/internal/models" "hub_admin_backend_service/internal/repository/tariff" ) @@ -32,12 +35,22 @@ func (t *TariffInternal) Get(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "url field id don't be empty"}) } - var req models.CreateUpdateTariff - if err := ctx.BodyParser(&req); err != nil { - return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"}) + objID, err := primitive.ObjectIDFromHex(tariffID) + if err != nil { + return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "no valid object tariff id"}) } - return nil + result, err := t.repo.GetByID(ctx.Context(), objID) + if err != nil { + switch { + case errors.Is(err, our_errors.ErrNotFound): + return ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Tariff not found"}) + default: + return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()}) + } + } + + return ctx.Status(fiber.StatusOK).JSON(result) } func (t *TariffInternal) GetList(ctx *fiber.Ctx) error { @@ -45,6 +58,10 @@ func (t *TariffInternal) GetList(ctx *fiber.Ctx) error { } func (t *TariffInternal) Create(ctx *fiber.Ctx) error { + var req models.CreateUpdateTariff + if err := ctx.BodyParser(&req); err != nil { + return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"}) + } return nil } diff --git a/internal/repository/tariff/tariff.go b/internal/repository/tariff/tariff.go index 4c290aa..d93e9f2 100644 --- a/internal/repository/tariff/tariff.go +++ b/internal/repository/tariff/tariff.go @@ -1,8 +1,13 @@ package tariff import ( + "context" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/zap" + "hub_admin_backend_service/internal/errors" + "hub_admin_backend_service/internal/models" ) type Deps struct { @@ -21,3 +26,22 @@ func NewTariffRepo(deps Deps) *Tariff { logger: deps.Logger, } } + +func (t *Tariff) GetByID(ctx context.Context, id primitive.ObjectID) (models.Tariff, error) { + var tariff models.Tariff + filter := bson.M{ + "_id": id, + "isDeleted": false, + } + + err := t.mdb.FindOne(ctx, filter).Decode(&tariff) + if err != nil { + if err == mongo.ErrNoDocuments { + return tariff, errors.ErrNotFound + } + t.logger.Error("failed to get tariff by object id", zap.Error(err)) + return tariff, err + } + + return tariff, nil +}