66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"lmika.dev/lmika/hugo-cms/gen/sqlc/dbq"
|
|
"lmika.dev/lmika/hugo-cms/models"
|
|
"lmika.dev/pkg/modash/momap"
|
|
"lmika.dev/pkg/modash/moslice"
|
|
)
|
|
|
|
func (db *DB) InsertBundle(ctx context.Context, bundle *models.Bundle) error {
|
|
id, err := db.q.InsertBundle(ctx, dbq.InsertBundleParams{
|
|
SiteID: bundle.SiteID,
|
|
Name: bundle.Name,
|
|
CreatedAt: pgtype.Timestamp{Time: bundle.CreatedAt, Valid: true},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
bundle.ID = id
|
|
return nil
|
|
}
|
|
|
|
func (db *DB) ListBundles(ctx context.Context, siteID int64) ([]models.Bundle, error) {
|
|
res, err := db.q.ListBundles(ctx, siteID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return moslice.Map(res, dbBundleToBundle), nil
|
|
}
|
|
|
|
func (db *DB) GetBundleWithID(ctx context.Context, id int64) (models.Bundle, error) {
|
|
res, err := db.q.GetBundleWithID(ctx, id)
|
|
if err != nil {
|
|
return models.Bundle{}, err
|
|
}
|
|
|
|
return dbBundleToBundle(res), nil
|
|
}
|
|
|
|
func (db *DB) GetSiteBundleInfo(ctx context.Context, siteID int64) (map[int64]models.BundleInfo, error) {
|
|
res, err := db.q.GetSiteBundleInfo(ctx, siteID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return momap.FromSlice(res, func(bi dbq.GetSiteBundleInfoRow) (int64, models.BundleInfo) {
|
|
return bi.BundleID, models.BundleInfo{
|
|
BundleID: bi.BundleID,
|
|
PageCount: int(bi.PageCount),
|
|
IndexPageID: bi.IndexPageID.Int64,
|
|
}
|
|
}), nil
|
|
}
|
|
|
|
func dbBundleToBundle(b dbq.Bundle) models.Bundle {
|
|
return models.Bundle{
|
|
ID: b.ID,
|
|
SiteID: b.SiteID,
|
|
Name: b.Name,
|
|
CreatedAt: b.CreatedAt.Time,
|
|
UpdatedAt: b.UpdatedAt.Time,
|
|
}
|
|
}
|