Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using DotNetGuideBlogModel.Dtos;
using DotNetGuideBlogModel.Entities;
using DotNetGuideBlogRepository.Articles;

namespace DotNetGuideBlogBLL.Articles;

public class ArticleService(IArticleRepository articleRepository) : IArticleService
{
public async Task<List<ArticleDto>> GetListAsync()
{
var articles = await articleRepository.GetListAsync();
return articles.Select(MapToDto).ToList();
}

public async Task<ArticleDto?> GetByIdAsync(int id)
{
if (id <= 0)
{
return null;
}

var article = await articleRepository.GetByIdAsync(id);
return article is null ? null : MapToDto(article);
}

public async Task<ArticleDto?> AddAsync(CreateArticleRequest request)
{
if (!IsValidTitle(request.Title))
{
return null;
}

var entity = new Article
{
Title = request.Title.Trim(),
Content = request.Content?.Trim() ?? string.Empty,
Author = request.Author?.Trim() ?? string.Empty
};

var created = await articleRepository.AddAsync(entity);
return MapToDto(created);
}

public async Task<bool> UpdateAsync(int id, UpdateArticleRequest request)
{
if (id <= 0 || !IsValidTitle(request.Title))
{
return false;
}

var existing = await articleRepository.GetByIdAsync(id);
if (existing is null)
{
return false;
}

existing.Title = request.Title.Trim();
existing.Content = request.Content?.Trim() ?? string.Empty;
existing.Author = request.Author?.Trim() ?? string.Empty;

return await articleRepository.UpdateAsync(existing);
}

public async Task<bool> DeleteAsync(int id)
{
if (id <= 0)
{
return false;
}

var existing = await articleRepository.GetByIdAsync(id);
if (existing is null)
{
return false;
}

return await articleRepository.DeleteAsync(id);
}

private static bool IsValidTitle(string? title)
{
return !string.IsNullOrWhiteSpace(title);
}

private static ArticleDto MapToDto(Article article)
{
return new ArticleDto
{
Id = article.Id,
Title = article.Title,
Content = article.Content,
Author = article.Author,
CreateTime = article.CreateTime,
UpdateTime = article.UpdateTime
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DotNetGuideBlogModel.Dtos;

namespace DotNetGuideBlogBLL.Articles;

public interface IArticleService
{
Task<List<ArticleDto>> GetListAsync();

Task<ArticleDto?> GetByIdAsync(int id);

Task<ArticleDto?> AddAsync(CreateArticleRequest request);

Task<bool> UpdateAsync(int id, UpdateArticleRequest request);

Task<bool> DeleteAsync(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetGuideBlogRepository\DotNetGuideBlogRepository.csproj" />
<ProjectReference Include="..\DotNetGuideBlogModel\DotNetGuideBlogModel.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Collections.Concurrent;
using System.Threading;
using DotNetGuideBlogModel.Entities;

namespace DotNetGuideBlogDAL.Articles;

/// <summary>
/// 使用线程安全内存集合模拟数据库访问。
/// </summary>
public class ArticleDal : IArticleDal
{
private readonly ConcurrentDictionary<int, Article> _articles = new();
private int _idSeed;

public Task<List<Article>> GetListAsync()
{
var result = _articles.Values
.Where(x => !x.IsDeleted)
.OrderByDescending(x => x.CreateTime)
.Select(Clone)
.ToList();

return Task.FromResult(result);
}

public Task<Article?> GetByIdAsync(int id)
{
if (_articles.TryGetValue(id, out var article) && !article.IsDeleted)
{
return Task.FromResult<Article?>(Clone(article));
}

return Task.FromResult<Article?>(null);
}

public Task<Article> AddAsync(Article article)
{
var now = DateTime.UtcNow;
var entity = Clone(article);
entity.Id = Interlocked.Increment(ref _idSeed);
entity.CreateTime = now;
entity.UpdateTime = null;
entity.IsDeleted = false;

_articles[entity.Id] = entity;
return Task.FromResult(Clone(entity));
}

public Task<bool> UpdateAsync(Article article)
{
if (!_articles.TryGetValue(article.Id, out var existing) || existing.IsDeleted)
{
return Task.FromResult(false);
}

var updated = Clone(existing);
updated.Title = article.Title;
updated.Content = article.Content;
updated.Author = article.Author;
updated.UpdateTime = DateTime.UtcNow;

_articles[article.Id] = updated;
return Task.FromResult(true);
}

public Task<bool> DeleteAsync(int id)
{
if (!_articles.TryGetValue(id, out var existing) || existing.IsDeleted)
{
return Task.FromResult(false);
}

var deleted = Clone(existing);
deleted.IsDeleted = true;
deleted.UpdateTime = DateTime.UtcNow;

_articles[id] = deleted;
return Task.FromResult(true);
}

private static Article Clone(Article source)
{
return new Article
{
Id = source.Id,
Title = source.Title,
Content = source.Content,
Author = source.Author,
CreateTime = source.CreateTime,
UpdateTime = source.UpdateTime,
IsDeleted = source.IsDeleted
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DotNetGuideBlogModel.Entities;

namespace DotNetGuideBlogDAL.Articles;

public interface IArticleDal
{
Task<List<Article>> GetListAsync();

Task<Article?> GetByIdAsync(int id);

Task<Article> AddAsync(Article article);

Task<bool> UpdateAsync(Article article);

Task<bool> DeleteAsync(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetGuideBlogModel\DotNetGuideBlogModel.csproj" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions Platforms/DotNetGuideBlog/DotNetGuideBlogModel/Common/ApiResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace DotNetGuideBlogModel.Common;

public class ApiResult<T>
{
public bool Success { get; set; }

public string Message { get; set; } = string.Empty;

public T? Data { get; set; }

public static ApiResult<T> Ok(T? data, string message = "操作成功")
{
return new ApiResult<T>
{
Success = true,
Message = message,
Data = data
};
}

public static ApiResult<T> Fail(string message)
{
return new ApiResult<T>
{
Success = false,
Message = message,
Data = default
};
}
}
16 changes: 16 additions & 0 deletions Platforms/DotNetGuideBlog/DotNetGuideBlogModel/Dtos/ArticleDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace DotNetGuideBlogModel.Dtos;

public class ArticleDto
{
public int Id { get; set; }

public string Title { get; set; } = string.Empty;

public string Content { get; set; } = string.Empty;

public string Author { get; set; } = string.Empty;

public DateTime CreateTime { get; set; }

public DateTime? UpdateTime { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DotNetGuideBlogModel.Dtos;

public class CreateArticleRequest
{
public string Title { get; set; } = string.Empty;

public string Content { get; set; } = string.Empty;

public string Author { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DotNetGuideBlogModel.Dtos;

public class UpdateArticleRequest
{
public string Title { get; set; } = string.Empty;

public string Content { get; set; } = string.Empty;

public string Author { get; set; } = string.Empty;
}
18 changes: 18 additions & 0 deletions Platforms/DotNetGuideBlog/DotNetGuideBlogModel/Entities/Article.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace DotNetGuideBlogModel.Entities;

public class Article
{
public int Id { get; set; }

public string Title { get; set; } = string.Empty;

public string Content { get; set; } = string.Empty;

public string Author { get; set; } = string.Empty;

public DateTime CreateTime { get; set; }

public DateTime? UpdateTime { get; set; }

public bool IsDeleted { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using DotNetGuideBlogDAL.Articles;
using DotNetGuideBlogModel.Entities;

namespace DotNetGuideBlogRepository.Articles;

public class ArticleRepository(IArticleDal articleDal) : IArticleRepository
{
public Task<Article?> GetByIdAsync(int id)
{
return articleDal.GetByIdAsync(id);
}

public Task<List<Article>> GetListAsync()
{
return articleDal.GetListAsync();
}

public Task<Article> AddAsync(Article article)
{
return articleDal.AddAsync(article);
}

public Task<bool> UpdateAsync(Article article)
{
return articleDal.UpdateAsync(article);
}

public Task<bool> DeleteAsync(int id)
{
return articleDal.DeleteAsync(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DotNetGuideBlogModel.Entities;

namespace DotNetGuideBlogRepository.Articles;

public interface IArticleRepository
{
Task<Article?> GetByIdAsync(int id);

Task<List<Article>> GetListAsync();

Task<Article> AddAsync(Article article);

Task<bool> UpdateAsync(Article article);

Task<bool> DeleteAsync(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetGuideBlogDAL\DotNetGuideBlogDAL.csproj" />
<ProjectReference Include="..\DotNetGuideBlogModel\DotNetGuideBlogModel.csproj" />
</ItemGroup>

</Project>
Loading