- 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
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"git.godopu.com/lab/landing_page/backend/internal/models"
|
|
)
|
|
|
|
type PublicationsRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewPublicationsRepository(db *sql.DB) *PublicationsRepository {
|
|
return &PublicationsRepository{db: db}
|
|
}
|
|
|
|
func (r *PublicationsRepository) GetPublications(ctx context.Context, category string) ([]models.Publication, error) {
|
|
query := `
|
|
SELECT id, category, title, authors, venue, volume, published_at, kci, doi, is_highlight, created_at
|
|
FROM publications
|
|
`
|
|
var args []any
|
|
if category != "" {
|
|
query += " WHERE category = ?"
|
|
args = append(args, category)
|
|
}
|
|
query += " ORDER BY published_at DESC, id ASC"
|
|
|
|
rows, err := r.db.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query publications: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var pubs []models.Publication
|
|
for rows.Next() {
|
|
var p models.Publication
|
|
if err := rows.Scan(
|
|
&p.ID, &p.Category, &p.Title, &p.Authors,
|
|
&p.Venue, &p.Volume, &p.PublishedAt, &p.KCI, &p.DOI, &p.IsHighlight, &p.CreatedAt,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("scan publication: %w", err)
|
|
}
|
|
pubs = append(pubs, p)
|
|
}
|
|
return pubs, rows.Err()
|
|
}
|
|
|
|
func (r *PublicationsRepository) GetHighlights(ctx context.Context) ([]models.Publication, error) {
|
|
query := `
|
|
SELECT id, category, title, authors, venue, volume, published_at, kci, doi, is_highlight, created_at
|
|
FROM publications
|
|
WHERE is_highlight = 1
|
|
ORDER BY published_at DESC, id ASC
|
|
`
|
|
rows, err := r.db.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query highlights: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var pubs []models.Publication
|
|
for rows.Next() {
|
|
var p models.Publication
|
|
if err := rows.Scan(
|
|
&p.ID, &p.Category, &p.Title, &p.Authors,
|
|
&p.Venue, &p.Volume, &p.PublishedAt, &p.KCI, &p.DOI, &p.IsHighlight, &p.CreatedAt,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("scan highlight: %w", err)
|
|
}
|
|
pubs = append(pubs, p)
|
|
}
|
|
return pubs, rows.Err()
|
|
}
|
|
|
|
func (r *PublicationsRepository) GetPatents(ctx context.Context) ([]models.Patent, error) {
|
|
query := `
|
|
SELECT id, title, inventors, application_no, application_at, registration_no, registration_at, country, published_at, created_at
|
|
FROM patents
|
|
ORDER BY published_at DESC, id ASC
|
|
`
|
|
rows, err := r.db.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query patents: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var patents []models.Patent
|
|
for rows.Next() {
|
|
var p models.Patent
|
|
if err := rows.Scan(
|
|
&p.ID, &p.Title, &p.Inventors, &p.ApplicationNo,
|
|
&p.ApplicationAt, &p.RegistrationNo, &p.RegistrationAt, &p.Country, &p.PublishedAt, &p.CreatedAt,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("scan patent: %w", err)
|
|
}
|
|
patents = append(patents, p)
|
|
}
|
|
return patents, rows.Err()
|
|
}
|