added getting controllers for tariffs
This commit is contained in:
parent
c8f2b77b93
commit
a1ec819de7
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user