2024-02-19 16:33:15 +00:00
|
|
|
package answer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-05-17 17:58:51 +00:00
|
|
|
"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 {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnswerRepository(deps Deps) *AnswerRepository {
|
|
|
|
return &AnswerRepository{
|
2024-03-28 11:54:20 +00:00
|
|
|
queries: deps.Queries,
|
|
|
|
pool: deps.Pool,
|
|
|
|
answerMinio: deps.AnswerMinio,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.Answer, session, fp string, quizID uint64) ([]uint64, []error) {
|
|
|
|
var (
|
|
|
|
answered []uint64
|
|
|
|
errs []error
|
|
|
|
)
|
|
|
|
|
|
|
|
tx, err := r.pool.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ans := range answers {
|
2024-06-01 11:40:37 +00:00
|
|
|
utmJSON, err := json.Marshal(ans.Utm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
2024-05-17 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2024-03-15 11:35:39 +00:00
|
|
|
Start: ans.Start,
|
2024-05-17 17:58:51 +00:00
|
|
|
Utm: utmJSON,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 17:58:51 +00:00
|
|
|
err = r.queries.InsertAnswers(ctx, params)
|
2024-02-19 16:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
} else {
|
|
|
|
answered = append(answered, ans.QuestionId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
return nil, errs
|
|
|
|
}
|
|
|
|
|
2024-04-01 11:57:35 +00:00
|
|
|
return answered, 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 {
|
2024-05-04 12:35:52 +00:00
|
|
|
//todo тут забыл добавить проверку на то что minio !=nil
|
2024-04-13 23:13:58 +00:00
|
|
|
/*if row.Questiontype == model.TypeFile {
|
2024-04-11 16:26:02 +00:00
|
|
|
fmt.Println("GALL", row.Qid, row.QuestionID, row.Content)
|
2024-03-28 11:53:12 +00:00
|
|
|
fileURL, err := r.answerMinio.GetAnswerURL(ctx, row.Qid.UUID.String(), row.QuestionID, row.Content.String)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("GetAnswerURL dal answer minio answer", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
row.Content = sql.NullString{String: fmt.Sprintf("%s|%s", fileURL, row.Content.String), Valid: true}
|
2024-04-13 23:13:58 +00:00
|
|
|
}*/
|
2024-03-28 11:53:12 +00:00
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
resultAnswer := model.ResultAnswer{
|
|
|
|
Content: row.Content.String,
|
|
|
|
CreatedAt: row.CreatedAt.Time,
|
|
|
|
QuestionID: uint64(row.QuestionID),
|
|
|
|
AnswerID: uint64(row.ID),
|
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, resultAnswer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|