127 lines
4.0 KiB
Go
127 lines
4.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func examPrepPracticeFromListRow(r dbgen.ExamPrepListLessonPracticesByLessonIDRow) domain.ExamPrepPractice {
|
|
return examPrepPracticeToDomain(dbgen.ExamPrepLessonPractice{
|
|
ID: r.ID,
|
|
UnitModuleLessonID: r.UnitModuleLessonID,
|
|
Title: r.Title,
|
|
StoryDescription: r.StoryDescription,
|
|
StoryImage: r.StoryImage,
|
|
PersonaID: r.PersonaID,
|
|
QuestionSetID: r.QuestionSetID,
|
|
QuickTips: r.QuickTips,
|
|
CreatedAt: r.CreatedAt,
|
|
UpdatedAt: r.UpdatedAt,
|
|
})
|
|
}
|
|
|
|
func examPrepPracticeToDomain(p dbgen.ExamPrepLessonPractice) domain.ExamPrepPractice {
|
|
out := domain.ExamPrepPractice{
|
|
ID: p.ID,
|
|
LessonID: p.UnitModuleLessonID,
|
|
Title: p.Title,
|
|
QuestionSetID: p.QuestionSetID,
|
|
}
|
|
out.StoryDescription = fromPgText(p.StoryDescription)
|
|
out.StoryImage = fromPgText(p.StoryImage)
|
|
out.QuickTips = fromPgText(p.QuickTips)
|
|
out.PersonaID = fromPgInt8ID(p.PersonaID)
|
|
out.CreatedAt = p.CreatedAt.Time
|
|
if p.UpdatedAt.Valid {
|
|
t := p.UpdatedAt.Time
|
|
out.UpdatedAt = &t
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Store) CreateExamPrepLessonPractice(ctx context.Context, lessonID int64, in domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
|
|
p, err := s.queries.ExamPrepCreateLessonPractice(ctx, dbgen.ExamPrepCreateLessonPracticeParams{
|
|
UnitModuleLessonID: lessonID,
|
|
Title: in.Title,
|
|
StoryDescription: toPgText(in.StoryDescription),
|
|
StoryImage: toPgText(in.StoryImage),
|
|
PersonaID: int64PtrToPg8(in.PersonaID),
|
|
QuestionSetID: in.QuestionSetID,
|
|
QuickTips: toPgText(in.QuickTips),
|
|
})
|
|
if err != nil {
|
|
return domain.ExamPrepPractice{}, err
|
|
}
|
|
return examPrepPracticeToDomain(p), nil
|
|
}
|
|
|
|
func (s *Store) GetExamPrepLessonPracticeByID(ctx context.Context, id int64) (domain.ExamPrepPractice, error) {
|
|
p, err := s.queries.ExamPrepGetLessonPracticeByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.ExamPrepPractice{}, pgx.ErrNoRows
|
|
}
|
|
return domain.ExamPrepPractice{}, err
|
|
}
|
|
return examPrepPracticeToDomain(p), nil
|
|
}
|
|
|
|
func (s *Store) ListExamPrepLessonPracticesByLessonID(ctx context.Context, lessonID int64, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
|
|
rows, err := s.queries.ExamPrepListLessonPracticesByLessonID(ctx, dbgen.ExamPrepListLessonPracticesByLessonIDParams{
|
|
UnitModuleLessonID: lessonID,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return []domain.ExamPrepPractice{}, 0, nil
|
|
}
|
|
var total int64
|
|
out := make([]domain.ExamPrepPractice, 0, len(rows))
|
|
for i, r := range rows {
|
|
if i == 0 {
|
|
total = r.TotalCount
|
|
}
|
|
out = append(out, examPrepPracticeFromListRow(r))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
|
|
func (s *Store) UpdateExamPrepLessonPractice(ctx context.Context, id int64, input domain.UpdateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
|
|
var titleText pgtype.Text
|
|
if input.Title != nil {
|
|
titleText = pgtype.Text{String: *input.Title, Valid: true}
|
|
} else {
|
|
titleText = pgtype.Text{Valid: false}
|
|
}
|
|
qs := optionalInt8UpdateID(input.QuestionSetID)
|
|
p, err := s.queries.ExamPrepUpdateLessonPractice(ctx, dbgen.ExamPrepUpdateLessonPracticeParams{
|
|
ID: id,
|
|
Title: titleText,
|
|
StoryDescription: optionalTextUpdate(input.StoryDescription),
|
|
StoryImage: optionalTextUpdate(input.StoryImage),
|
|
PersonaID: optionalInt8UpdateID(input.PersonaID),
|
|
QuestionSetID: qs,
|
|
QuickTips: optionalTextUpdate(input.QuickTips),
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.ExamPrepPractice{}, pgx.ErrNoRows
|
|
}
|
|
return domain.ExamPrepPractice{}, err
|
|
}
|
|
return examPrepPracticeToDomain(p), nil
|
|
}
|
|
|
|
func (s *Store) DeleteExamPrepLessonPractice(ctx context.Context, id int64) error {
|
|
return s.queries.ExamPrepDeleteLessonPractice(ctx, id)
|
|
}
|