167 lines
3.5 KiB
Go
167 lines
3.5 KiB
Go
package repository
|
|
|
|
import (
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) CreateCourse(
|
|
ctx context.Context,
|
|
categoryID int64,
|
|
title string,
|
|
description *string,
|
|
thumbnail *string,
|
|
introVideoURL *string,
|
|
) (domain.Course, error) {
|
|
var descVal, thumbVal, introVideoVal string
|
|
if description != nil {
|
|
descVal = *description
|
|
}
|
|
if thumbnail != nil {
|
|
thumbVal = *thumbnail
|
|
}
|
|
if introVideoURL != nil {
|
|
introVideoVal = *introVideoURL
|
|
}
|
|
|
|
row, err := s.queries.CreateCourse(ctx, dbgen.CreateCourseParams{
|
|
CategoryID: categoryID,
|
|
Title: title,
|
|
Description: pgtype.Text{String: descVal, Valid: description != nil},
|
|
Thumbnail: pgtype.Text{String: thumbVal, Valid: thumbnail != nil},
|
|
IntroVideoUrl: pgtype.Text{String: introVideoVal, Valid: introVideoURL != nil},
|
|
Column6: true,
|
|
})
|
|
if err != nil {
|
|
return domain.Course{}, err
|
|
}
|
|
|
|
return mapCourse(row), nil
|
|
}
|
|
|
|
func (s *Store) GetCourseByID(
|
|
ctx context.Context,
|
|
id int64,
|
|
) (domain.Course, error) {
|
|
|
|
row, err := s.queries.GetCourseByID(ctx, id)
|
|
if err != nil {
|
|
return domain.Course{}, err
|
|
}
|
|
|
|
return mapCourse(row), nil
|
|
}
|
|
|
|
func (s *Store) GetCoursesByCategory(
|
|
ctx context.Context,
|
|
categoryID int64,
|
|
limit int32,
|
|
offset int32,
|
|
) ([]domain.Course, int64, error) {
|
|
|
|
rows, err := s.queries.GetCoursesByCategory(ctx, dbgen.GetCoursesByCategoryParams{
|
|
CategoryID: categoryID,
|
|
Limit: pgtype.Int4{Int32: limit},
|
|
Offset: pgtype.Int4{Int32: offset},
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var (
|
|
courses []domain.Course
|
|
totalCount int64
|
|
)
|
|
|
|
for i, row := range rows {
|
|
if i == 0 {
|
|
totalCount = row.TotalCount
|
|
}
|
|
|
|
courses = append(courses, domain.Course{
|
|
ID: row.ID,
|
|
CategoryID: row.CategoryID,
|
|
Title: row.Title,
|
|
Description: ptrText(row.Description),
|
|
Thumbnail: ptrText(row.Thumbnail),
|
|
IntroVideoURL: ptrText(row.IntroVideoUrl),
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return courses, totalCount, nil
|
|
}
|
|
|
|
func (s *Store) UpdateCourse(
|
|
ctx context.Context,
|
|
id int64,
|
|
title *string,
|
|
description *string,
|
|
thumbnail *string,
|
|
introVideoURL *string,
|
|
isActive *bool,
|
|
) error {
|
|
var (
|
|
titleVal string
|
|
descriptionVal string
|
|
thumbnailVal string
|
|
introVideoVal string
|
|
isActiveVal bool
|
|
)
|
|
|
|
if title != nil {
|
|
titleVal = *title
|
|
}
|
|
if description != nil {
|
|
descriptionVal = *description
|
|
}
|
|
if thumbnail != nil {
|
|
thumbnailVal = *thumbnail
|
|
}
|
|
if introVideoURL != nil {
|
|
introVideoVal = *introVideoURL
|
|
}
|
|
if isActive != nil {
|
|
isActiveVal = *isActive
|
|
}
|
|
|
|
return s.queries.UpdateCourse(ctx, dbgen.UpdateCourseParams{
|
|
Title: titleVal,
|
|
Description: pgtype.Text{String: descriptionVal, Valid: description != nil},
|
|
Thumbnail: pgtype.Text{String: thumbnailVal, Valid: thumbnail != nil},
|
|
IntroVideoUrl: pgtype.Text{String: introVideoVal, Valid: introVideoURL != nil},
|
|
IsActive: isActiveVal,
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) DeleteCourse(
|
|
ctx context.Context,
|
|
id int64,
|
|
) error {
|
|
|
|
return s.queries.DeleteCourse(ctx, id)
|
|
}
|
|
|
|
func mapCourse(row dbgen.Course) domain.Course {
|
|
return domain.Course{
|
|
ID: row.ID,
|
|
CategoryID: row.CategoryID,
|
|
Title: row.Title,
|
|
Description: ptrText(row.Description),
|
|
Thumbnail: ptrText(row.Thumbnail),
|
|
IntroVideoURL: ptrText(row.IntroVideoUrl),
|
|
IsActive: row.IsActive,
|
|
}
|
|
}
|
|
|
|
func ptrText(t pgtype.Text) *string {
|
|
if t.Valid {
|
|
return &t.String
|
|
}
|
|
return nil
|
|
}
|