117 lines
3.0 KiB
Go
117 lines
3.0 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 courseToDomain(c dbgen.Course) domain.Course {
|
|
out := domain.Course{
|
|
ID: c.ID,
|
|
ProgramID: c.ProgramID,
|
|
Name: c.Name,
|
|
}
|
|
out.Description = fromPgText(c.Description)
|
|
out.Thumbnail = fromPgText(c.Thumbnail)
|
|
out.CreatedAt = c.CreatedAt.Time
|
|
if c.UpdatedAt.Valid {
|
|
t := c.UpdatedAt.Time
|
|
out.UpdatedAt = &t
|
|
}
|
|
out.SortOrder = int(c.SortOrder)
|
|
return out
|
|
}
|
|
|
|
func (s *Store) CreateCourse(ctx context.Context, programID int64, input domain.CreateCourseInput) (domain.Course, error) {
|
|
c, err := s.queries.CreateCourse(ctx, dbgen.CreateCourseParams{
|
|
ProgramID: programID,
|
|
Name: input.Name,
|
|
Description: toPgText(input.Description),
|
|
Thumbnail: toPgText(input.Thumbnail),
|
|
})
|
|
if err != nil {
|
|
return domain.Course{}, err
|
|
}
|
|
return courseToDomain(c), nil
|
|
}
|
|
|
|
func (s *Store) ListCourseIDsByProgram(ctx context.Context, programID int64) ([]int64, error) {
|
|
return s.queries.ListCourseIDsByProgram(ctx, programID)
|
|
}
|
|
|
|
func (s *Store) GetCourseByID(ctx context.Context, id int64) (domain.Course, error) {
|
|
c, err := s.queries.GetCourseByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.Course{}, pgx.ErrNoRows
|
|
}
|
|
return domain.Course{}, err
|
|
}
|
|
return courseToDomain(c), nil
|
|
}
|
|
|
|
func (s *Store) ListCoursesByProgramID(ctx context.Context, programID int64, limit, offset int32) ([]domain.Course, int64, error) {
|
|
rows, err := s.queries.ListCoursesByProgramID(ctx, dbgen.ListCoursesByProgramIDParams{
|
|
ProgramID: programID,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return []domain.Course{}, 0, nil
|
|
}
|
|
var total int64
|
|
out := make([]domain.Course, 0, len(rows))
|
|
for i, r := range rows {
|
|
if i == 0 {
|
|
total = r.TotalCount
|
|
}
|
|
out = append(out, courseToDomain(dbgen.Course{
|
|
ID: r.ID,
|
|
ProgramID: r.ProgramID,
|
|
Name: r.Name,
|
|
Description: r.Description,
|
|
Thumbnail: r.Thumbnail,
|
|
CreatedAt: r.CreatedAt,
|
|
UpdatedAt: r.UpdatedAt,
|
|
SortOrder: r.SortOrder,
|
|
}))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
|
|
func (s *Store) UpdateCourse(ctx context.Context, id int64, input domain.UpdateCourseInput) (domain.Course, error) {
|
|
var nameText pgtype.Text
|
|
if input.Name != nil {
|
|
nameText = pgtype.Text{String: *input.Name, Valid: true}
|
|
} else {
|
|
nameText = pgtype.Text{Valid: false}
|
|
}
|
|
c, err := s.queries.UpdateCourse(ctx, dbgen.UpdateCourseParams{
|
|
ID: id,
|
|
Name: nameText,
|
|
Description: optionalTextUpdate(input.Description),
|
|
Thumbnail: optionalTextUpdate(input.Thumbnail),
|
|
SortOrder: optionalInt4Update(input.SortOrder),
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.Course{}, pgx.ErrNoRows
|
|
}
|
|
return domain.Course{}, err
|
|
}
|
|
return courseToDomain(c), nil
|
|
}
|
|
|
|
func (s *Store) DeleteCourse(ctx context.Context, id int64) error {
|
|
return s.queries.DeleteCourse(ctx, id)
|
|
}
|