126 lines
2.7 KiB
Go
126 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) CreateLevel(
|
|
ctx context.Context,
|
|
programID int64,
|
|
title string,
|
|
description *string,
|
|
levelIndex int,
|
|
isActive *bool,
|
|
) (domain.Level, error) {
|
|
|
|
row, err := s.queries.CreateLevel(ctx, dbgen.CreateLevelParams{
|
|
ProgramID: programID,
|
|
Title: title,
|
|
Description: pgtype.Text{String: *description},
|
|
LevelIndex: int32(levelIndex),
|
|
Column5: isActive,
|
|
})
|
|
if err != nil {
|
|
return domain.Level{}, err
|
|
}
|
|
|
|
return domain.Level{
|
|
ID: row.ID,
|
|
ProgramID: row.ProgramID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
LevelIndex: int(row.LevelIndex),
|
|
NumberOfModules: int(row.NumberOfModules),
|
|
NumberOfPractices: int(row.NumberOfPractices),
|
|
NumberOfVideos: int(row.NumberOfVideos),
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) GetLevelsByProgram(
|
|
ctx context.Context,
|
|
programID int64,
|
|
) ([]domain.Level, error) {
|
|
|
|
rows, err := s.queries.GetLevelsByProgram(ctx, programID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
levels := make([]domain.Level, 0, len(rows))
|
|
for _, row := range rows {
|
|
levels = append(levels, domain.Level{
|
|
ID: row.ID,
|
|
ProgramID: row.ProgramID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
LevelIndex: int(row.LevelIndex),
|
|
NumberOfModules: int(row.NumberOfModules),
|
|
NumberOfPractices: int(row.NumberOfPractices),
|
|
NumberOfVideos: int(row.NumberOfVideos),
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return levels, nil
|
|
}
|
|
|
|
func (s *Store) UpdateLevel(
|
|
ctx context.Context,
|
|
id int64,
|
|
title *string,
|
|
description *string,
|
|
levelIndex *int,
|
|
isActive *bool,
|
|
) error {
|
|
|
|
return s.queries.UpdateLevel(ctx, dbgen.UpdateLevelParams{
|
|
Title: func() string {
|
|
if title != nil {
|
|
return *title
|
|
}
|
|
return ""
|
|
}(),
|
|
Description: pgtype.Text{String: *description},
|
|
LevelIndex: int32(*levelIndex),
|
|
IsActive: *isActive,
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) IncrementLevelModuleCount(
|
|
ctx context.Context,
|
|
levelID int64,
|
|
) error {
|
|
|
|
return s.queries.IncrementLevelModuleCount(ctx, levelID)
|
|
}
|
|
|
|
func (s *Store) IncrementLevelPracticeCount(
|
|
ctx context.Context,
|
|
levelID int64,
|
|
) error {
|
|
|
|
return s.queries.IncrementLevelPracticeCount(ctx, levelID)
|
|
}
|
|
|
|
func (s *Store) IncrementLevelVideoCount(
|
|
ctx context.Context,
|
|
levelID int64,
|
|
) error {
|
|
|
|
return s.queries.IncrementLevelVideoCount(ctx, levelID)
|
|
}
|
|
|
|
func (s *Store) DeleteLevel(
|
|
ctx context.Context,
|
|
levelID int64,
|
|
) error {
|
|
|
|
return s.queries.DeleteLevel(ctx, levelID)
|
|
}
|