104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package controllers
|
|
|
|
import (
|
|
"errors"
|
|
"gitea.pena/SQuiz/common/middleware"
|
|
"gitea.pena/SQuiz/common/pj_errors"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (c *Controller) UpdateListUsers(ctx *fiber.Ctx) error {
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
}
|
|
|
|
//accountID := "654a8909725f47e926f0bebc"
|
|
|
|
err := c.service.UpdateListUsers(ctx.Context(), accountID)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
func (c *Controller) GettingUserWithPagination(ctx *fiber.Ctx) error {
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
}
|
|
|
|
req, err := extractParams(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
response, err := c.service.GettingUserWithPagination(ctx.Context(), req, accountID)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
}
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
}
|
|
|
|
func (c *Controller) SoftDeleteAccount(ctx *fiber.Ctx) error {
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
}
|
|
|
|
err := c.service.SoftDeleteAccount(ctx.Context(), accountID)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
func (c *Controller) GetCurrentAccount(ctx *fiber.Ctx) error {
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
}
|
|
|
|
response, err := c.service.GetCurrentAccount(ctx.Context(), accountID)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, pj_errors.ErrNotFound):
|
|
return ctx.Status(fiber.StatusNotFound).SendString("user not found")
|
|
default:
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
}
|
|
}
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
}
|
|
|
|
func (c *Controller) ConnectAccount(ctx *fiber.Ctx) error {
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
if !ok {
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
}
|
|
|
|
var req struct {
|
|
ClientBitrixURL string `json:"client_bitrix_url"`
|
|
}
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
}
|
|
|
|
if req.ClientBitrixURL == "" {
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("client_bitrix_url is required")
|
|
}
|
|
//accountID := "64f2cd7a7047f28fdabf6d9e"
|
|
|
|
response, err := c.service.ConnectAccount(ctx.Context(), accountID, req.ClientBitrixURL)
|
|
if err != nil {
|
|
c.logger.Error("error connect account", zap.Error(err))
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
}
|