add repo method for create privilege
This commit is contained in:
parent
a2baad87d7
commit
dc25cc41df
@ -1,8 +1,11 @@
|
|||||||
package privilege_internal
|
package privilege_internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
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/repository/privilege"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@ -35,11 +38,39 @@ func (p *PrivilegeInternal) Get(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// хаб нода registerPrivilege
|
// хаб нода registerPrivilege
|
||||||
func (p *PrivilegeInternal) Create(c *fiber.Ctx) error {
|
func (p *PrivilegeInternal) Create(c *fiber.Ctx) error {
|
||||||
return c.SendStatus(http.StatusCreated)
|
var req models.CreateUpdateReq
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !Validate(req) {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := p.repo.Create(c.Context(), &req)
|
||||||
|
if err != nil {
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, our_errors.ErrAlreadyExist):
|
||||||
|
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "Privilege already exist"})
|
||||||
|
default:
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// хаб нода replacePrivilege
|
// хаб нода replacePrivilege
|
||||||
func (p *PrivilegeInternal) Update(c *fiber.Ctx) error {
|
func (p *PrivilegeInternal) Update(c *fiber.Ctx) error {
|
||||||
|
var req models.CreateUpdateReq
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !Validate(req) {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
||||||
|
}
|
||||||
|
|
||||||
return c.SendStatus(http.StatusOK)
|
return c.SendStatus(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,11 +91,19 @@ func (p *PrivilegeInternal) GetByService(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// хаб нода registerPrivileges
|
// хаб нода registerPrivileges
|
||||||
func (p *PrivilegeInternal) PostMany(c *fiber.Ctx) error {
|
func (p *PrivilegeInternal) PostMany(c *fiber.Ctx) error {
|
||||||
|
var req models.ManyCreateUpdate
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||||
|
}
|
||||||
return c.SendStatus(http.StatusCreated)
|
return c.SendStatus(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
// хаб нода replacePrivileges
|
// хаб нода replacePrivileges
|
||||||
func (p *PrivilegeInternal) UpdateMany(c *fiber.Ctx) error {
|
func (p *PrivilegeInternal) UpdateMany(c *fiber.Ctx) error {
|
||||||
|
var req models.ManyCreateUpdate
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||||
|
}
|
||||||
return c.SendStatus(http.StatusOK)
|
return c.SendStatus(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
internal/controller/privilege_internal/validate.go
Normal file
13
internal/controller/privilege_internal/validate.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package privilege_internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"hub_admin_backend_service/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Validate(req models.CreateUpdateReq) bool {
|
||||||
|
if req.Name == "" || req.PrivilegeId == "" || req.ServiceKey == "" || req.Type == "" || req.Value == "" || req.Description == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
8
internal/errors/repository.go
Normal file
8
internal/errors/repository.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrAlreadyExist = errors.New("already exist")
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
|
)
|
@ -1,14 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type CreateUpdateReq struct {
|
type CreateUpdateReq struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
PrivilegeId string `json:"privilegeId"`
|
PrivilegeId string `json:"privilegeId"`
|
||||||
ServiceKey string `json:"serviceKey"`
|
ServiceKey string `json:"serviceKey"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
Price int `json:"price"`
|
Price float64 `json:"price"`
|
||||||
Amount int `json:"amount"`
|
Amount int `json:"amount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ManyCreateUpdate struct {
|
type ManyCreateUpdate struct {
|
||||||
|
@ -3,10 +3,13 @@ package privilege
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"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"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"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 +48,37 @@ func (p *Privilege) GetAllPrivileges(ctx context.Context) ([]models.Privilege, e
|
|||||||
|
|
||||||
return privileges, nil
|
return privileges, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Privilege) Create(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 == nil {
|
||||||
|
return nil, errors.ErrAlreadyExist
|
||||||
|
} else if err != mongo.ErrNoDocuments {
|
||||||
|
p.logger.Error("Failed to check existing privilege", zap.Error(err))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
privilege := &models.Privilege{
|
||||||
|
ID: primitive.NewObjectID(),
|
||||||
|
Name: req.Name,
|
||||||
|
PrivilegeID: req.PrivilegeId,
|
||||||
|
ServiceKey: req.ServiceKey,
|
||||||
|
Description: req.Description,
|
||||||
|
Type: req.Type,
|
||||||
|
Value: req.Value,
|
||||||
|
Price: req.Price,
|
||||||
|
// amount в хабе не вставляется
|
||||||
|
Amount: req.Amount,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
IsDeleted: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = p.mdb.InsertOne(ctx, privilege)
|
||||||
|
if err != nil {
|
||||||
|
p.logger.Error("Failed to create privilege", zap.Error(err))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return privilege, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user