42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
|
|
using AsadiTools.Data;
|
||
|
|
using AsadiTools.Models;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace AsadiTools.Pages.Admin.Blog;
|
||
|
|
|
||
|
|
[Authorize(AuthenticationSchemes = "AdminCookie")]
|
||
|
|
public class AdminBlogIndexModel(AppDbContext db) : PageModel
|
||
|
|
{
|
||
|
|
public List<BlogPost> Posts { get; private set; } = [];
|
||
|
|
|
||
|
|
public async Task OnGetAsync()
|
||
|
|
{
|
||
|
|
Posts = await db.BlogPosts.OrderByDescending(p => p.CreatedAt).ToListAsync();
|
||
|
|
ViewData["Title"] = "مدیریت بلاگ";
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
||
|
|
{
|
||
|
|
var post = await db.BlogPosts.FindAsync(id);
|
||
|
|
if (post is not null) { db.BlogPosts.Remove(post); await db.SaveChangesAsync(); }
|
||
|
|
return RedirectToPage();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IActionResult> OnPostTogglePublishAsync(int id)
|
||
|
|
{
|
||
|
|
var post = await db.BlogPosts.FindAsync(id);
|
||
|
|
if (post is not null)
|
||
|
|
{
|
||
|
|
post.IsPublished = !post.IsPublished;
|
||
|
|
if (post.IsPublished && post.PublishedAt is null)
|
||
|
|
post.PublishedAt = DateTime.Now;
|
||
|
|
post.UpdatedAt = DateTime.Now;
|
||
|
|
await db.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
return RedirectToPage();
|
||
|
|
}
|
||
|
|
}
|