129 lines
4.3 KiB
C#
129 lines
4.3 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|
||
} |