feat(help-center): align category payloads
This commit is contained in:
@@ -22,7 +22,18 @@ func (r *CategoryRepo) Create(ctx context.Context, category *model.Category) err
|
||||
|
||||
func (r *CategoryRepo) GetByID(ctx context.Context, id uint) (*model.Category, error) {
|
||||
var category model.Category
|
||||
if err := r.db.WithContext(ctx).Preload("Parent").Preload("RelatedCategories").Preload("Folders").First(&category, id).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Folders").Preload("Articles").First(&category, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &category, nil
|
||||
}
|
||||
|
||||
func (r *CategoryRepo) GetByPortalAndID(ctx context.Context, portalID, id uint) (*model.Category, error) {
|
||||
var category model.Category
|
||||
if err := r.db.WithContext(ctx).
|
||||
Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Folders").Preload("Articles").
|
||||
Where("portal_id = ?", portalID).
|
||||
First(&category, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &category, nil
|
||||
@@ -42,7 +53,11 @@ func (r *CategoryRepo) FindByPortalID(ctx context.Context, portalID uint, offset
|
||||
var count int64
|
||||
db := r.db.WithContext(ctx).Model(&model.Category{}).Where("portal_id = ?", portalID)
|
||||
db.Count(&count)
|
||||
if err := db.Offset(offset).Limit(limit).Order("position ASC").Find(&categories).Error; err != nil {
|
||||
query := db.Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Articles").Order("position ASC")
|
||||
if limit > 0 {
|
||||
query = query.Offset(offset).Limit(limit)
|
||||
}
|
||||
if err := query.Find(&categories).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return categories, count, nil
|
||||
@@ -54,7 +69,11 @@ func (r *CategoryRepo) FindByPortalIDAndLocale(ctx context.Context, portalID uin
|
||||
var count int64
|
||||
db := r.db.WithContext(ctx).Model(&model.Category{}).Where("portal_id = ? AND locale = ?", portalID, locale)
|
||||
db.Count(&count)
|
||||
if err := db.Offset(offset).Limit(limit).Order("position ASC").Find(&categories).Error; err != nil {
|
||||
query := db.Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Articles").Order("position ASC")
|
||||
if limit > 0 {
|
||||
query = query.Offset(offset).Limit(limit)
|
||||
}
|
||||
if err := query.Find(&categories).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return categories, count, nil
|
||||
@@ -83,6 +102,15 @@ func (r *CategoryRepo) UpdatePositions(ctx context.Context, positions map[uint]i
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CategoryRepo) UpdatePositionsForPortal(ctx context.Context, portalID uint, positions map[uint]int) error {
|
||||
for id, pos := range positions {
|
||||
if err := r.db.WithContext(ctx).Model(&model.Category{}).Where("id = ? AND portal_id = ?", id, portalID).Update("position", pos).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindBySlugAndPortalIDAndLocale returns a category by slug, portal, and locale.
|
||||
func (r *CategoryRepo) FindBySlugAndPortalIDAndLocale(ctx context.Context, slug string, portalID uint, locale string) (*model.Category, error) {
|
||||
var category model.Category
|
||||
@@ -90,4 +118,4 @@ func (r *CategoryRepo) FindBySlugAndPortalIDAndLocale(ctx context.Context, slug
|
||||
return nil, err
|
||||
}
|
||||
return &category, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func newTestCategory(portalID uint, name string) *model.Category {
|
||||
// ========== Create ==========
|
||||
|
||||
func TestCategoryRepo_Create(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
cat := newTestCategory(1, "TestCategory")
|
||||
@@ -36,7 +36,7 @@ func TestCategoryRepo_Create(t *testing.T) {
|
||||
// ========== GetByID ==========
|
||||
|
||||
func TestCategoryRepo_GetByID(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
cat := newTestCategory(1, "FindByIDCat")
|
||||
@@ -49,7 +49,7 @@ func TestCategoryRepo_GetByID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCategoryRepo_GetByID_NotFound(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
found, err := repo.GetByID(context.Background(), 9999)
|
||||
@@ -60,7 +60,7 @@ func TestCategoryRepo_GetByID_NotFound(t *testing.T) {
|
||||
// ========== Update ==========
|
||||
|
||||
func TestCategoryRepo_Update(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
cat := newTestCategory(1, "BeforeUpdate")
|
||||
@@ -81,7 +81,7 @@ func TestCategoryRepo_Update(t *testing.T) {
|
||||
// ========== Delete ==========
|
||||
|
||||
func TestCategoryRepo_Delete(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
cat := newTestCategory(1, "DeleteCat")
|
||||
@@ -99,7 +99,7 @@ func TestCategoryRepo_Delete(t *testing.T) {
|
||||
// ========== FindByPortalID ==========
|
||||
|
||||
func TestCategoryRepo_FindByPortalID(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
portalID := uint(10)
|
||||
@@ -119,7 +119,7 @@ func TestCategoryRepo_FindByPortalID(t *testing.T) {
|
||||
// ========== FindByPortalIDAndParentID ==========
|
||||
|
||||
func TestCategoryRepo_FindByPortalIDAndParentID(t *testing.T) {
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{})
|
||||
db := setupTestDB(t, &model.Category{}, &model.Folder{}, &model.RelatedCategory{}, &model.Portal{}, &model.Account{}, &model.Article{})
|
||||
repo := NewCategoryRepo(db)
|
||||
|
||||
portalID := uint(10)
|
||||
@@ -145,4 +145,4 @@ func TestCategoryRepo_FindByPortalIDAndParentID(t *testing.T) {
|
||||
for _, c := range categories {
|
||||
assert.Equal(t, parentID, *c.ParentID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user