113 lines
2.0 KiB
Go
113 lines
2.0 KiB
Go
package repository
|
|
|
|
import (
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) CreateModule(
|
|
ctx context.Context,
|
|
levelID int64,
|
|
title string,
|
|
content *string,
|
|
displayOrder *int32,
|
|
) (domain.Module, error) {
|
|
|
|
row, err := s.queries.CreateModule(ctx, dbgen.CreateModuleParams{
|
|
LevelID: levelID,
|
|
Title: title,
|
|
Content: pgtype.Text{String: *content},
|
|
Column4: displayOrder,
|
|
Column5: true,
|
|
})
|
|
if err != nil {
|
|
return domain.Module{}, err
|
|
}
|
|
|
|
return domain.Module{
|
|
ID: row.ID,
|
|
LevelID: row.LevelID,
|
|
Title: row.Title,
|
|
Content: &row.Content.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) GetModulesByLevel(
|
|
ctx context.Context,
|
|
levelID int64,
|
|
) ([]domain.Module, int64, error) {
|
|
|
|
rows, err := s.queries.GetModulesByLevel(ctx, levelID)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var (
|
|
modules []domain.Module
|
|
totalCount int64
|
|
)
|
|
|
|
for i, row := range rows {
|
|
if i == 0 {
|
|
totalCount = row.TotalCount
|
|
}
|
|
|
|
modules = append(modules, domain.Module{
|
|
ID: row.ID,
|
|
LevelID: row.LevelID,
|
|
Title: row.Title,
|
|
Content: &row.Content.String,
|
|
DisplayOrder: row.DisplayOrder,
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return modules, totalCount, nil
|
|
}
|
|
|
|
func (s *Store) UpdateModule(
|
|
ctx context.Context,
|
|
id int64,
|
|
title *string,
|
|
content *string,
|
|
displayOrder *int32,
|
|
isActive *bool,
|
|
) error {
|
|
|
|
titleVal := ""
|
|
if title != nil {
|
|
titleVal = *title
|
|
}
|
|
|
|
var displayOrderVal int32
|
|
if displayOrder != nil {
|
|
displayOrderVal = *displayOrder
|
|
}
|
|
|
|
var isActiveVal bool
|
|
if isActive != nil {
|
|
isActiveVal = *isActive
|
|
}
|
|
|
|
return s.queries.UpdateModule(ctx, dbgen.UpdateModuleParams{
|
|
Title: titleVal,
|
|
Content: pgtype.Text{String: *content},
|
|
DisplayOrder: displayOrderVal,
|
|
IsActive: isActiveVal,
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) DeleteModule(
|
|
ctx context.Context,
|
|
id int64,
|
|
) error {
|
|
|
|
return s.queries.DeleteModule(ctx, id)
|
|
}
|