Files
MyHomepage/backend/Services/CategoryService.cs
T
g82tt 68be41e7a2 初始提交:浏览器首页 MyHomePage 全栈项目
# 项目概述
个人浏览器首页导航应用,支持书签分类管理、搜索引擎快捷搜索、
必应每日壁纸轮播、前后端分离部署,适配 1Panel 服务器(Docker 模式)。

# 技术栈
- 前端:Vue 3 + TypeScript + Vite + Pinia + Capacitor(Android 打包)
- 后端:.NET 8 + SqlSugar(多数据库) + SQLite/MySQL + Swashbuckle
- 部署:1Panel 应用商店自定义应用(Docker Compose 模式)

# 项目结构
- backend/    .NET 8 API 后端(8 个 Controller + 15 个 Service)
- frontend/   Vue 3 前端(19 个组件 + 9 个 API 模块 + 5 个 Store)
- docker/     Docker 部署文件(后端镜像 + Nginx 反代)
- docs/       部署手册(1Panel 实战版)
- scripts/    E2E 测试脚本

# 已实现功能
- 书签管理:增删改查 + 树形分类 + 拖拽排序 + 主色自适应
- 搜索引擎:8 个内置引擎 + 自定义引擎 + favicon 自动抓取
- 必应壁纸:每日轮播 + 多分辨率自动选择 + 1.6MP 质量优先
- 全局设置:主题/行为/数据/工具 4 分类 + 跨设备同步
- 文件上传:图标/书签/通用(容器持久化 + 跨域 URL 拼接)
- 同步:基于变更日志的设备间数据同步
- 跨域部署:前后端分离 + runtime config.json 无需重新编译

# 进度记录
- 已完成 P0~P52 共 53 个开发节点(详细见 说明文档.md)
- 当前版本:v1.0 部署就绪

# 部署文档
- README.md:项目说明 + 快速开始
- 说明文档.md:完整开发进度(中文)
- docs/DEPLOY.md:1Panel 部署手册(Docker 模式)
2026-07-05 05:09:56 +08:00

112 lines
3.5 KiB
C#

using MyHomePage.Api.Common;
using MyHomePage.Api.Models.Dtos;
using MyHomePage.Api.Models.Entities;
using SqlSugar;
namespace MyHomePage.Api.Services;
/// <inheritdoc />
public class CategoryService : ICategoryService
{
private readonly ISqlSugarClient _db;
private readonly SyncLogHelper _sync;
public CategoryService(ISqlSugarClient db, SyncLogHelper sync)
{
_db = db;
_sync = sync;
}
/// <inheritdoc />
public async Task<List<CategoryDto>> GetTreeAsync()
{
var all = await _db.Queryable<Category>()
.OrderBy(c => c.Sort)
.OrderBy(c => c.Id)
.ToListAsync();
return CategoryDto.BuildTree(all);
}
/// <inheritdoc />
public async Task<CategoryDto?> GetByIdAsync(int id)
{
var entity = await _db.Queryable<Category>().InSingleAsync(id);
return entity is null ? null : ToDto(entity);
}
/// <inheritdoc />
public async Task<CategoryDto> CreateAsync(CategoryUpsertRequest request)
{
Validate(request);
var now = DateTime.UtcNow;
var entity = new Category
{
ParentId = request.ParentId,
Name = request.Name.Trim(),
Icon = request.Icon,
Sort = request.Sort,
CreatedAt = now,
UpdatedAt = now
};
entity.Id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
await _sync.WriteAsync("category", entity.Id, "create");
return ToDto(entity);
}
/// <inheritdoc />
public async Task<CategoryDto> UpdateAsync(int id, CategoryUpsertRequest request)
{
Validate(request);
var entity = await _db.Queryable<Category>().InSingleAsync(id)
?? throw new BusinessException("分类不存在", 404);
entity.ParentId = request.ParentId;
entity.Name = request.Name.Trim();
entity.Icon = request.Icon;
entity.Sort = request.Sort;
entity.UpdatedAt = DateTime.UtcNow;
await _db.Updateable(entity).ExecuteCommandAsync();
await _sync.WriteAsync("category", id, "update");
return ToDto(entity);
}
/// <inheritdoc />
public async Task DeleteAsync(int id)
{
var entity = await _db.Queryable<Category>().InSingleAsync(id)
?? throw new BusinessException("分类不存在", 404);
// 如果是父分类,先检查是否有子分类 / 链接
if (entity.ParentId == 0)
{
var hasChildren = await _db.Queryable<Category>().AnyAsync(c => c.ParentId == id);
if (hasChildren) throw new BusinessException("请先删除子分类", 400);
}
var hasBookmarks = await _db.Queryable<Bookmark>().AnyAsync(b => b.CategoryId == id && !b.IsDeleted);
if (hasBookmarks) throw new BusinessException("该分类下仍有链接,请先删除链接", 400);
await _db.Deleteable<Category>(id).ExecuteCommandAsync();
await _sync.WriteAsync("category", id, "delete");
}
/// <summary>校验入参</summary>
private static void Validate(CategoryUpsertRequest req)
{
if (string.IsNullOrWhiteSpace(req.Name)) throw new BusinessException("分类名称不能为空", 400);
if (req.Name.Length > 64) throw new BusinessException("分类名称不能超过 64 字符", 400);
}
private static CategoryDto ToDto(Category c) => new()
{
Id = c.Id,
ParentId = c.ParentId,
Name = c.Name,
Icon = c.Icon,
Sort = c.Sort,
CreatedAt = c.CreatedAt,
UpdatedAt = c.UpdatedAt
};
}