65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
|
|
"git.loyso.art/frx/kurious/internal/common/config"
|
|
"git.loyso.art/frx/kurious/internal/kurious/adapters"
|
|
"git.loyso.art/frx/kurious/internal/kurious/app"
|
|
"git.loyso.art/frx/kurious/internal/kurious/app/command"
|
|
"git.loyso.art/frx/kurious/internal/kurious/app/query"
|
|
)
|
|
|
|
type ApplicationConfig struct {
|
|
LogConfig config.Log
|
|
YDB config.YDB
|
|
}
|
|
|
|
type Application struct {
|
|
app.Application
|
|
|
|
log *slog.Logger
|
|
closers []io.Closer
|
|
}
|
|
|
|
func NewApplication(ctx context.Context, cfg ApplicationConfig) (Application, error) {
|
|
log := config.NewSLogger(cfg.LogConfig)
|
|
ydbConnection, err := adapters.NewYDBConnection(ctx, cfg.YDB, log.With(slog.String("db", "ydb")))
|
|
if err != nil {
|
|
return Application{}, fmt.Errorf("making ydb connection: %w", err)
|
|
}
|
|
|
|
courseadapter := ydbConnection.CourseRepository()
|
|
|
|
application := app.Application{
|
|
Commands: app.Commands{
|
|
InsertCourses: command.NewCreateCoursesHandler(courseadapter, log),
|
|
InsertCourse: command.NewCreateCourseHandler(courseadapter, log),
|
|
DeleteCourse: command.NewDeleteCourseHandler(courseadapter, log),
|
|
UpdateCourseDescription: command.NewUpdateCourseDescriptionHandler(courseadapter, log),
|
|
},
|
|
Queries: app.Queries{
|
|
GetCourse: query.NewGetCourseHandler(courseadapter, log),
|
|
ListCourses: query.NewListCourseHandler(courseadapter, log),
|
|
},
|
|
}
|
|
|
|
out := Application{Application: application}
|
|
out.closers = append(out.closers, ydbConnection)
|
|
out.log = log
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func (app Application) Close() {
|
|
for _, closer := range app.closers {
|
|
err := closer.Close()
|
|
if err != nil {
|
|
app.log.Error("unable to close closer", slog.Any("err", err))
|
|
}
|
|
}
|
|
}
|