codeword/internal/controllers/promocode.go

136 lines
3.8 KiB
Go
Raw Normal View History

2024-03-03 20:18:42 +00:00
package controllers
import (
"codeword/internal/models"
"codeword/internal/service"
"github.com/gofiber/fiber/v2"
)
type PromocodeController struct {
PromocodeService *service.PromocodeService
}
func NewPromocodeController(service *service.PromocodeService) *PromocodeController {
return &PromocodeController{
PromocodeService: service,
}
}
func (c *PromocodeController) Register(router fiber.Router) {
2024-11-15 11:21:04 +00:00
router.Post("/promocode/activate", c.Activate)
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
router.Post("/promocode/getList", c.Getlist)
2024-03-03 20:18:42 +00:00
router.Delete("/promocode/{promocodeID}", c.Delete)
2024-11-15 11:21:04 +00:00
router.Post("/promocode/create", c.Createpromocode)
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
router.Put("/promocode/edit", c.Editpromocode)
router.Post("/promocode/fastlink", c.Createfastlink)
2024-03-03 20:18:42 +00:00
}
func (c *PromocodeController) Name() string {
return ""
}
2024-11-15 11:21:04 +00:00
func (c *PromocodeController) Activate(ctx *fiber.Ctx) error {
// Обработчик для метода Activate
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
var request models.ActivateReq
2024-03-03 20:18:42 +00:00
if err := ctx.BodyParser(&request); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
}
2024-11-15 11:21:04 +00:00
response, err := c.PromocodeService.Activate(ctx.Context(), &request)
2024-03-03 20:18:42 +00:00
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}
2024-11-15 11:21:04 +00:00
func (c *PromocodeController) Getlist(ctx *fiber.Ctx) error {
// Обработчик для метода Getlist
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
var request models.GetPromoCodesListReq
2024-03-03 20:18:42 +00:00
if err := ctx.BodyParser(&request); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
}
2024-11-15 11:21:04 +00:00
response, err := c.PromocodeService.Getlist(ctx.Context(), &request)
2024-03-03 20:18:42 +00:00
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}
2024-11-15 11:21:04 +00:00
func (c *PromocodeController) Delete(ctx *fiber.Ctx) error {
// Обработчик для метода Delete
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
err := c.PromocodeService.Delete(ctx.Context())
2024-03-03 20:18:42 +00:00
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
2024-11-15 11:21:04 +00:00
return ctx.SendStatus(fiber.StatusOK)
2024-03-03 20:18:42 +00:00
}
2024-11-15 11:21:04 +00:00
func (c *PromocodeController) Createpromocode(ctx *fiber.Ctx) error {
// Обработчик для метода Createpromocode
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
var request models.PromoCodeReq
if err := ctx.BodyParser(&request); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
}
response, err := c.PromocodeService.Createpromocode(ctx.Context(), &request)
2024-03-03 20:18:42 +00:00
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
2024-11-15 11:21:04 +00:00
return ctx.Status(fiber.StatusOK).JSON(response)
2024-03-03 20:18:42 +00:00
}
2024-11-15 11:21:04 +00:00
func (c *PromocodeController) Editpromocode(ctx *fiber.Ctx) error {
// Обработчик для метода Editpromocode
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
var request models.EditPromoCodeReq
2024-03-03 20:18:42 +00:00
if err := ctx.BodyParser(&request); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
}
2024-11-15 11:21:04 +00:00
response, err := c.PromocodeService.Editpromocode(ctx.Context(), &request)
2024-03-03 20:18:42 +00:00
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}
2024-11-15 11:21:04 +00:00
func (c *PromocodeController) Createfastlink(ctx *fiber.Ctx) error {
// Обработчик для метода Createfastlink
2024-03-03 20:18:42 +00:00
2024-11-15 11:21:04 +00:00
var request models.CreateFastLinkReq
2024-03-03 20:18:42 +00:00
if err := ctx.BodyParser(&request); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
}
2024-11-15 11:21:04 +00:00
response, err := c.PromocodeService.Createfastlink(ctx.Context(), &request)
2024-03-03 20:18:42 +00:00
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}