- Implement standalone Go backend API server using Gin and pure-Go SQLite (modernc.org/sqlite) - Add 11-table schema DDL migrations (000001_init.up.sql) based on DATA_TABLE.md - Add seed data ingestion tool (cmd/seed) and static TypeScript content exporter (cmd/export-content) - Add configuration loader (internal/config) supporting environment variables and .env files (HOST, PORT, DB_PATH, GIN_MODE, CORS_ALLOW_ORIGINS, AUTO_MIGRATE) - Add lightweight multi-stage Dockerfile and docker-compose.yml with persistent SQLite volume - Add comprehensive architecture specification (ARCHITECTURE.md) and REST API interface specification (API_INTERFACE.md) - Harmonize frontend publications page to consume isHighlight flag from database-synced content
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"git.godopu.com/lab/landing_page/backend/internal/models"
|
|
)
|
|
|
|
type LecturesRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewLecturesRepository(db *sql.DB) *LecturesRepository {
|
|
return &LecturesRepository{db: db}
|
|
}
|
|
|
|
func (r *LecturesRepository) GetSemestersWithCourses(ctx context.Context) ([]models.SemesterWithCourses, error) {
|
|
// Query semesters ordered by year DESC and Fall before Spring within same year
|
|
semQuery := `
|
|
SELECT id, year, term
|
|
FROM semesters
|
|
ORDER BY year DESC, CASE term WHEN 'Fall' THEN 1 WHEN 'Spring' THEN 2 ELSE 3 END, id DESC
|
|
`
|
|
semRows, err := r.db.QueryContext(ctx, semQuery)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query semesters: %w", err)
|
|
}
|
|
defer semRows.Close()
|
|
|
|
var semesters []models.SemesterWithCourses
|
|
semMap := make(map[int]int) // semester_id -> index in slice
|
|
|
|
for semRows.Next() {
|
|
var s models.SemesterWithCourses
|
|
if err := semRows.Scan(&s.ID, &s.Year, &s.Term); err != nil {
|
|
return nil, fmt.Errorf("scan semester: %w", err)
|
|
}
|
|
s.Courses = []models.Course{}
|
|
semMap[s.ID] = len(semesters)
|
|
semesters = append(semesters, s)
|
|
}
|
|
if err := semRows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Query courses ordered by display_order ASC, id ASC
|
|
courseQuery := `
|
|
SELECT id, semester_id, code, name_en, name_kr, note, display_order
|
|
FROM courses
|
|
ORDER BY semester_id ASC, display_order ASC, id ASC
|
|
`
|
|
courseRows, err := r.db.QueryContext(ctx, courseQuery)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query courses: %w", err)
|
|
}
|
|
defer courseRows.Close()
|
|
|
|
for courseRows.Next() {
|
|
var c models.Course
|
|
if err := courseRows.Scan(&c.ID, &c.SemesterID, &c.Code, &c.NameEn, &c.NameKr, &c.Note, &c.DisplayOrder); err != nil {
|
|
return nil, fmt.Errorf("scan course: %w", err)
|
|
}
|
|
if idx, exists := semMap[c.SemesterID]; exists {
|
|
semesters[idx].Courses = append(semesters[idx].Courses, c)
|
|
}
|
|
}
|
|
|
|
return semesters, courseRows.Err()
|
|
}
|