121 lines
3.4 KiB
Go
121 lines
3.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func examPrepModuleToDomain(m dbgen.ExamPrepUnitModule) domain.ExamPrepModule {
|
|
out := domain.ExamPrepModule{
|
|
ID: m.ID,
|
|
UnitID: m.UnitID,
|
|
Name: m.Name,
|
|
SortOrder: int(m.SortOrder),
|
|
}
|
|
out.Description = fromPgText(m.Description)
|
|
out.Thumbnail = fromPgText(m.Thumbnail)
|
|
out.Icon = fromPgText(m.Icon)
|
|
out.CreatedAt = m.CreatedAt.Time
|
|
if m.UpdatedAt.Valid {
|
|
t := m.UpdatedAt.Time
|
|
out.UpdatedAt = &t
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Store) CreateExamPrepUnitModule(ctx context.Context, unitID int64, input domain.CreateExamPrepModuleInput) (domain.ExamPrepModule, error) {
|
|
m, err := s.queries.ExamPrepCreateUnitModule(ctx, dbgen.ExamPrepCreateUnitModuleParams{
|
|
UnitID: unitID,
|
|
Name: input.Name,
|
|
Description: toPgText(input.Description),
|
|
Thumbnail: toPgText(input.Thumbnail),
|
|
Icon: toPgText(input.Icon),
|
|
})
|
|
if err != nil {
|
|
return domain.ExamPrepModule{}, err
|
|
}
|
|
return examPrepModuleToDomain(m), nil
|
|
}
|
|
|
|
func (s *Store) GetExamPrepUnitModuleByID(ctx context.Context, id int64) (domain.ExamPrepModule, error) {
|
|
m, err := s.queries.ExamPrepGetUnitModuleByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.ExamPrepModule{}, pgx.ErrNoRows
|
|
}
|
|
return domain.ExamPrepModule{}, err
|
|
}
|
|
return examPrepModuleToDomain(m), nil
|
|
}
|
|
|
|
func (s *Store) ListExamPrepUnitModulesByUnit(ctx context.Context, unitID int64, limit, offset int32) ([]domain.ExamPrepModule, int64, error) {
|
|
rows, err := s.queries.ExamPrepListUnitModulesByUnit(ctx, dbgen.ExamPrepListUnitModulesByUnitParams{
|
|
UnitID: unitID,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return []domain.ExamPrepModule{}, 0, nil
|
|
}
|
|
var total int64
|
|
out := make([]domain.ExamPrepModule, 0, len(rows))
|
|
for i, r := range rows {
|
|
if i == 0 {
|
|
total = r.TotalCount
|
|
}
|
|
out = append(out, examPrepModuleToDomain(dbgen.ExamPrepUnitModule{
|
|
ID: r.ID,
|
|
UnitID: r.UnitID,
|
|
Name: r.Name,
|
|
Description: r.Description,
|
|
Thumbnail: r.Thumbnail,
|
|
Icon: r.Icon,
|
|
SortOrder: r.SortOrder,
|
|
CreatedAt: r.CreatedAt,
|
|
UpdatedAt: r.UpdatedAt,
|
|
}))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
|
|
func (s *Store) ListExamPrepUnitModuleIDsByUnit(ctx context.Context, unitID int64) ([]int64, error) {
|
|
return s.queries.ExamPrepListUnitModuleIDsByUnit(ctx, unitID)
|
|
}
|
|
|
|
func (s *Store) UpdateExamPrepUnitModule(ctx context.Context, id int64, input domain.UpdateExamPrepModuleInput) (domain.ExamPrepModule, error) {
|
|
var nameText pgtype.Text
|
|
if input.Name != nil {
|
|
nameText = pgtype.Text{String: *input.Name, Valid: true}
|
|
} else {
|
|
nameText = pgtype.Text{Valid: false}
|
|
}
|
|
m, err := s.queries.ExamPrepUpdateUnitModule(ctx, dbgen.ExamPrepUpdateUnitModuleParams{
|
|
ID: id,
|
|
Name: nameText,
|
|
Description: optionalTextUpdate(input.Description),
|
|
Thumbnail: optionalTextUpdate(input.Thumbnail),
|
|
Icon: optionalTextUpdate(input.Icon),
|
|
SortOrder: optionalInt4Update(input.SortOrder),
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.ExamPrepModule{}, pgx.ErrNoRows
|
|
}
|
|
return domain.ExamPrepModule{}, err
|
|
}
|
|
return examPrepModuleToDomain(m), nil
|
|
}
|
|
|
|
func (s *Store) DeleteExamPrepUnitModule(ctx context.Context, id int64) error {
|
|
return s.queries.ExamPrepDeleteUnitModule(ctx, id)
|
|
}
|