add method for create many privileges
This commit is contained in:
parent
b3a689e447
commit
1df10f9e33
@ -163,7 +163,23 @@ func (p *PrivilegeInternal) PostMany(c *fiber.Ctx) error {
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||
}
|
||||
return c.SendStatus(http.StatusCreated)
|
||||
|
||||
if len(req.Privileges) == 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "len array dont be 0"})
|
||||
}
|
||||
|
||||
for _, priv := range req.Privileges {
|
||||
if !tools.Validate(priv) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
||||
}
|
||||
}
|
||||
|
||||
result, err := p.repo.PostMany(c.Context(), req)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(result)
|
||||
}
|
||||
|
||||
// хаб нода replacePrivileges
|
||||
|
@ -191,3 +191,35 @@ func (p *Privilege) GetByServiceKey(ctx context.Context, serviceKey string) ([]m
|
||||
|
||||
return privileges, nil
|
||||
}
|
||||
|
||||
func (p *Privilege) PostMany(ctx context.Context, req models.ManyCreateUpdate) ([]models.Privilege, error) {
|
||||
var privileges []interface{}
|
||||
for _, r := range req.Privileges {
|
||||
privileges = append(privileges, models.Privilege{
|
||||
ID: primitive.NewObjectID(),
|
||||
Name: r.Name,
|
||||
PrivilegeID: r.PrivilegeId,
|
||||
ServiceKey: r.ServiceKey,
|
||||
Description: r.Description,
|
||||
Type: r.Type,
|
||||
Value: r.Value,
|
||||
Price: r.Price,
|
||||
CreatedAt: time.Now(),
|
||||
IsDeleted: false,
|
||||
})
|
||||
}
|
||||
|
||||
result, err := p.mdb.InsertMany(ctx, privileges)
|
||||
if err != nil {
|
||||
p.logger.Error("failed to insert privileges", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
inserted := make([]models.Privilege, len(result.InsertedIDs))
|
||||
for i, id := range result.InsertedIDs {
|
||||
inserted[i] = privileges[i].(models.Privilege)
|
||||
inserted[i].ID = id.(primitive.ObjectID)
|
||||
}
|
||||
|
||||
return inserted, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user