tariffs/internal/controller/tariff_external/controller.go

99 lines
2.7 KiB
Go
Raw Normal View History

2024-07-23 11:27:56 +00:00
package tariff_external
import (
2024-07-25 11:06:49 +00:00
"errors"
2024-07-23 11:27:56 +00:00
"github.com/gofiber/fiber/v2"
2024-07-25 11:06:49 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2024-07-23 11:27:56 +00:00
"go.uber.org/zap"
2024-07-25 11:49:50 +00:00
"hub_admin_backend_service/internal/controller/middleware"
2024-07-25 11:06:49 +00:00
our_errors "hub_admin_backend_service/internal/errors"
"hub_admin_backend_service/internal/models"
2024-07-25 10:51:27 +00:00
"hub_admin_backend_service/internal/repository/tariff"
2024-07-26 11:38:54 +00:00
"hub_admin_backend_service/internal/tools"
2024-07-23 11:27:56 +00:00
)
type Deps struct {
2024-07-25 11:49:50 +00:00
Repo *tariff.Tariff
Logger *zap.Logger
MiddleWare *middleware.MiddleWare
2024-07-23 11:27:56 +00:00
}
type TariffExternal struct {
2024-07-25 11:49:50 +00:00
repo *tariff.Tariff
logger *zap.Logger
middleWare *middleware.MiddleWare
2024-07-23 11:27:56 +00:00
}
func NewTariffExternal(deps Deps) *TariffExternal {
return &TariffExternal{
2024-07-25 11:49:50 +00:00
repo: deps.Repo,
logger: deps.Logger,
middleWare: deps.MiddleWare,
2024-07-23 11:27:56 +00:00
}
}
func (t *TariffExternal) Get(ctx *fiber.Ctx) error {
2024-07-25 11:06:49 +00:00
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)
2024-07-23 11:27:56 +00:00
}
func (t *TariffExternal) GetList(ctx *fiber.Ctx) error {
2024-07-25 14:10:01 +00:00
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)
2024-07-23 11:27:56 +00:00
}
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
2024-07-26 11:38:54 +00:00
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()})
}
2024-07-25 14:10:01 +00:00
return ctx.Status(fiber.StatusOK).JSON(result)
2024-07-23 11:27:56 +00:00
}