Implements SaveCategory, SelectCategory, SelectCategoriesOfSite, SelectCategoryBySlugAndSite, DeleteCategory, SelectCategoriesOfPost, SelectPostsOfCategory, CountPostsOfCategory, and SetPostCategories on the DB Provider, along with BeginTx/QueriesWithTx for transaction support. Also fixes pre-existing compilation errors in provider_test.go (missing PagingParams args) so new tests can compile and run. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"lmika.dev/lmika/weiro/models"
|
|
"lmika.dev/lmika/weiro/providers/db/gen/sqlgen"
|
|
)
|
|
|
|
func (db *Provider) SelectCategoriesOfSite(ctx context.Context, siteID int64) ([]*models.Category, error) {
|
|
rows, err := db.queries.SelectCategoriesOfSite(ctx, siteID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cats := make([]*models.Category, len(rows))
|
|
for i, row := range rows {
|
|
cats[i] = dbCategoryToCategory(row)
|
|
}
|
|
return cats, nil
|
|
}
|
|
|
|
func (db *Provider) SelectCategory(ctx context.Context, id int64) (*models.Category, error) {
|
|
row, err := db.queries.SelectCategory(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dbCategoryToCategory(row), nil
|
|
}
|
|
|
|
func (db *Provider) SelectCategoryBySlugAndSite(ctx context.Context, siteID int64, slug string) (*models.Category, error) {
|
|
row, err := db.queries.SelectCategoryBySlugAndSite(ctx, sqlgen.SelectCategoryBySlugAndSiteParams{
|
|
SiteID: siteID,
|
|
Slug: slug,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dbCategoryToCategory(row), nil
|
|
}
|
|
|
|
func (db *Provider) SaveCategory(ctx context.Context, cat *models.Category) error {
|
|
if cat.ID == 0 {
|
|
newID, err := db.queries.InsertCategory(ctx, sqlgen.InsertCategoryParams{
|
|
SiteID: cat.SiteID,
|
|
Guid: cat.GUID,
|
|
Name: cat.Name,
|
|
Slug: cat.Slug,
|
|
Description: cat.Description,
|
|
CreatedAt: timeToInt(cat.CreatedAt),
|
|
UpdatedAt: timeToInt(cat.UpdatedAt),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cat.ID = newID
|
|
return nil
|
|
}
|
|
|
|
return db.queries.UpdateCategory(ctx, sqlgen.UpdateCategoryParams{
|
|
ID: cat.ID,
|
|
Name: cat.Name,
|
|
Slug: cat.Slug,
|
|
Description: cat.Description,
|
|
UpdatedAt: timeToInt(cat.UpdatedAt),
|
|
})
|
|
}
|
|
|
|
func (db *Provider) DeleteCategory(ctx context.Context, id int64) error {
|
|
return db.queries.DeleteCategory(ctx, id)
|
|
}
|
|
|
|
func (db *Provider) SelectCategoriesOfPost(ctx context.Context, postID int64) ([]*models.Category, error) {
|
|
rows, err := db.queries.SelectCategoriesOfPost(ctx, postID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cats := make([]*models.Category, len(rows))
|
|
for i, row := range rows {
|
|
cats[i] = dbCategoryToCategory(row)
|
|
}
|
|
return cats, nil
|
|
}
|
|
|
|
func (db *Provider) SelectPostsOfCategory(ctx context.Context, categoryID int64, pp PagingParams) ([]*models.Post, error) {
|
|
rows, err := db.queries.SelectPostsOfCategory(ctx, sqlgen.SelectPostsOfCategoryParams{
|
|
CategoryID: categoryID,
|
|
Limit: pp.Limit,
|
|
Offset: pp.Offset,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
posts := make([]*models.Post, len(rows))
|
|
for i, row := range rows {
|
|
posts[i] = dbPostToPost(row)
|
|
}
|
|
return posts, nil
|
|
}
|
|
|
|
func (db *Provider) CountPostsOfCategory(ctx context.Context, categoryID int64) (int64, error) {
|
|
return db.queries.CountPostsOfCategory(ctx, categoryID)
|
|
}
|
|
|
|
// SetPostCategories replaces all category associations for a post.
|
|
func (db *Provider) SetPostCategories(ctx context.Context, postID int64, categoryIDs []int64) error {
|
|
if err := db.queries.DeletePostCategoriesByPost(ctx, postID); err != nil {
|
|
return err
|
|
}
|
|
for _, catID := range categoryIDs {
|
|
if err := db.queries.InsertPostCategory(ctx, sqlgen.InsertPostCategoryParams{
|
|
PostID: postID,
|
|
CategoryID: catID,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func dbCategoryToCategory(row sqlgen.Category) *models.Category {
|
|
return &models.Category{
|
|
ID: row.ID,
|
|
SiteID: row.SiteID,
|
|
GUID: row.Guid,
|
|
Name: row.Name,
|
|
Slug: row.Slug,
|
|
Description: row.Description,
|
|
CreatedAt: time.Unix(row.CreatedAt, 0).UTC(),
|
|
UpdatedAt: time.Unix(row.UpdatedAt, 0).UTC(),
|
|
}
|
|
}
|