99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package tariff_external
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.uber.org/zap"
|
|
"hub_admin_backend_service/internal/controller/middleware"
|
|
our_errors "hub_admin_backend_service/internal/errors"
|
|
"hub_admin_backend_service/internal/models"
|
|
"hub_admin_backend_service/internal/repository/tariff"
|
|
"hub_admin_backend_service/internal/tools"
|
|
)
|
|
|
|
type Deps struct {
|
|
Repo *tariff.Tariff
|
|
Logger *zap.Logger
|
|
MiddleWare *middleware.MiddleWare
|
|
}
|
|
|
|
type TariffExternal struct {
|
|
repo *tariff.Tariff
|
|
logger *zap.Logger
|
|
middleWare *middleware.MiddleWare
|
|
}
|
|
|
|
func NewTariffExternal(deps Deps) *TariffExternal {
|
|
return &TariffExternal{
|
|
repo: deps.Repo,
|
|
logger: deps.Logger,
|
|
middleWare: deps.MiddleWare,
|
|
}
|
|
}
|
|
|
|
func (t *TariffExternal) Get(ctx *fiber.Ctx) error {
|
|
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 {
|
|
page, limit := t.middleWare.GetPaginationParameters(ctx)
|
|
|
|
userID, ok := t.middleWare.ExtractUserID(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "No auth"})
|
|
}
|
|
|
|
result, err := t.repo.GetList(ctx.Context(), page, limit, userID)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
|
}
|
|
|
|
func (t *TariffExternal) Create(ctx *fiber.Ctx) error {
|
|
userID, ok := t.middleWare.ExtractUserID(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "No auth"})
|
|
}
|
|
|
|
var req models.Tariff
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
}
|
|
req.UserID = userID
|
|
|
|
err := tools.ValidateTariff(req)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
result, err := t.repo.Create(ctx.Context(), req)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
|
}
|