Files
kurious/internal/kurious/app/command/createcourse.go
2024-01-08 18:31:35 +03:00

91 lines
2.0 KiB
Go

package command
import (
"context"
"fmt"
"log/slog"
"time"
"git.loyso.art/frx/kurious/internal/common/decorator"
"git.loyso.art/frx/kurious/internal/common/nullable"
"git.loyso.art/frx/kurious/internal/common/xslices"
"git.loyso.art/frx/kurious/internal/kurious/domain"
)
type CreateCourse struct {
ID string
ExternalID nullable.Value[string]
Name string
SourceType domain.SourceType
SourceName nullable.Value[string]
CourseThematic string
LearningType string
OrganizationID string
OriginLink string
ImageLink string
Description string
FullPrice float64
Discount float64
Duration time.Duration
StartsAt time.Time
}
type CreateCourseHandler decorator.CommandHandler[CreateCourse]
type createCourseHandler struct {
repo domain.CourseRepository
}
func NewCreateCourseHandler(
repo domain.CourseRepository,
log *slog.Logger,
) CreateCourseHandler {
h := createCourseHandler{
repo: repo,
}
return decorator.ApplyCommandDecorators(h, log)
}
func (h createCourseHandler) Handle(ctx context.Context, cmd CreateCourse) error {
_, err := h.repo.Create(ctx, domain.CreateCourseParams(cmd))
if err != nil {
return fmt.Errorf("creating course: %w", err)
}
return nil
}
type CreateCourses struct {
Courses []CreateCourse
}
type CreateCoursesHandler decorator.CommandHandler[CreateCourses]
type createCoursesHandler struct {
repo domain.CourseRepository
}
func NewCreateCoursesHandler(
repo domain.CourseRepository,
log *slog.Logger,
) CreateCoursesHandler {
h := createCoursesHandler{
repo: repo,
}
return decorator.ApplyCommandDecorators(h, log)
}
func (h createCoursesHandler) Handle(ctx context.Context, cmd CreateCourses) error {
params := xslices.Map(cmd.Courses, func(in CreateCourse) (out domain.CreateCourseParams) {
return domain.CreateCourseParams(in)
})
err := h.repo.CreateBatch(ctx, params...)
if err != nil {
return fmt.Errorf("creating course: %w", err)
}
return nil
}