added methods for pagination tariffs
This commit is contained in:
parent
1324c917ce
commit
91b67483b8
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"hub_admin_backend_service/internal/models"
|
"hub_admin_backend_service/internal/models"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MiddleWare struct {
|
type MiddleWare struct {
|
||||||
@ -25,3 +26,19 @@ func (mw *MiddleWare) ExtractToken(ctx *fiber.Ctx) (string, bool) {
|
|||||||
token, ok := ctx.Context().UserValue(models.AuthJWTDecodedAccessTokenKey).(string)
|
token, ok := ctx.Context().UserValue(models.AuthJWTDecodedAccessTokenKey).(string)
|
||||||
return token, ok
|
return token, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mw *MiddleWare) GetPaginationParameters(ctx *fiber.Ctx) (int64, int64) {
|
||||||
|
page := int64(1)
|
||||||
|
limit := int64(25)
|
||||||
|
if p := ctx.Query("page"); p != "" {
|
||||||
|
if parPage, err := strconv.ParseInt(p, 10, 64); err == nil {
|
||||||
|
page = parPage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if l := ctx.Query("limit"); l != "" {
|
||||||
|
if parLimit, err := strconv.ParseInt(l, 10, 64); err == nil {
|
||||||
|
limit = parLimit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return page, limit
|
||||||
|
}
|
||||||
|
@ -56,7 +56,19 @@ func (t *TariffExternal) Get(ctx *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TariffExternal) GetList(ctx *fiber.Ctx) error {
|
func (t *TariffExternal) GetList(ctx *fiber.Ctx) error {
|
||||||
return nil
|
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 {
|
func (t *TariffExternal) Create(ctx *fiber.Ctx) error {
|
||||||
@ -76,5 +88,5 @@ func (t *TariffExternal) Create(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Status(fiber.StatusCreated).JSON(result)
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,13 @@ func (t *TariffInternal) Get(ctx *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TariffInternal) GetList(ctx *fiber.Ctx) error {
|
func (t *TariffInternal) GetList(ctx *fiber.Ctx) error {
|
||||||
return nil
|
page, limit := t.middleWare.GetPaginationParameters(ctx)
|
||||||
|
result, err := t.repo.GetList(ctx.Context(), page, limit, "")
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TariffInternal) Create(ctx *fiber.Ctx) error {
|
func (t *TariffInternal) Create(ctx *fiber.Ctx) error {
|
||||||
@ -72,7 +78,7 @@ func (t *TariffInternal) Create(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Status(fiber.StatusCreated).JSON(result)
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TariffInternal) Delete(ctx *fiber.Ctx) error {
|
func (t *TariffInternal) Delete(ctx *fiber.Ctx) error {
|
||||||
|
@ -5,9 +5,11 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"hub_admin_backend_service/internal/errors"
|
"hub_admin_backend_service/internal/errors"
|
||||||
"hub_admin_backend_service/internal/models"
|
"hub_admin_backend_service/internal/models"
|
||||||
|
"math"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -61,3 +63,43 @@ func (t *Tariff) Create(ctx context.Context, req models.Tariff) (models.Tariff,
|
|||||||
|
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tariff) GetList(ctx context.Context, page, limit int64, userID string) (models.TariffPagination, error) {
|
||||||
|
var result models.TariffPagination
|
||||||
|
filter := bson.M{
|
||||||
|
"$or": []bson.M{
|
||||||
|
{"isCustom": false, "isDeleted": false},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if userID != "" {
|
||||||
|
filter["$or"] = append(filter["$or"].([]bson.M), bson.M{"isCustom": true, "userID": userID})
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := t.mdb.CountDocuments(ctx, filter)
|
||||||
|
if err != nil {
|
||||||
|
t.logger.Error("failed count tariffs", zap.Error(err))
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
totalPages := int(math.Ceil(float64(count) / float64(limit)))
|
||||||
|
offset := (page - 1) * limit
|
||||||
|
findOptions := options.Find().SetSort(bson.D{{"order", 1}}).SetSkip(offset).SetLimit(limit)
|
||||||
|
|
||||||
|
cursor, err := t.mdb.Find(ctx, filter, findOptions)
|
||||||
|
if err != nil {
|
||||||
|
t.logger.Error("failed find tariffs", zap.Error(err))
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
defer cursor.Close(ctx)
|
||||||
|
|
||||||
|
var tariffs []models.Tariff
|
||||||
|
if err = cursor.All(ctx, &tariffs); err != nil {
|
||||||
|
t.logger.Error("failed decode tariffs", zap.Error(err))
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result.TotalPages = totalPages
|
||||||
|
result.Tariffs = tariffs
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user