110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
type HeroSectionRepository struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
func NewHeroSectionRepository(db *sql.DB) *HeroSectionRepository {
|
|
return &HeroSectionRepository{DB: db}
|
|
}
|
|
|
|
func (r *HeroSectionRepository) Get(ctx context.Context) (map[string]interface{}, error) {
|
|
query := `
|
|
SELECT hc.id, hc.primary_title, hc.secondary_title, hc.description,
|
|
hc.primary_cta, hc.secondary_cta_text,
|
|
hci.image_path, hci.image_url
|
|
FROM hero_content hc
|
|
LEFT JOIN hero_content_image hci ON hc.id = hci.hero_content_id
|
|
LIMIT 1
|
|
`
|
|
|
|
row := r.DB.QueryRowContext(ctx, query)
|
|
|
|
var result map[string]interface{} = make(map[string]interface{})
|
|
var id int
|
|
var primaryTitle, secondaryTitle, description, primaryCTA, secondaryCTA string
|
|
var imagePath, imageUrl sql.NullString
|
|
|
|
err := row.Scan(&id, &primaryTitle, &secondaryTitle, &description,
|
|
&primaryCTA, &secondaryCTA, &imagePath, &imageUrl)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result["id"] = id
|
|
result["primary_title"] = primaryTitle
|
|
result["secondary_title"] = secondaryTitle
|
|
result["description"] = description
|
|
result["primary_cta"] = primaryCTA
|
|
result["secondary_cta_text"] = secondaryCTA
|
|
result["image_path"] = imagePath.String
|
|
result["image_url"] = imageUrl.String
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (r *HeroSectionRepository) Create(ctx context.Context, data map[string]interface{}) (int, error) {
|
|
query := `
|
|
INSERT INTO hero_content
|
|
(primary_title, secondary_title, description, primary_cta, secondary_cta_text)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
`
|
|
|
|
result, err := r.DB.ExecContext(
|
|
ctx,
|
|
query,
|
|
data["primary_title"],
|
|
data["secondary_title"],
|
|
data["description"],
|
|
data["primary_cta"],
|
|
data["secondary_cta_text"],
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return int(id), nil
|
|
}
|
|
|
|
func (r *HeroSectionRepository) Update(ctx context.Context, id int, data map[string]interface{}) error {
|
|
query := `
|
|
UPDATE hero_content SET
|
|
primary_title = ?,
|
|
secondary_title = ?,
|
|
description = ?,
|
|
primary_cta = ?,
|
|
secondary_cta_text = ?
|
|
WHERE id = ?
|
|
`
|
|
|
|
_, err := r.DB.ExecContext(
|
|
ctx,
|
|
query,
|
|
data["primary_title"],
|
|
data["secondary_title"],
|
|
data["description"],
|
|
data["primary_cta"],
|
|
data["secondary_cta_text"],
|
|
id,
|
|
)
|
|
|
|
return err
|
|
}
|
|
|
|
func (r *HeroSectionRepository) Delete(ctx context.Context, id int) error {
|
|
query := `DELETE FROM hero_content WHERE id = ?`
|
|
|
|
_, err := r.DB.ExecContext(ctx, query, id)
|
|
return err
|
|
} |