第八章:分类管理与站点配置
博客v1.0系列教程(Csharp)博客 v1.0 系列教程 (C#)
8.1 分类管理
分类支持数据库存储和文件夹目录两种来源:
public class CategoryService : ICategoryService
{
private readonly IRepository<CategoryModel> _categoryRepo;
public async Task<List<CategoryDto>> GetCategoriesAsync()
{
var categories = await _categoryRepo.GetListAsync();
// 合并文件夹分类
var folderCategories = GetCategoriesFromFolders();
categories.AddRange(folderCategories);
return categories.DistinctBy(c => c.Name).ToList();
}
private List<CategoryModel> GetCategoriesFromFolders()
{
// 从 articles/ 子目录读取分类
var dirs = Directory.GetDirectories("articles");
return dirs.Select(d => new CategoryModel
{
Name = Path.GetFileName(d),
Source = "folder"
}).ToList();
}
}
8.2 站点配置
支持运行时编辑,JSON 持久化到数据库:
public class SiteConfigService
{
private readonly IRepository<SiteConfigModel> _configRepo;
public async Task<T?> GetConfigAsync<T>(string name) where T : class
{
var config = await _configRepo.AsQueryable()
.FirstAsync(c => c.Name == name);
return config == null
? null
: JsonSerializer.Deserialize<T>(config.ConfigJson);
}
public async Task UpdateConfigAsync<T>(string name, T config)
{
var json = JsonSerializer.Serialize(config);
var existing = await _configRepo.AsQueryable()
.FirstAsync(c => c.Name == name);
if (existing != null)
{
existing.ConfigJson = json;
await _configRepo.UpdateAsync(existing);
}
else
{
await _configRepo.InsertAsync(new SiteConfigModel
{
Name = name,
ConfigJson = json
});
}
}
}
8.3 收藏功能
public class CollectService : ICollectService
{
public async Task<List<CollectDto>> GetUserCollectsAsync(long userId)
{
var collects = await _collectRepo.AsQueryable()
.Where(c => c.UserId == userId)
.ToListAsync();
return collects.Adapt<List<CollectDto>>();
}
public async Task ToggleArticleCollectAsync(long userId, long articleId)
{
var existing = await _userArticleCollectRepo.AsQueryable()
.Where(uc => uc.UserId == userId && uc.ArticleId == articleId)
.FirstAsync();
if (existing != null)
await _userArticleCollectRepo.DeleteAsync(existing);
else
await _userArticleCollectRepo.InsertAsync(
new UserArticleCollectModel
{
UserId = userId,
ArticleId = articleId
});
}
}
下一章将实现文件上传与数据统计。
csharpcategoryconfigmanagement