2024-04-09 15:52:37 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-04-23 12:22:12 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
|
2024-04-17 12:21:06 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
2024-04-23 12:22:12 +00:00
|
|
|
"strconv"
|
2024-04-09 15:52:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Controller) ChangeQuizSettings(ctx *fiber.Ctx) error {
|
2024-04-23 12:22:12 +00:00
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
quizID := ctx.Params("quizID")
|
|
|
|
if quizID == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
|
|
|
|
}
|
|
|
|
|
2024-04-23 12:22:12 +00:00
|
|
|
quizIDInt, err := strconv.Atoi(quizID)
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
|
|
|
|
}
|
|
|
|
|
2024-04-17 12:21:06 +00:00
|
|
|
var request model.RulesReq
|
2024-04-09 15:52:37 +00:00
|
|
|
if err := ctx.BodyParser(&request); err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
}
|
|
|
|
|
2024-04-23 12:22:12 +00:00
|
|
|
response, err := c.service.ChangeQuizSettings(ctx.Context(), &request, accountID, quizIDInt)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
2024-04-23 12:22:12 +00:00
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) SetQuizSettings(ctx *fiber.Ctx) error {
|
2024-04-23 12:22:12 +00:00
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
quizID := ctx.Params("quizID")
|
|
|
|
if quizID == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
|
|
|
|
}
|
|
|
|
|
2024-04-23 12:22:12 +00:00
|
|
|
quizIDInt, err := strconv.Atoi(quizID)
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
|
|
|
|
}
|
|
|
|
|
2024-04-17 12:21:06 +00:00
|
|
|
var request model.RulesReq
|
2024-04-09 15:52:37 +00:00
|
|
|
if err := ctx.BodyParser(&request); err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
|
|
|
}
|
|
|
|
|
2024-04-23 12:22:12 +00:00
|
|
|
response, err := c.service.SetQuizSettings(ctx.Context(), &request, accountID, quizIDInt)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
2024-04-23 12:22:12 +00:00
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GettingQuizRules(ctx *fiber.Ctx) error {
|
|
|
|
quizID := ctx.Params("quizID")
|
|
|
|
if quizID == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
|
|
|
|
}
|
|
|
|
|
2024-04-23 12:22:12 +00:00
|
|
|
quizIDInt, err := strconv.Atoi(quizID)
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := c.service.GettingQuizRules(ctx.Context(), quizIDInt)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|