diff --git a/internal/controller/middleware/middleware.go b/internal/controller/middleware/middleware.go index 11592fd..afd8d28 100644 --- a/internal/controller/middleware/middleware.go +++ b/internal/controller/middleware/middleware.go @@ -4,6 +4,7 @@ import ( "github.com/gofiber/fiber/v2" "go.uber.org/zap" "hub_admin_backend_service/internal/models" + "strconv" ) type MiddleWare struct { @@ -25,3 +26,19 @@ func (mw *MiddleWare) ExtractToken(ctx *fiber.Ctx) (string, bool) { token, ok := ctx.Context().UserValue(models.AuthJWTDecodedAccessTokenKey).(string) 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 +} diff --git a/internal/controller/tariff_external/controller.go b/internal/controller/tariff_external/controller.go index abe2e35..a95b9ad 100644 --- a/internal/controller/tariff_external/controller.go +++ b/internal/controller/tariff_external/controller.go @@ -56,7 +56,19 @@ func (t *TariffExternal) Get(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 { @@ -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.StatusCreated).JSON(result) + return ctx.Status(fiber.StatusOK).JSON(result) } diff --git a/internal/controller/tariff_internal/controller.go b/internal/controller/tariff_internal/controller.go index d432964..d2e2e5b 100644 --- a/internal/controller/tariff_internal/controller.go +++ b/internal/controller/tariff_internal/controller.go @@ -58,7 +58,13 @@ func (t *TariffInternal) Get(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 { @@ -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.StatusCreated).JSON(result) + return ctx.Status(fiber.StatusOK).JSON(result) } func (t *TariffInternal) Delete(ctx *fiber.Ctx) error { diff --git a/internal/repository/tariff/tariff.go b/internal/repository/tariff/tariff.go index 2265d91..32cafa2 100644 --- a/internal/repository/tariff/tariff.go +++ b/internal/repository/tariff/tariff.go @@ -5,9 +5,11 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "go.uber.org/zap" "hub_admin_backend_service/internal/errors" "hub_admin_backend_service/internal/models" + "math" "time" ) @@ -61,3 +63,43 @@ func (t *Tariff) Create(ctx context.Context, req models.Tariff) (models.Tariff, 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 +}