From 1324c917ce90272a5792a7e59ad4e69771f0af8e Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Jul 2024 15:43:43 +0300 Subject: [PATCH] added create methods for tariff users and admmins --- .../controller/tariff_external/controller.go | 19 +++++++++++++++- .../controller/tariff_internal/controller.go | 10 +++++++-- internal/models/reqBodies.go | 10 --------- internal/models/tariff.go | 22 ++++++++++--------- internal/repository/tariff/tariff.go | 16 ++++++++++++++ 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/internal/controller/tariff_external/controller.go b/internal/controller/tariff_external/controller.go index 130365d..abe2e35 100644 --- a/internal/controller/tariff_external/controller.go +++ b/internal/controller/tariff_external/controller.go @@ -7,6 +7,7 @@ import ( "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" ) @@ -59,5 +60,21 @@ func (t *TariffExternal) GetList(ctx *fiber.Ctx) error { } func (t *TariffExternal) Create(ctx *fiber.Ctx) error { - return nil + 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 + 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.StatusCreated).JSON(result) } diff --git a/internal/controller/tariff_internal/controller.go b/internal/controller/tariff_internal/controller.go index acc045e..d432964 100644 --- a/internal/controller/tariff_internal/controller.go +++ b/internal/controller/tariff_internal/controller.go @@ -62,11 +62,17 @@ func (t *TariffInternal) GetList(ctx *fiber.Ctx) error { } func (t *TariffInternal) Create(ctx *fiber.Ctx) error { - var req models.CreateUpdateTariff + var req models.Tariff if err := ctx.BodyParser(&req); err != nil { return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"}) } - return nil + + 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.StatusCreated).JSON(result) } func (t *TariffInternal) Delete(ctx *fiber.Ctx) error { diff --git a/internal/models/reqBodies.go b/internal/models/reqBodies.go index a98952b..7ea4f1e 100644 --- a/internal/models/reqBodies.go +++ b/internal/models/reqBodies.go @@ -18,13 +18,3 @@ type TariffPagination struct { TotalPages int `json:"totalPages"` Tariffs []Tariff `json:"tariffs"` } - -type CreateUpdateTariff struct { - Name string `json:"name"` - UserID string `json:"userId"` - Description string `json:"description"` - Price int `json:"price"` - Order int `json:"order"` - IsCustom bool `json:"isCustom"` - Privileges []Privilege `json:"privileges"` -} diff --git a/internal/models/tariff.go b/internal/models/tariff.go index c395864..9309152 100644 --- a/internal/models/tariff.go +++ b/internal/models/tariff.go @@ -6,14 +6,16 @@ import ( ) type Tariff struct { - ID primitive.ObjectID `json:"_id" bson:"_id"` - Name string `json:"name" bson:"name"` - UserID string `json:"userID" bson:"userID"` - Price int `json:"price" bson:"price"` - IsCustom bool `json:"isCustom" bson:"isCustom"` - Privileges []Privilege `json:"privileges" bson:"privileges"` - IsDeleted bool `json:"isDeleted" bson:"isDeleted"` - CreatedAt time.Time `json:"createdAt" bson:"createdAt"` - UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` - DeletedAt time.Time `json:"deletedAt" bson:"deletedAt"` + ID primitive.ObjectID `json:"_id" bson:"_id"` + Name string `json:"name" bson:"name"` + UserID string `json:"userID" bson:"userID"` + Description string `json:"description" bson:"description"` + Price int `json:"price" bson:"price"` + Order int `json:"order" bson:"order"` + IsCustom bool `json:"isCustom" bson:"isCustom"` + Privileges []Privilege `json:"privileges" bson:"privileges"` + IsDeleted bool `json:"isDeleted" bson:"isDeleted"` + CreatedAt time.Time `json:"createdAt" bson:"createdAt"` + UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` + DeletedAt time.Time `json:"deletedAt" bson:"deletedAt"` } diff --git a/internal/repository/tariff/tariff.go b/internal/repository/tariff/tariff.go index d93e9f2..2265d91 100644 --- a/internal/repository/tariff/tariff.go +++ b/internal/repository/tariff/tariff.go @@ -8,6 +8,7 @@ import ( "go.uber.org/zap" "hub_admin_backend_service/internal/errors" "hub_admin_backend_service/internal/models" + "time" ) type Deps struct { @@ -45,3 +46,18 @@ func (t *Tariff) GetByID(ctx context.Context, id primitive.ObjectID) (models.Tar return tariff, nil } + +func (t *Tariff) Create(ctx context.Context, req models.Tariff) (models.Tariff, error) { + req.ID = primitive.NewObjectID() + req.CreatedAt = time.Now() + req.IsDeleted = false + req.UpdatedAt = time.Now() + + _, err := t.mdb.InsertOne(ctx, req) + if err != nil { + t.logger.Error("failed insert tariff", zap.Error(err)) + return req, err + } + + return req, nil +}