common/repository/answer/answer.go

127 lines
3.0 KiB
Go
Raw Normal View History

2024-02-19 16:33:15 +00:00
package answer
import (
"context"
"database/sql"
"encoding/json"
2024-02-19 17:43:47 +00:00
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
2024-02-19 16:33:15 +00:00
)
type Deps struct {
2024-03-28 11:53:12 +00:00
Queries *sqlcgen.Queries
Pool *sql.DB
2024-03-28 12:22:54 +00:00
AnswerMinio *StorerAnswer
2024-02-19 16:33:15 +00:00
}
type AnswerRepository struct {
queries *sqlcgen.Queries
pool *sql.DB
2024-02-19 16:33:15 +00:00
}
func NewAnswerRepository(deps Deps) *AnswerRepository {
return &AnswerRepository{
queries: deps.Queries,
pool: deps.Pool,
2024-02-19 16:33:15 +00:00
}
}
// test +
func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.Answer, session, fp string, quizID uint64) ([]model.Answer, []error) {
2024-02-19 16:33:15 +00:00
var (
createdAnswers []model.Answer
errs []error
2024-02-19 16:33:15 +00:00
)
tx, err := r.pool.BeginTx(ctx, nil)
if err != nil {
return nil, []error{err}
}
for _, ans := range answers {
if ans.Utm == nil {
ans.Utm = make(model.UTMSavingMap)
}
utmJSON, err := json.Marshal(ans.Utm)
if err != nil {
return nil, []error{err}
}
2024-02-19 16:33:15 +00:00
params := sqlcgen.InsertAnswersParams{
Content: sql.NullString{String: ans.Content, Valid: true},
QuizID: int64(quizID),
QuestionID: int64(ans.QuestionId),
Fingerprint: sql.NullString{String: fp, Valid: true},
Session: sql.NullString{String: session, Valid: true},
Result: sql.NullBool{Bool: ans.Result, Valid: true},
2024-03-13 16:21:37 +00:00
Email: ans.Email,
2024-03-14 13:36:22 +00:00
Device: ans.Device,
DeviceType: ans.DeviceType,
Ip: ans.IP,
Browser: ans.Browser,
Os: ans.OS,
Start: ans.Start,
Utm: utmJSON,
2024-09-19 08:42:49 +00:00
Version: ans.Version,
2024-02-19 16:33:15 +00:00
}
row, err := r.queries.InsertAnswers(ctx, params)
createdAnswer := model.Answer{
Id: uint64(row.ID),
Content: row.Content.String,
QuizId: uint64(row.QuizID),
QuestionId: uint64(row.QuestionID),
Fingerprint: row.Fingerprint.String,
Session: row.Session.String,
Result: row.Result.Bool,
New: row.New.Bool,
Email: row.Email,
DeviceType: row.DeviceType,
Device: row.Device,
OS: row.Os,
Browser: row.Browser,
IP: row.Ip,
Start: row.Start,
2024-09-19 08:42:49 +00:00
Version: row.Version,
}
2024-02-19 16:33:15 +00:00
if err != nil {
errs = append(errs, err)
} else {
createdAnswers = append(createdAnswers, createdAnswer)
2024-02-19 16:33:15 +00:00
}
}
err = tx.Commit()
if err != nil {
errs = append(errs, err)
return nil, errs
}
return createdAnswers, errs
2024-02-19 16:33:15 +00:00
}
// test +
func (r *AnswerRepository) GetAllAnswersByQuizID(ctx context.Context, session string) ([]model.ResultAnswer, error) {
var results []model.ResultAnswer
rows, err := r.queries.GetAllAnswersByQuizID(ctx, sql.NullString{String: session, Valid: true})
if err != nil {
return nil, err
}
for _, row := range rows {
resultAnswer := model.ResultAnswer{
Content: row.Content.String,
CreatedAt: row.CreatedAt.Time,
QuestionID: uint64(row.QuestionID),
AnswerID: uint64(row.ID),
2024-09-18 16:22:34 +00:00
Version: row.Version,
2024-02-19 16:33:15 +00:00
}
results = append(results, resultAnswer)
}
return results, nil
}