From 00c3b32e2db200a2f42396ac7f8381704ec97268 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Fri, 10 Feb 2023 16:19:46 +0100 Subject: Initial Commit --- CoreWiki/Pages/Delete.cshtml.cs | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 CoreWiki/Pages/Delete.cshtml.cs (limited to 'CoreWiki/Pages/Delete.cshtml.cs') diff --git a/CoreWiki/Pages/Delete.cshtml.cs b/CoreWiki/Pages/Delete.cshtml.cs new file mode 100644 index 0000000..9de563e --- /dev/null +++ b/CoreWiki/Pages/Delete.cshtml.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.EntityFrameworkCore; +using CoreWiki.Models; + +namespace CoreWiki.Pages +{ + public class DeleteModel : PageModel + { + private readonly CoreWiki.Models.ApplicationDbContext _context; + + public DeleteModel(CoreWiki.Models.ApplicationDbContext context) + { + _context = context; + } + + [BindProperty] + public Article Article { get; set; } = default!; + + public async Task OnGetAsync(string id) + { + if (id == null || _context.Articles == null) + { + return NotFound(); + } + + var article = await _context.Articles.FirstOrDefaultAsync(m => m.Topic == id); + + if (article == null) + { + return NotFound(); + } + else + { + Article = article; + } + return Page(); + } + + public async Task OnPostAsync(string id) + { + if (id == null || _context.Articles == null) + { + return NotFound(); + } + var article = await _context.Articles.FindAsync(id); + + if (article != null) + { + Article = article; + _context.Articles.Remove(Article); + await _context.SaveChangesAsync(); + } + + return Redirect("/"); + } + } +} -- cgit v1.2.3