2024-04-04 09:42:40 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2024-04-08 08:20:10 +00:00
|
|
|
"amocrm/internal/models"
|
2024-04-04 09:42:40 +00:00
|
|
|
"amocrm/internal/service"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccountController struct {
|
|
|
|
AccountService *service.AccountService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAccountController(service *service.AccountService) *AccountController {
|
|
|
|
return &AccountController{
|
|
|
|
AccountService: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AccountController) Register(router fiber.Router) {
|
2024-04-08 15:24:50 +00:00
|
|
|
router.Patch("/users", c.UpdateListUsers)
|
|
|
|
router.Get("/users", c.GettingUserFromCash)
|
|
|
|
router.Delete("/utms/:quizID", c.DeletingUserUtm)
|
|
|
|
router.Post("/utms/:quizID", c.SavingUserUtm)
|
|
|
|
router.Get("/utms/:quizID", c.GettingUserUtm)
|
|
|
|
router.Delete("/account", c.SoftDeleteAccount)
|
|
|
|
router.Get("/account", c.GetCurrentAccount)
|
|
|
|
router.Post("/account", c.ConnectAccount)
|
2024-04-04 09:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AccountController) Name() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) UpdateListUsers(ctx *fiber.Ctx) error {
|
|
|
|
err := c.AccountService.UpdateListUsers(ctx.Context())
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) GettingUserFromCash(ctx *fiber.Ctx) error {
|
|
|
|
req, err := extractParams(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
response, err := c.AccountService.GettingUserFromCash(ctx.Context())
|
2024-04-08 08:20:10 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) DeletingUserUtm(ctx *fiber.Ctx) error {
|
|
|
|
quizID := ctx.Params("quizID")
|
|
|
|
if quizID == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
|
|
|
|
}
|
2024-04-08 08:20:10 +00:00
|
|
|
|
|
|
|
var request models.ListDeleteUTMIDsReq
|
|
|
|
if err := ctx.BodyParser(&request); err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
err := c.AccountService.DeletingUserUtm(ctx.Context(), &request)
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) SavingUserUtm(ctx *fiber.Ctx) error {
|
|
|
|
quizID := ctx.Params("quizID")
|
|
|
|
if quizID == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
|
|
|
|
}
|
2024-04-08 08:20:10 +00:00
|
|
|
|
|
|
|
var request models.SaveUserListUTMReq
|
|
|
|
if err := ctx.BodyParser(&request); err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
response, err := c.AccountService.SavingUserUtm(ctx.Context(), &request)
|
2024-04-08 08:20:10 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) GettingUserUtm(ctx *fiber.Ctx) error {
|
|
|
|
quizID := ctx.Params("quizID")
|
|
|
|
if quizID == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := extractParams(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-08 08:20:10 +00:00
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
response, err := c.AccountService.GettingUserUtm(ctx.Context())
|
2024-04-08 08:20:10 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) SoftDeleteAccount(ctx *fiber.Ctx) error {
|
|
|
|
err := c.AccountService.SoftDeleteAccount(ctx.Context())
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) GetCurrentAccount(ctx *fiber.Ctx) error {
|
|
|
|
response, err := c.AccountService.GetCurrentAccount(ctx.Context())
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:24:50 +00:00
|
|
|
func (c *AccountController) ConnectAccount(ctx *fiber.Ctx) error {
|
|
|
|
response, err := c.AccountService.ConnectAccount(ctx.Context())
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|