优化
This commit is contained in:
+13
-3
@@ -55,6 +55,7 @@ func (r *mediaRepo) CreateMedia(ctx context.Context, v *biz.MediaFile) error {
|
||||
}
|
||||
v.ID = po.ID
|
||||
v.CreatedAt = po.CreatedAt
|
||||
v.UpdatedAt = po.UpdatedAt
|
||||
return nil
|
||||
}
|
||||
func (r *mediaRepo) FindMedia(ctx context.Context, id uint) (*biz.MediaFile, error) {
|
||||
@@ -119,7 +120,13 @@ func (r *mediaRepo) ListMedia(ctx context.Context, filter biz.MediaFilter) ([]*b
|
||||
return out, total, nil
|
||||
}
|
||||
func (r *mediaRepo) UpdateMediaName(ctx context.Context, id uint, name string) error {
|
||||
return r.data.gormDB.WithContext(ctx).Model(&mediaPO{}).Where("id = ?", id).Update("name", name).Error
|
||||
// The compatible flow performs a First before Update, so a missing record returns a
|
||||
// not-found error instead of silently succeeding with RowsAffected=0.
|
||||
var po mediaPO
|
||||
if err := r.data.gormDB.WithContext(ctx).Where("id = ?", id).First(&po).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return r.data.gormDB.WithContext(ctx).Model(&po).Update("name", name).Error
|
||||
}
|
||||
func (r *mediaRepo) DeleteMedia(ctx context.Context, id uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Unscoped().Delete(&mediaPO{}, id).Error
|
||||
@@ -176,14 +183,17 @@ func (r *mediaRepo) ListCategories(ctx context.Context) ([]*biz.AttachmentCatego
|
||||
}
|
||||
byID := map[uint]*biz.AttachmentCategory{}
|
||||
for _, po := range pos {
|
||||
byID[po.ID] = &biz.AttachmentCategory{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, ParentID: po.ParentID, Children: []*biz.AttachmentCategory{}}
|
||||
// Children remains nil for leaf nodes, which serializes as null.
|
||||
byID[po.ID] = &biz.AttachmentCategory{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, ParentID: po.ParentID}
|
||||
}
|
||||
roots := []*biz.AttachmentCategory{}
|
||||
for _, po := range pos {
|
||||
v := byID[po.ID]
|
||||
if parent := byID[po.ParentID]; parent != nil {
|
||||
parent.Children = append(parent.Children, v)
|
||||
} else {
|
||||
} else if po.ParentID == 0 {
|
||||
// The tree starts at parent id 0; non-zero orphan nodes are
|
||||
// intentionally omitted instead of being promoted to roots.
|
||||
roots = append(roots, v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user