162 lines
3.5 KiB
Go
162 lines
3.5 KiB
Go
package repository
|
|
|
|
import (
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) CreateModuleVideo(
|
|
ctx context.Context,
|
|
moduleID int64,
|
|
title string,
|
|
description *string,
|
|
videoURL string,
|
|
duration int32,
|
|
resolution *string,
|
|
instructorID *string,
|
|
thumbnail *string,
|
|
visibility *string,
|
|
) (domain.ModuleVideo, error) {
|
|
|
|
row, err := s.queries.CreateModuleVideo(ctx, dbgen.CreateModuleVideoParams{
|
|
ModuleID: moduleID,
|
|
Title: title,
|
|
Description: pgtype.Text{String: *description},
|
|
VideoUrl: videoURL,
|
|
Duration: duration,
|
|
Resolution: pgtype.Text{String: *resolution},
|
|
InstructorID: pgtype.Text{String: *instructorID},
|
|
Thumbnail: pgtype.Text{String: *thumbnail},
|
|
Visibility: pgtype.Text{String: *visibility},
|
|
Column10: true,
|
|
})
|
|
if err != nil {
|
|
return domain.ModuleVideo{}, err
|
|
}
|
|
|
|
var publishDate *time.Time
|
|
if row.PublishDate.Valid {
|
|
publishDate = &row.PublishDate.Time
|
|
}
|
|
|
|
return domain.ModuleVideo{
|
|
ID: row.ID,
|
|
ModuleID: row.ModuleID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
VideoURL: row.VideoUrl,
|
|
Duration: row.Duration,
|
|
Resolution: &row.Resolution.String,
|
|
InstructorID: &row.InstructorID.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
Visibility: &row.Visibility.String,
|
|
IsPublished: row.IsPublished,
|
|
PublishDate: publishDate,
|
|
IsActive: row.IsActive,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) PublishModuleVideo(
|
|
ctx context.Context,
|
|
videoID int64,
|
|
) error {
|
|
|
|
return s.queries.PublishModuleVideo(ctx, videoID)
|
|
}
|
|
|
|
func (s *Store) GetPublishedVideosByModule(
|
|
ctx context.Context,
|
|
moduleID int64,
|
|
) ([]domain.ModuleVideo, error) {
|
|
|
|
rows, err := s.queries.GetPublishedVideosByModule(ctx, moduleID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
videos := make([]domain.ModuleVideo, 0, len(rows))
|
|
for _, row := range rows {
|
|
|
|
var publishDate *time.Time
|
|
if row.PublishDate.Valid {
|
|
publishDate = &row.PublishDate.Time
|
|
}
|
|
|
|
videos = append(videos, domain.ModuleVideo{
|
|
ID: row.ID,
|
|
ModuleID: row.ModuleID,
|
|
Title: row.Title,
|
|
Description: &row.Description.String,
|
|
VideoURL: row.VideoUrl,
|
|
Duration: row.Duration,
|
|
Resolution: &row.Resolution.String,
|
|
InstructorID: &row.InstructorID.String,
|
|
Thumbnail: &row.Thumbnail.String,
|
|
Visibility: &row.Visibility.String,
|
|
IsPublished: row.IsPublished,
|
|
PublishDate: publishDate,
|
|
IsActive: row.IsActive,
|
|
})
|
|
}
|
|
|
|
return videos, nil
|
|
}
|
|
|
|
func (s *Store) UpdateModuleVideo(
|
|
ctx context.Context,
|
|
id int64,
|
|
title *string,
|
|
description *string,
|
|
videoURL *string,
|
|
duration *int32,
|
|
resolution *string,
|
|
visibility *string,
|
|
thumbnail *string,
|
|
isActive *bool,
|
|
) error {
|
|
|
|
return s.queries.UpdateModuleVideo(ctx, dbgen.UpdateModuleVideoParams{
|
|
Title: func() string {
|
|
if title != nil {
|
|
return *title
|
|
}
|
|
return ""
|
|
}(),
|
|
Description: pgtype.Text{String: *description},
|
|
VideoUrl: func() string {
|
|
if videoURL != nil {
|
|
return *videoURL
|
|
}
|
|
return ""
|
|
}(),
|
|
Duration: func() int32 {
|
|
if duration != nil {
|
|
return *duration
|
|
}
|
|
return 0
|
|
}(),
|
|
Resolution: pgtype.Text{String: *resolution},
|
|
Visibility: pgtype.Text{String: *visibility},
|
|
Thumbnail: pgtype.Text{String: *thumbnail},
|
|
IsActive: func() bool {
|
|
if isActive != nil {
|
|
return *isActive
|
|
}
|
|
return false
|
|
}(),
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) DeleteModuleVideo(
|
|
ctx context.Context,
|
|
id int64,
|
|
) error {
|
|
|
|
return s.queries.DeleteModuleVideo(ctx, id)
|
|
}
|