add repo method for update privilege
This commit is contained in:
parent
dc25cc41df
commit
71fd522c46
@ -7,6 +7,7 @@ import (
|
||||
our_errors "hub_admin_backend_service/internal/errors"
|
||||
"hub_admin_backend_service/internal/models"
|
||||
"hub_admin_backend_service/internal/repository/privilege"
|
||||
"hub_admin_backend_service/internal/tools"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -43,7 +44,7 @@ func (p *PrivilegeInternal) Create(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||
}
|
||||
|
||||
if !Validate(req) {
|
||||
if !tools.Validate(req) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
||||
}
|
||||
|
||||
@ -67,11 +68,21 @@ func (p *PrivilegeInternal) Update(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||
}
|
||||
|
||||
if !Validate(req) {
|
||||
if !tools.Validate(req) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
||||
}
|
||||
|
||||
return c.SendStatus(http.StatusOK)
|
||||
result, err := p.repo.Update(c.Context(), &req)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, our_errors.ErrNotFound):
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Privilege not found"})
|
||||
default:
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(result)
|
||||
}
|
||||
|
||||
// хаб нода removePrivilege
|
||||
|
@ -82,3 +82,45 @@ func (p *Privilege) Create(ctx context.Context, req *models.CreateUpdateReq) (*m
|
||||
|
||||
return privilege, nil
|
||||
}
|
||||
|
||||
func (p *Privilege) Update(ctx context.Context, req *models.CreateUpdateReq) (*models.Privilege, error) {
|
||||
exist := &models.Privilege{}
|
||||
err := p.mdb.FindOne(ctx, bson.M{"privilegeId": req.PrivilegeId}).Decode(exist)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, errors.ErrNotFound
|
||||
} else if err != nil {
|
||||
p.logger.Error("Failed to find privilege", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"name": req.Name,
|
||||
"serviceKey": req.ServiceKey,
|
||||
"description": req.Description,
|
||||
"type": req.Type,
|
||||
"value": req.Value,
|
||||
"price": req.Price,
|
||||
"updatedAt": time.Now(),
|
||||
// amount в хабе не вставляется
|
||||
"amount": req.Amount,
|
||||
},
|
||||
}
|
||||
|
||||
_, err = p.mdb.UpdateOne(ctx, bson.M{"privilegeId": req.PrivilegeId}, update)
|
||||
if err != nil {
|
||||
p.logger.Error("Failed to update privilege", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exist.Name = req.Name
|
||||
exist.ServiceKey = req.ServiceKey
|
||||
exist.Description = req.Description
|
||||
exist.Type = req.Type
|
||||
exist.Value = req.Value
|
||||
exist.Price = req.Price
|
||||
exist.UpdatedAt = time.Now()
|
||||
exist.Amount = req.Amount
|
||||
|
||||
return exist, nil
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package privilege_internal
|
||||
package tools
|
||||
|
||||
import (
|
||||
"hub_admin_backend_service/internal/models"
|
Loading…
Reference in New Issue
Block a user