新增单词管理后台,分类有单词时禁止删除,支持 Docker 一键部署
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -2,11 +2,15 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/fish/language/backend/internal/models"
|
||||
"github.com/fish/language/backend/internal/repository/queries"
|
||||
)
|
||||
|
||||
// ErrCategoryNotEmpty is returned when deleting a category that still has words.
|
||||
var ErrCategoryNotEmpty = errors.New("category has words")
|
||||
|
||||
// CategoryService handles business logic for categories.
|
||||
type CategoryService struct {
|
||||
q *queries.Queries
|
||||
@@ -64,8 +68,15 @@ func (s *CategoryService) Update(ctx context.Context, id int32, req models.Updat
|
||||
return toCategory(row), nil
|
||||
}
|
||||
|
||||
// Delete removes a category by id.
|
||||
// Delete removes a category by id. It refuses to delete a category that still has words.
|
||||
func (s *CategoryService) Delete(ctx context.Context, id int32) error {
|
||||
count, err := s.q.CountWords(ctx, queries.CountWordsParams{CategoryID: id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return ErrCategoryNotEmpty
|
||||
}
|
||||
return s.q.DeleteCategory(ctx, id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user