Blazor/BlazorApp/Components/Pages/BlogPage/Blog.razor.cs
2024-10-12 16:04:03 +09:00

102 lines
2.4 KiB
C#

using BlazorApp.PrefixCSharp;
using Microsoft.AspNetCore.Components;
namespace BlazorApp.Components.Pages.BlogPage;
public class PostItem
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
public partial class Blog : ComponentBase
{
// BlogService BS = BlogService.Instance;
// private PostItem post = new PostItem();
private string title = string.Empty;
private string content = string.Empty;
private int id = 0;
private bool isEdit = false;
public void SavePost()
{
if (isEdit)
{
Console.WriteLine(this.id);
EditPost(this.id);
}
else
{
PostItem post = new PostItem
{
Title = this.title,
Content = this.content
};
CreatePost(post);
}
title = string.Empty;
content = string.Empty;
}
public void CreatePost(PostItem post)
{
BlogService.Instance.CreatePost(post);
BlogService.Instance.ReadPosts();
}
public void RemovePost(int id)
{
BlogService.Instance.DeletePost(id);
}
public void EditPost(int id)
{
isEdit = Prefix.Toggle(isEdit);
if (isEdit)
{
PostItem post = BlogService.Instance.ReadPost(id);
title = post.Title;
content = post.Content;
this.id = post.Id;
}
else
{
BlogService.Instance.UpdatePost(BlogService.Instance.ReadPost(id),this.title, this.content);
}
}
}
public class Singleton
{
// Lazy 선언으로 사용시에만 객체를 생성하게 하면서 외부에서 접근할 수 없는 단일 인스턴스를 저장한다.
private static Lazy<Singleton> _instance = new Lazy<Singleton>(() => new Singleton());
// 외부에서 해당 인스턴스에 접근하려면 이 속성을 통해서만 접근이 가능
public static Singleton Instance => _instance.Value;
private int value = 0;
// 싱글턴이니까 외부에서 인스턴스 생성 불가하게
private Singleton()
{
}
public int DoSomething(int value)
{
Console.WriteLine("DO SINGLETON");
this.value++;
return this.value + value;
}
public void checkValue()
{
Console.WriteLine($"CHECK VALUE:{this.value}");
}
}