117 lines
3.0 KiB
Go
117 lines
3.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 lessonToDomain(l dbgen.Lesson) domain.Lesson {
|
|
out := domain.Lesson{
|
|
ID: l.ID,
|
|
ModuleID: l.ModuleID,
|
|
Title: l.Title,
|
|
}
|
|
out.VideoURL = fromPgText(l.VideoUrl)
|
|
out.Thumbnail = fromPgText(l.Thumbnail)
|
|
out.Description = fromPgText(l.Description)
|
|
out.CreatedAt = l.CreatedAt.Time
|
|
if l.UpdatedAt.Valid {
|
|
t := l.UpdatedAt.Time
|
|
out.UpdatedAt = &t
|
|
}
|
|
out.SortOrder = int(l.SortOrder)
|
|
return out
|
|
}
|
|
|
|
func (s *Store) CreateLesson(ctx context.Context, moduleID int64, input domain.CreateLessonInput) (domain.Lesson, error) {
|
|
l, err := s.queries.CreateLesson(ctx, dbgen.CreateLessonParams{
|
|
ModuleID: moduleID,
|
|
Title: input.Title,
|
|
VideoUrl: toPgText(input.VideoURL),
|
|
Thumbnail: toPgText(input.Thumbnail),
|
|
Description: toPgText(input.Description),
|
|
})
|
|
if err != nil {
|
|
return domain.Lesson{}, err
|
|
}
|
|
return lessonToDomain(l), nil
|
|
}
|
|
|
|
func (s *Store) GetLessonByID(ctx context.Context, id int64) (domain.Lesson, error) {
|
|
l, err := s.queries.GetLessonByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.Lesson{}, pgx.ErrNoRows
|
|
}
|
|
return domain.Lesson{}, err
|
|
}
|
|
return lessonToDomain(l), nil
|
|
}
|
|
|
|
func (s *Store) ListLessonsByModuleID(ctx context.Context, moduleID int64, limit, offset int32) ([]domain.Lesson, int64, error) {
|
|
rows, err := s.queries.ListLessonsByModuleID(ctx, dbgen.ListLessonsByModuleIDParams{
|
|
ModuleID: moduleID,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return []domain.Lesson{}, 0, nil
|
|
}
|
|
var total int64
|
|
out := make([]domain.Lesson, 0, len(rows))
|
|
for i, r := range rows {
|
|
if i == 0 {
|
|
total = r.TotalCount
|
|
}
|
|
out = append(out, lessonToDomain(dbgen.Lesson{
|
|
ID: r.ID,
|
|
ModuleID: r.ModuleID,
|
|
Title: r.Title,
|
|
VideoUrl: r.VideoUrl,
|
|
Thumbnail: r.Thumbnail,
|
|
Description: r.Description,
|
|
CreatedAt: r.CreatedAt,
|
|
UpdatedAt: r.UpdatedAt,
|
|
SortOrder: r.SortOrder,
|
|
}))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
|
|
func (s *Store) UpdateLesson(ctx context.Context, id int64, input domain.UpdateLessonInput) (domain.Lesson, error) {
|
|
var titleText pgtype.Text
|
|
if input.Title != nil {
|
|
titleText = pgtype.Text{String: *input.Title, Valid: true}
|
|
} else {
|
|
titleText = pgtype.Text{Valid: false}
|
|
}
|
|
l, err := s.queries.UpdateLesson(ctx, dbgen.UpdateLessonParams{
|
|
ID: id,
|
|
Title: titleText,
|
|
VideoUrl: optionalTextUpdate(input.VideoURL),
|
|
Thumbnail: optionalTextUpdate(input.Thumbnail),
|
|
Description: optionalTextUpdate(input.Description),
|
|
SortOrder: optionalInt4Update(input.SortOrder),
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.Lesson{}, pgx.ErrNoRows
|
|
}
|
|
return domain.Lesson{}, err
|
|
}
|
|
return lessonToDomain(l), nil
|
|
}
|
|
|
|
func (s *Store) DeleteLesson(ctx context.Context, id int64) error {
|
|
return s.queries.DeleteLesson(ctx, id)
|
|
}
|