Files
kurious/internal/kurious/app/query/listcoursethematics.go
2024-01-10 00:02:40 +03:00

63 lines
1.5 KiB
Go

package query
import (
"context"
"fmt"
"log/slog"
"git.loyso.art/frx/kurious/internal/common/decorator"
"git.loyso.art/frx/kurious/internal/kurious/domain"
)
type ListCourseThematics struct {
LearningTypeID string
}
type CourseThematic struct {
ID string
Name string
}
type ListCourseThematicsResult struct {
CourseThematics []CourseThematic
}
type ListCourseThematicsHandler decorator.QueryHandler[ListCourseThematics, ListCourseThematicsResult]
type listCourseThematicsHandler struct {
repo domain.CourseRepository
mapper domain.CourseMapper
}
func NewListCourseThematicsHandler(
repo domain.CourseRepository,
mapper domain.CourseMapper,
log *slog.Logger,
) ListCourseThematicsHandler {
h := listCourseThematicsHandler{
repo: repo,
mapper: mapper,
}
return decorator.AddQueryDecorators(h, log)
}
func (h listCourseThematicsHandler) Handle(ctx context.Context, query ListCourseThematics) (out ListCourseThematicsResult, err error) {
result, err := h.repo.ListCourseThematics(ctx, domain.ListCourseThematicsParams{
LearningTypeID: query.LearningTypeID,
})
if err != nil {
return out, fmt.Errorf("listing course thematics from repo: %w", err)
}
out.CourseThematics = make([]CourseThematic, 0, len(result.CourseThematicIDs))
for _, ct := range result.CourseThematicIDs {
var item CourseThematic
item.ID = ct
item.Name = h.mapper.CourseThematicNameByID(ct)
out.CourseThematics = append(out.CourseThematics, item)
}
return out, nil
}