添加主界面
This commit is contained in:
129
Service/Implement/PostService.cs
Normal file
129
Service/Implement/PostService.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@
|
||||
using Model.Entity;
|
||||
using ORM;
|
||||
using Service.Implement;
|
||||
using Service.Interface;
|
||||
|
||||
public class UserService: BaseService<UserEntity>
|
||||
public class UserService: BaseService<UserEntity>, IUserService
|
||||
{
|
||||
public UserService(SqlSugarRepository<UserEntity> repository) : base(repository)
|
||||
{
|
||||
|
||||
16
Service/Interface/IPostService.cs
Normal file
16
Service/Interface/IPostService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
11
Service/Interface/IUserService.cs
Normal file
11
Service/Interface/IUserService.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
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