框架优化
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using ORM;
|
||||
using Service.Implement;
|
||||
using Service.Interface;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Service.Implement
|
||||
{
|
||||
public class PostService : BaseService<PostEntity>, IPostService
|
||||
{
|
||||
public PostService(SqlSugarRepository<PostEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Result<List<PostEntity>>> GetRecommendedPostsAsync(int count = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
var posts = await _repository.Entities
|
||||
.Where(x => x.IsDel == 0) // 使用正确的字段名
|
||||
.OrderBy(x => x.IsTop, OrderByType.Desc)
|
||||
.OrderBy(x => x.PublishTime, OrderByType.Desc)
|
||||
.Take(count)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<PostEntity>>.Success(posts);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<PostEntity>>.Error("获取推荐帖子失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<List<PostEntity>>> SearchPostsAsync(string keyword)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
return Result<List<PostEntity>>.Success(new List<PostEntity>());
|
||||
}
|
||||
|
||||
var posts = await _repository.Entities
|
||||
.Where(x => (x.Title.Contains(keyword) || x.Content.Contains(keyword))
|
||||
&& x.IsDel == 0) // 使用正确的字段名
|
||||
.OrderBy(x => x.IsTop, OrderByType.Desc)
|
||||
.OrderBy(x => x.PublishTime, OrderByType.Desc)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<PostEntity>>.Success(posts);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<PostEntity>>.Error("搜索帖子失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<List<PostEntity>>> GetPostsByCategoryAsync(string category, int count = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(category))
|
||||
{
|
||||
return Result<List<PostEntity>>.Success(new List<PostEntity>());
|
||||
}
|
||||
|
||||
var posts = await _repository.Entities
|
||||
.Where(x => x.Category == category && x.IsDel == 0) // 使用正确的字段名
|
||||
.OrderBy(x => x.IsTop, OrderByType.Desc)
|
||||
.OrderBy(x => x.PublishTime, OrderByType.Desc)
|
||||
.Take(count)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<PostEntity>>.Success(posts);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<PostEntity>>.Error("获取分类帖子失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<bool>> CreatePostAsync(PostEntity post)
|
||||
{
|
||||
try
|
||||
{
|
||||
post.PublishTime = DateTime.Now;
|
||||
var result = await _repository.InsertAsync(post);
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("创建帖子失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<bool>> UpdatePostAsync(PostEntity post)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _repository.UpdateAsync(post);
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("更新帖子失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<bool>> DeletePostAsync(long postId)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 软删除:将IsDel设为1
|
||||
var post = await _repository.Entities.FirstAsync(x => x.Id == postId);
|
||||
if (post == null)
|
||||
return Result<bool>.Error("帖子不存在");
|
||||
|
||||
post.IsDel = 1;
|
||||
var result = await _repository.UpdateAsync(post);
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("删除帖子失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using ORM;
|
||||
using Service.Implement;
|
||||
using Service.Interface;
|
||||
|
||||
public class UserService: BaseService<UserEntity>, IUserService
|
||||
{
|
||||
public UserService(SqlSugarRepository<UserEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Result<UserEntity>> GetUserByUserNameAsync(string username)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
return Result<UserEntity>.Error("用户名不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
var user = await _repository.Entities
|
||||
.Where(x => x.UserName == username)
|
||||
.FirstAsync();
|
||||
|
||||
if (user == null)
|
||||
return Result<UserEntity>.Error("用户不存在");
|
||||
|
||||
return Result<UserEntity>.Success(user);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<UserEntity>.Error("获取用户失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<Result<bool>> InsertAsync(UserEntity entity)
|
||||
{
|
||||
if (entity == null)
|
||||
return Result<bool>.Error("用户数据不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
// 唯一性校验
|
||||
bool exists = await _repository.Entities
|
||||
.AnyAsync(x => x.UserName == entity.UserName);
|
||||
|
||||
if (exists)
|
||||
return Result<bool>.Error("用户名已存在");
|
||||
|
||||
var result = await _repository.InsertAsync(entity);
|
||||
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("插入数据失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新单条用户记录
|
||||
/// </summary>
|
||||
/// <param name="entity">要更新的用户实体,Id 必须有值</param>
|
||||
public async Task<Result<bool>> UpdateAsync(UserEntity entity)
|
||||
{
|
||||
if (entity == null)
|
||||
return Result<bool>.Error("用户数据不能为空");
|
||||
|
||||
if (entity.Id <= 0)
|
||||
return Result<bool>.Error("主键 Id 无效,无法更新");
|
||||
|
||||
try
|
||||
{
|
||||
// 唯一性校验(可选,如果允许改用户名就检查)
|
||||
bool exists = await _repository.Entities
|
||||
.Where(x => x.UserName == entity.UserName && x.Id != entity.Id)
|
||||
.AnyAsync();
|
||||
|
||||
if (exists)
|
||||
return Result<bool>.Error("用户名已存在");
|
||||
|
||||
// 更新整个实体
|
||||
var result = await _repository.UpdateAsync(entity);
|
||||
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("更新用户失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using Service.Interface;
|
||||
|
||||
namespace Service.Interface
|
||||
{
|
||||
public interface IPostService : IBaseService<PostEntity>
|
||||
{
|
||||
Task<Result<List<PostEntity>>> GetRecommendedPostsAsync(int count = 10);
|
||||
Task<Result<List<PostEntity>>> SearchPostsAsync(string keyword);
|
||||
Task<Result<List<PostEntity>>> GetPostsByCategoryAsync(string category, int count = 10);
|
||||
Task<Result<bool>> CreatePostAsync(PostEntity post);
|
||||
Task<Result<bool>> UpdatePostAsync(PostEntity post);
|
||||
Task<Result<bool>> DeletePostAsync(long postId);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
|
||||
namespace Service.Interface
|
||||
{
|
||||
public interface IUserService : IBaseService<UserEntity>
|
||||
{
|
||||
public Task<Result<UserEntity>> GetUserByUserNameAsync(string username);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user