54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
namespace BlazorApp.Components.Pages.BlogPage;
|
|
|
|
public class BlogService
|
|
{
|
|
private static Lazy<BlogService> _instance = new Lazy<BlogService>(() => new BlogService());
|
|
|
|
public static BlogService Instance = _instance.Value;
|
|
|
|
private List<PostItem> posts = new List<PostItem>();
|
|
private BlogService() {
|
|
|
|
}
|
|
|
|
public void CreatePost(PostItem post)
|
|
{
|
|
if(posts.Count > 0)
|
|
post.Id = posts[^1].Id + 1;
|
|
else
|
|
post.Id = 1;
|
|
|
|
posts.Add(post);
|
|
}
|
|
|
|
public List<PostItem> ReadPosts()
|
|
{
|
|
foreach (var post in posts)
|
|
Console.WriteLine($"READ {post.Id} : {post.Title} = {post.Content}");
|
|
return this.posts;
|
|
}
|
|
|
|
public PostItem ReadPost(int id)
|
|
{
|
|
foreach (var post in posts)
|
|
{
|
|
if (post.Id == id)
|
|
return post;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void UpdatePost(PostItem post,string title, string content)
|
|
{
|
|
post.Title = title;
|
|
post.Content = content;
|
|
}
|
|
|
|
|
|
public void DeletePost(int id)
|
|
{
|
|
PostItem post = ReadPost(id);
|
|
posts.Remove(post);
|
|
}
|
|
|
|
} |