125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
|
"git.sch9.ru/new_gate/ms-tester/internal/tester"
|
|
"git.sch9.ru/new_gate/ms-tester/pkg"
|
|
)
|
|
|
|
type ProblemUseCase struct {
|
|
problemRepo tester.ProblemPostgresRepository
|
|
//pandocClient pkg.PandocClient
|
|
}
|
|
|
|
func NewProblemUseCase(
|
|
problemRepo tester.ProblemPostgresRepository,
|
|
// pandocClient pkg.PandocClient,
|
|
) *ProblemUseCase {
|
|
return &ProblemUseCase{
|
|
problemRepo: problemRepo,
|
|
//pandocClient: pandocClient,
|
|
}
|
|
}
|
|
|
|
func (u *ProblemUseCase) CreateProblem(ctx context.Context, title string) (int32, error) {
|
|
return u.problemRepo.CreateProblem(ctx, u.problemRepo.DB(), title)
|
|
}
|
|
|
|
func (u *ProblemUseCase) ReadProblemById(ctx context.Context, id int32) (*models.Problem, error) {
|
|
return u.problemRepo.ReadProblemById(ctx, u.problemRepo.DB(), id)
|
|
}
|
|
|
|
func (u *ProblemUseCase) DeleteProblem(ctx context.Context, id int32) error {
|
|
return u.problemRepo.DeleteProblem(ctx, u.problemRepo.DB(), id)
|
|
}
|
|
|
|
func (u *ProblemUseCase) ListProblems(ctx context.Context, page int32, pageSize int32) ([]*models.ProblemListItem, int32, error) {
|
|
return u.problemRepo.ListProblems(ctx, u.problemRepo.DB(), page, pageSize)
|
|
}
|
|
|
|
func isEmpty(p models.ProblemUpdate) bool {
|
|
return p.Title == nil &&
|
|
p.Legend == nil &&
|
|
p.InputFormat == nil &&
|
|
p.OutputFormat == nil &&
|
|
p.Notes == nil &&
|
|
p.Tutorial == nil &&
|
|
p.LatexSummary == nil &&
|
|
p.MemoryLimit == nil &&
|
|
p.TimeLimit == nil
|
|
}
|
|
|
|
func build(p models.ProblemStatement) string {
|
|
return ""
|
|
}
|
|
|
|
func (u *ProblemUseCase) UpdateProblem(ctx context.Context, id int32, problemUpdate models.ProblemUpdate) error {
|
|
if isEmpty(problemUpdate) {
|
|
return pkg.Wrap(pkg.ErrBadInput, nil, "UpdateProblem", "empty problem update")
|
|
}
|
|
|
|
tx, err := u.problemRepo.BeginTx(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
problem, err := u.problemRepo.ReadProblemById(ctx, tx, id)
|
|
if err != nil {
|
|
return errors.Join(err, tx.Rollback())
|
|
}
|
|
|
|
statement := models.ProblemStatement{
|
|
Title: problem.Title,
|
|
Legend: problem.Legend,
|
|
InputFormat: problem.InputFormat,
|
|
OutputFormat: problem.OutputFormat,
|
|
Notes: problem.Notes,
|
|
Tutorial: problem.Tutorial,
|
|
TimeLimit: problem.TimeLimit,
|
|
MemoryLimit: problem.MemoryLimit,
|
|
}
|
|
|
|
if problemUpdate.Title != nil {
|
|
statement.Title = *problemUpdate.Title
|
|
}
|
|
if problemUpdate.Legend != nil {
|
|
statement.Legend = *problemUpdate.Legend
|
|
}
|
|
if problemUpdate.InputFormat != nil {
|
|
statement.InputFormat = *problemUpdate.InputFormat
|
|
}
|
|
if problemUpdate.OutputFormat != nil {
|
|
statement.OutputFormat = *problemUpdate.OutputFormat
|
|
}
|
|
if problemUpdate.Notes != nil {
|
|
statement.Notes = *problemUpdate.Notes
|
|
}
|
|
if problemUpdate.Tutorial != nil {
|
|
statement.Tutorial = *problemUpdate.Tutorial
|
|
}
|
|
if problemUpdate.TimeLimit != nil {
|
|
statement.TimeLimit = *problemUpdate.TimeLimit
|
|
}
|
|
if problemUpdate.MemoryLimit != nil {
|
|
statement.MemoryLimit = *problemUpdate.MemoryLimit
|
|
}
|
|
|
|
builtStatement := build(statement)
|
|
if builtStatement != problem.LatexSummary {
|
|
problemUpdate.LatexSummary = &builtStatement
|
|
}
|
|
|
|
err = u.problemRepo.UpdateProblem(ctx, tx, id, problemUpdate)
|
|
if err != nil {
|
|
return errors.Join(err, tx.Rollback())
|
|
}
|
|
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|