241 lines
5.1 KiB
Go
241 lines
5.1 KiB
Go
package repository
|
|
|
|
import (
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) CreateProgram(
|
|
ctx context.Context,
|
|
courseID int64,
|
|
title string,
|
|
description *string,
|
|
thumbnail *string,
|
|
displayOrder *int32,
|
|
) (domain.Program, error) {
|
|
|
|
row, err := s.queries.CreateProgram(ctx, dbgen.CreateProgramParams{
|
|
CourseID: courseID,
|
|
Title: title,
|
|
Description: pgtype.Text{String: *description},
|
|
Thumbnail: pgtype.Text{String: *thumbnail},
|
|
Column5: displayOrder,
|
|
Column6: true,
|
|
})
|
|
if err != nil {
|
|
return domain.Program{}, err
|
|
}
|
|
|
|
return domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) GetProgramByID(
|
|
ctx context.Context,
|
|
id int64,
|
|
) (domain.Program, error) {
|
|
|
|
row, err := s.queries.GetProgramByID(ctx, id)
|
|
if err != nil {
|
|
return domain.Program{}, err
|
|
}
|
|
|
|
return domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) GetProgramsByCourse(
|
|
ctx context.Context,
|
|
courseID int64,
|
|
) ([]domain.Program, int64, error) {
|
|
|
|
rows, err := s.queries.GetProgramsByCourse(ctx, courseID)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var (
|
|
programs []domain.Program
|
|
totalCount int64
|
|
)
|
|
|
|
for i, row := range rows {
|
|
if i == 0 {
|
|
totalCount = row.TotalCount
|
|
}
|
|
|
|
programs = append(programs, domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return programs, totalCount, nil
|
|
}
|
|
|
|
func (s *Store) ListProgramsByCourse(
|
|
ctx context.Context,
|
|
courseID int64,
|
|
) ([]domain.Program, error) {
|
|
|
|
rows, err := s.queries.ListProgramsByCourse(ctx, courseID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
programs := make([]domain.Program, 0, len(rows))
|
|
for _, row := range rows {
|
|
programs = append(programs, domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return programs, nil
|
|
}
|
|
|
|
func (s *Store) ListActivePrograms(
|
|
ctx context.Context,
|
|
) ([]domain.Program, error) {
|
|
|
|
rows, err := s.queries.ListActivePrograms(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
programs := make([]domain.Program, 0, len(rows))
|
|
for _, row := range rows {
|
|
programs = append(programs, domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return programs, nil
|
|
}
|
|
|
|
func (s *Store) UpdateProgramPartial(
|
|
ctx context.Context,
|
|
id int64,
|
|
title *string,
|
|
description *string,
|
|
thumbnail *string,
|
|
displayOrder *int32,
|
|
isActive *bool,
|
|
) error {
|
|
|
|
return s.queries.UpdateProgramPartial(ctx, dbgen.UpdateProgramPartialParams{
|
|
Title: func() string {
|
|
if title != nil {
|
|
return *title
|
|
}
|
|
return ""
|
|
}(),
|
|
Description: pgtype.Text{String: *description},
|
|
Thumbnail: pgtype.Text{String: *thumbnail},
|
|
DisplayOrder: func() int32 {
|
|
if displayOrder != nil {
|
|
return *displayOrder
|
|
}
|
|
return 0
|
|
}(),
|
|
IsActive: func() bool {
|
|
if isActive != nil {
|
|
return *isActive
|
|
}
|
|
return false
|
|
}(),
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) UpdateProgramFull(
|
|
ctx context.Context,
|
|
program domain.Program,
|
|
) (domain.Program, error) {
|
|
|
|
row, err := s.queries.UpdateProgramFull(ctx, dbgen.UpdateProgramFullParams{
|
|
ID: program.ID,
|
|
CourseID: program.CourseID,
|
|
Title: program.Title,
|
|
Description: pgtype.Text{String: *program.Description},
|
|
Thumbnail: pgtype.Text{String: *program.Thumbnail},
|
|
DisplayOrder: program.DisplayOrder,
|
|
IsActive: program.IsActive,
|
|
})
|
|
if err != nil {
|
|
return domain.Program{}, err
|
|
}
|
|
|
|
return domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) DeactivateProgram(
|
|
ctx context.Context,
|
|
id int64,
|
|
) error {
|
|
|
|
return s.queries.DeactivateProgram(ctx, id)
|
|
}
|
|
|
|
func (s *Store) DeleteProgram(
|
|
ctx context.Context,
|
|
id int64,
|
|
) (domain.Program, error) {
|
|
|
|
row, err := s.queries.DeleteProgram(ctx, id)
|
|
if err != nil {
|
|
return domain.Program{}, err
|
|
}
|
|
|
|
return domain.Program{
|
|
ID: row.ID,
|
|
CourseID: row.CourseID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|