create utm controller
Some checks failed
Deploy / CreateImage (push) Successful in 2m42s
Deploy / ValidateConfig (push) Successful in 23s
Deploy / MigrateDatabase (push) Failing after 42s
Deploy / DeployService (push) Has been skipped

This commit is contained in:
skeris 2025-08-12 21:16:48 +03:00
parent 929cb09680
commit 44e8ac3eea
5 changed files with 203 additions and 2 deletions

@ -2088,4 +2088,117 @@ paths:
'application/json':
schema:
type: string
default: if you get any content string send it to developer
default: if you get any content string send it to developer
/utm/{quizID}:
get:
summary: Получить все UTM по quizID
parameters:
- name: quizID
in: path
required: true
description: Идентификатор опроса
schema:
type: integer
format: int64
responses:
'200':
description: Список UTM
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: integer
format: int64
example: 1
quiz_id:
type: integer
format: int64
example: 123
utm:
type: string
example: utm_campaign=test
deleted:
type: boolean
example: false
created_at:
type: string
format: date-time
example: "2024-06-23T12:00:00Z"
'400':
description: Bad Request
'500':
description: Internal server error
/utm:
post:
summary: Создать новую UTM запись
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- quiz_id
- utm
properties:
quiz_id:
type: integer
format: int64
example: 123
utm:
type: string
example: utm_campaign=test
responses:
'200':
description: Созданная UTM запись
content:
application/json:
schema:
type: object
properties:
id:
type: integer
format: int64
example: 1
quiz_id:
type: integer
format: int64
example: 123
utm:
type: string
example: utm_campaign=test
deleted:
type: boolean
example: false
created_at:
type: string
format: date-time
example: "2024-06-23T12:00:00Z"
'400':
description: Bad Request
'500':
description: Internal server error
/utm/{utmID}:
delete:
summary: Мягкое удаление UTM по ID
parameters:
- name: utmID
in: path
required: true
description: Идентификатор UTM
schema:
type: integer
format: int64
responses:
'200':
description: Success delete
'400':
description: Bad Request
'500':
description: Internal server error

@ -163,7 +163,7 @@ func Run(ctx context.Context, cfg initialize.Config, build Build) error {
srv := http.NewServer(http.ServerConfig{
Logger: zapLogger,
Controllers: []http.Controller{controllers.HttpControllers.Account, controllers.HttpControllers.Telegram, controllers.HttpControllers.Result,
controllers.HttpControllers.Question, controllers.HttpControllers.Quiz, controllers.HttpControllers.Statistic},
controllers.HttpControllers.Question, controllers.HttpControllers.Quiz, controllers.HttpControllers.Statistic, controllers.HttpControllers.UTM},
Hlogger: loggerHlog,
})

@ -0,0 +1,13 @@
package utm
import "github.com/gofiber/fiber/v2"
func (r *UTM) Register(router fiber.Router) {
router.Get("/:quizID", r.GetAllQuizUtms)
router.Post("/", r.CreateQuizUTM)
router.Delete("/:utmID", r.DeleteQuizUTM)
}
func (r *UTM) Name() string {
return "utm"
}

@ -0,0 +1,70 @@
package utm
import (
"gitea.pena/SQuiz/common/dal"
"github.com/gofiber/fiber/v2"
)
type Deps struct {
DAL *dal.DAL
}
type UTM struct {
dal *dal.DAL
}
func NewUTMController(deps Deps) *UTM {
return &UTM{
dal: deps.DAL,
}
}
func (r *UTM) CreateQuizUTM(ctx *fiber.Ctx) error {
var req struct {
QuizID int64 `json:"quiz_id"`
UTM string `json:"utm"`
}
if err := ctx.BodyParser(&req); err != nil {
return ctx.Status(fiber.StatusBadRequest).SendString(err.Error())
}
if req.QuizID <= 0 || req.UTM == "" {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "quiz_id and utm are required"})
}
utm, err := r.dal.QuizRepo.CreateQuizUTM(ctx.Context(), req.QuizID, req.UTM)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return ctx.Status(fiber.StatusOK).JSON(utm)
}
func (r *UTM) GetAllQuizUtms(ctx *fiber.Ctx) error {
quizID, err := ctx.ParamsInt("quizID")
if err != nil || quizID <= 0 {
return ctx.Status(fiber.StatusBadRequest).SendString(err.Error())
}
utms, err := r.dal.QuizRepo.GetAllQuizUtms(ctx.Context(), int64(quizID))
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return ctx.JSON(utms)
}
func (r *UTM) DeleteQuizUTM(ctx *fiber.Ctx) error {
utmID, err := ctx.ParamsInt("utmID")
if err != nil || utmID <= 0 {
return ctx.Status(fiber.StatusBadRequest).SendString(err.Error())
}
err = r.dal.QuizRepo.DeleteQuizUTM(ctx.Context(), int64(utmID))
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return ctx.SendStatus(fiber.StatusOK)
}

@ -8,6 +8,7 @@ import (
"gitea.pena/SQuiz/core/internal/controllers/http_controllers/result"
"gitea.pena/SQuiz/core/internal/controllers/http_controllers/statistic"
"gitea.pena/SQuiz/core/internal/controllers/http_controllers/telegram"
"gitea.pena/SQuiz/core/internal/controllers/http_controllers/utm"
"gitea.pena/SQuiz/core/internal/controllers/rpc_controllers"
"github.com/go-redis/redis/v8"
)
@ -36,6 +37,7 @@ type HttpControllers struct {
Result *result.Result
Statistic *statistic.Statistic
Telegram *telegram.Telegram
UTM *utm.UTM
}
func NewControllers(deps ControllerDeps) *Controller {
@ -70,6 +72,9 @@ func NewControllers(deps ControllerDeps) *Controller {
DAL: deps.DALs.PgDAL,
//TelegramClient: deps.Clients.TgClient,
}),
UTM: utm.NewUTMController(utm.Deps{
DAL: deps.DALs.PgDAL,
}),
},
}
}