2024-05-29 13:02:06 +00:00
|
|
|
|
package privilege_internal
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-29 17:06:28 +00:00
|
|
|
|
"errors"
|
2024-05-29 13:02:06 +00:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-05-29 14:31:33 +00:00
|
|
|
|
"go.uber.org/zap"
|
2024-05-29 17:06:28 +00:00
|
|
|
|
our_errors "hub_admin_backend_service/internal/errors"
|
|
|
|
|
"hub_admin_backend_service/internal/models"
|
2024-05-29 14:31:33 +00:00
|
|
|
|
"hub_admin_backend_service/internal/repository/privilege"
|
2024-05-29 17:27:25 +00:00
|
|
|
|
"hub_admin_backend_service/internal/tools"
|
2024-05-29 13:02:06 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Deps struct {
|
2024-05-29 14:31:33 +00:00
|
|
|
|
Repo *privilege.Privilege
|
|
|
|
|
Logger *zap.Logger
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PrivilegeInternal struct {
|
2024-05-29 14:31:33 +00:00
|
|
|
|
repo *privilege.Privilege
|
|
|
|
|
logger *zap.Logger
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPrivilegeInternal(deps Deps) *PrivilegeInternal {
|
2024-05-29 14:31:33 +00:00
|
|
|
|
return &PrivilegeInternal{
|
|
|
|
|
repo: deps.Repo,
|
|
|
|
|
logger: deps.Logger,
|
|
|
|
|
}
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода getAllPrivileges
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) Get(c *fiber.Ctx) error {
|
2024-05-29 15:55:17 +00:00
|
|
|
|
privileges, err := p.repo.GetAllPrivileges(c.Context())
|
|
|
|
|
if err != nil {
|
2024-06-04 10:15:09 +00:00
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, our_errors.ErrNotFound):
|
|
|
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Privileges not found"})
|
|
|
|
|
default:
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
|
|
|
}
|
2024-05-29 15:55:17 +00:00
|
|
|
|
}
|
2024-06-04 10:15:09 +00:00
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
return c.Status(fiber.StatusOK).JSON(privileges)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода registerPrivilege
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) Create(c *fiber.Ctx) error {
|
2024-05-29 17:06:28 +00:00
|
|
|
|
var req models.CreateUpdateReq
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-26 11:44:37 +00:00
|
|
|
|
if !tools.ValidatePrivilege(req) {
|
2024-05-29 17:06:28 +00:00
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:42:40 +00:00
|
|
|
|
result, err := p.repo.Create(c.Context(), req)
|
2024-05-29 17:06:28 +00:00
|
|
|
|
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)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода replacePrivilege
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) Update(c *fiber.Ctx) error {
|
2024-05-29 17:06:28 +00:00
|
|
|
|
var req models.CreateUpdateReq
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-26 11:44:37 +00:00
|
|
|
|
if !tools.ValidatePrivilege(req) {
|
2024-05-29 17:06:28 +00:00
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:42:40 +00:00
|
|
|
|
result, err := p.repo.Update(c.Context(), req)
|
2024-05-29 17:27:25 +00:00
|
|
|
|
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)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода removePrivilege
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) Delete(c *fiber.Ctx) error {
|
2024-06-04 09:42:40 +00:00
|
|
|
|
var req struct {
|
|
|
|
|
ID string `json:"privilegeId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "PrivilegeID is required"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := p.repo.Delete(c.Context(), req.ID)
|
|
|
|
|
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)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода getPrivilege
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) GetByID(c *fiber.Ctx) error {
|
2024-06-04 10:15:09 +00:00
|
|
|
|
privilegeID := c.Params("privilegeId")
|
|
|
|
|
if privilegeID == "" {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Privilege ID is required"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := p.repo.GetByID(c.Context(), privilegeID)
|
|
|
|
|
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)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода getServicePrivileges
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) GetByService(c *fiber.Ctx) error {
|
2024-06-04 10:15:09 +00:00
|
|
|
|
serviceKey := c.Params("serviceKey")
|
|
|
|
|
if serviceKey == "" {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Service key is required"})
|
|
|
|
|
}
|
|
|
|
|
result, err := p.repo.GetByServiceKey(c.Context(), serviceKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, our_errors.ErrNotFound):
|
|
|
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Privileges not found"})
|
|
|
|
|
default:
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(result)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода registerPrivileges
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) PostMany(c *fiber.Ctx) error {
|
2024-05-29 17:06:28 +00:00
|
|
|
|
var req models.ManyCreateUpdate
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
|
}
|
2024-06-04 11:53:48 +00:00
|
|
|
|
|
|
|
|
|
if len(req.Privileges) == 0 {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "len array dont be 0"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, priv := range req.Privileges {
|
2024-07-26 11:44:37 +00:00
|
|
|
|
if !tools.ValidatePrivilege(priv) {
|
2024-06-04 11:53:48 +00:00
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := p.repo.PostMany(c.Context(), req)
|
|
|
|
|
if err != nil {
|
2024-06-04 13:12:15 +00:00
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, our_errors.ErrAlreadyExist):
|
|
|
|
|
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "One is privilege already exist"})
|
|
|
|
|
default:
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
|
|
|
}
|
2024-06-04 11:53:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(result)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода replacePrivileges
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) UpdateMany(c *fiber.Ctx) error {
|
2024-05-29 17:06:28 +00:00
|
|
|
|
var req models.ManyCreateUpdate
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
|
}
|
2024-06-04 13:12:15 +00:00
|
|
|
|
|
|
|
|
|
if len(req.Privileges) == 0 {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "len array dont be 0"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, priv := range req.Privileges {
|
2024-07-26 11:44:37 +00:00
|
|
|
|
if !tools.ValidatePrivilege(priv) {
|
2024-06-04 13:12:15 +00:00
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Missing required fields"})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := p.repo.UpdateMany(c.Context(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, our_errors.ErrNotFound):
|
|
|
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "One is privilege not found"})
|
|
|
|
|
default:
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(result)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:55:17 +00:00
|
|
|
|
// хаб нода restorePrivilege
|
2024-05-29 13:02:06 +00:00
|
|
|
|
func (p *PrivilegeInternal) Restore(c *fiber.Ctx) error {
|
2024-06-04 13:12:15 +00:00
|
|
|
|
var req struct {
|
|
|
|
|
ID string `json:"privilegeId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "PrivilegeID is required"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := p.repo.RestorePrivilege(c.Context(), req.ID)
|
|
|
|
|
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)
|
2024-05-29 13:02:06 +00:00
|
|
|
|
}
|