Migration 000062 adds lessons.publish_status (DRAFT default for new rows; existing rows published). Editors see all lessons; learners see published-only lists and GET by id. Sequential prerequisites and completion counts ignore drafts. Course lesson_count counts published lessons only. Swagger documents publish_status on create/update bodies. Co-authored-by: Cursor <cursoragent@cursor.com>
89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package lessons
|
|
|
|
import (
|
|
"Yimaru-Backend/internal/domain"
|
|
"Yimaru-Backend/internal/ports"
|
|
"Yimaru-Backend/internal/services/modules"
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
var ErrLessonNotFound = errors.New("lesson not found")
|
|
|
|
type Service struct {
|
|
lessons ports.LessonStore
|
|
modules ports.ModuleStore
|
|
}
|
|
|
|
func NewService(lessons ports.LessonStore, modules ports.ModuleStore) *Service {
|
|
return &Service{lessons: lessons, modules: modules}
|
|
}
|
|
|
|
func (s *Service) getModuleOrErr(ctx context.Context, moduleID int64) error {
|
|
_, err := s.modules.GetModuleByID(ctx, moduleID)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return modules.ErrModuleNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, moduleID int64, input domain.CreateLessonInput) (domain.Lesson, error) {
|
|
if err := s.getModuleOrErr(ctx, moduleID); err != nil {
|
|
return domain.Lesson{}, err
|
|
}
|
|
return s.lessons.CreateLesson(ctx, moduleID, input)
|
|
}
|
|
|
|
func (s *Service) GetByID(ctx context.Context, id int64) (domain.Lesson, error) {
|
|
l, err := s.lessons.GetLessonByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.Lesson{}, ErrLessonNotFound
|
|
}
|
|
return domain.Lesson{}, err
|
|
}
|
|
return l, nil
|
|
}
|
|
|
|
func (s *Service) ListByModule(ctx context.Context, moduleID int64, publishedOnly bool, limit, offset int32) ([]domain.Lesson, int64, error) {
|
|
if err := s.getModuleOrErr(ctx, moduleID); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
if limit > 200 {
|
|
limit = 200
|
|
}
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
return s.lessons.ListLessonsByModuleID(ctx, moduleID, publishedOnly, limit, offset)
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, id int64, input domain.UpdateLessonInput) (domain.Lesson, error) {
|
|
l, err := s.lessons.UpdateLesson(ctx, id, input)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.Lesson{}, ErrLessonNotFound
|
|
}
|
|
return domain.Lesson{}, err
|
|
}
|
|
return l, nil
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, id int64) error {
|
|
if _, err := s.lessons.GetLessonByID(ctx, id); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrLessonNotFound
|
|
}
|
|
return err
|
|
}
|
|
return s.lessons.DeleteLesson(ctx, id)
|
|
}
|