added create methods for tariff users and admmins

This commit is contained in:
Pavel 2024-07-25 15:43:43 +03:00
parent c3d9e2a0d3
commit 1324c917ce
5 changed files with 54 additions and 23 deletions

@ -7,6 +7,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"hub_admin_backend_service/internal/controller/middleware" "hub_admin_backend_service/internal/controller/middleware"
our_errors "hub_admin_backend_service/internal/errors" 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/repository/tariff"
) )
@ -59,5 +60,21 @@ func (t *TariffExternal) GetList(ctx *fiber.Ctx) error {
} }
func (t *TariffExternal) Create(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)
} }

@ -62,11 +62,17 @@ func (t *TariffInternal) GetList(ctx *fiber.Ctx) error {
} }
func (t *TariffInternal) Create(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 { if err := ctx.BodyParser(&req); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"}) 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 { func (t *TariffInternal) Delete(ctx *fiber.Ctx) error {

@ -18,13 +18,3 @@ type TariffPagination struct {
TotalPages int `json:"totalPages"` TotalPages int `json:"totalPages"`
Tariffs []Tariff `json:"tariffs"` 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"`
}

@ -6,14 +6,16 @@ import (
) )
type Tariff struct { type Tariff struct {
ID primitive.ObjectID `json:"_id" bson:"_id"` ID primitive.ObjectID `json:"_id" bson:"_id"`
Name string `json:"name" bson:"name"` Name string `json:"name" bson:"name"`
UserID string `json:"userID" bson:"userID"` UserID string `json:"userID" bson:"userID"`
Price int `json:"price" bson:"price"` Description string `json:"description" bson:"description"`
IsCustom bool `json:"isCustom" bson:"isCustom"` Price int `json:"price" bson:"price"`
Privileges []Privilege `json:"privileges" bson:"privileges"` Order int `json:"order" bson:"order"`
IsDeleted bool `json:"isDeleted" bson:"isDeleted"` IsCustom bool `json:"isCustom" bson:"isCustom"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` Privileges []Privilege `json:"privileges" bson:"privileges"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` IsDeleted bool `json:"isDeleted" bson:"isDeleted"`
DeletedAt time.Time `json:"deletedAt" bson:"deletedAt"` CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
DeletedAt time.Time `json:"deletedAt" bson:"deletedAt"`
} }

@ -8,6 +8,7 @@ import (
"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"
"time"
) )
type Deps struct { type Deps struct {
@ -45,3 +46,18 @@ func (t *Tariff) GetByID(ctx context.Context, id primitive.ObjectID) (models.Tar
return tariff, nil 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
}