core/service/service.go

102 lines
3.5 KiB
Go

package service
import (
"github.com/go-redis/redis/v8"
"github.com/gofiber/fiber/v2"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/auth"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram"
)
// Service is an entity for http requests handling
type Service struct {
dal *dal.DAL
authClient *auth.AuthClient
producer *brokers.Producer
serviceName string
chDAL *dal.ClickHouseDAL
telegramClient *telegram.TelegramClient
redisClient *redis.Client
s3Prefix string
}
type Deps struct {
Dal *dal.DAL
AuthClient *auth.AuthClient
Producer *brokers.Producer
ServiceName string
ChDAL *dal.ClickHouseDAL
TelegramClient *telegram.TelegramClient
RedisClient *redis.Client
S3Prefix string
}
func New(deps Deps) *Service {
return &Service{
dal: deps.Dal,
authClient: deps.AuthClient,
producer: deps.Producer,
serviceName: deps.ServiceName,
chDAL: deps.ChDAL,
telegramClient: deps.TelegramClient,
redisClient: deps.RedisClient,
s3Prefix: deps.S3Prefix,
}
}
// Register is a function for add handlers of service to external multiplexer
func (s *Service) Register(app *fiber.App) {
// quiz manipulating handlers
app.Post("/quiz/create", s.CreateQuiz)
app.Post("/quiz/getList", s.GetQuizList)
app.Patch("/quiz/edit", s.UpdateQuiz)
app.Post("/quiz/copy", s.CopyQuiz)
app.Post("/quiz/history", s.GetQuizHistory)
app.Delete("/quiz/delete", s.DeleteQuiz)
app.Patch("/quiz/archive", s.ArchiveQuiz)
app.Post("/quiz/move", s.QuizMove)
app.Post("/quiz/template", s.TemplateCopy)
// question manipulating handlers
app.Post("/question/create", s.CreateQuestion)
app.Post("/question/getList", s.GetQuestionList)
app.Patch("/question/edit", s.UpdateQuestion)
app.Post("/question/copy", s.CopyQuestion)
app.Post("/question/history", s.GetQuestionHistory)
app.Delete("/question/delete", s.DeleteQuestion)
// account handlers
app.Get("/account/get", s.getCurrentAccount)
app.Post("/account/create", s.createAccount)
app.Delete("/account/delete", s.deleteAccount)
app.Get("/accounts", s.getAccounts)
app.Get("/privilege/:userId", s.getPrivilegeByUserID)
app.Delete("/account/:userId", s.deleteAccountByUserID)
app.Post("/account/manualdone", s.ManualDone)
app.Post("/account/leadtarget", s.PostLeadTarget)
app.Delete("/account/leadtarget/:id", s.DeleteLeadTarget)
app.Get("/account/leadtarget/:quizID", s.GetLeadTarget)
app.Put("/account/leadtarget", s.UpdateLeadTarget)
// result handlers
app.Post("/results/getResults/:quizId", s.GetResultsByQuizID)
app.Delete("/results/delete/:resultId", s.DelResultByID)
app.Patch("/result/seen", s.SetStatus)
app.Post("/results/:quizID/export", s.ExportResultsToCSV)
app.Get("/result/:resultID", s.GetResultAnswers)
// statistics handlers
app.Post("/statistic/:quizID/devices", s.GetDeviceStatistics)
app.Post("/statistic/:quizID/general", s.GetGeneralStatistics)
app.Post("/statistic/:quizID/questions", s.GetQuestionsStatistics)
app.Post("/statistic", s.AllServiceStatistics)
app.Get("/statistics/:quizID/pipelines", s.GetPipelinesStatistics)
//telegram handlers
app.Get("/telegram/pool", s.GetPoolTgAccounts)
app.Post("/telegram/create", s.AddingTgAccount)
app.Delete("/telegram/:id", s.DeleteTgAccountByID)
app.Post("/telegram/setCode", s.SettingTgCode)
}