JPHD-2021-backend/Pages/Sessions/Delete.cshtml.cs

78 lines
2.2 KiB
C#

using _2021_backend.Data;
using _2021_backend.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _2021_backend.Pages.Sessions
{
public class DeleteModel : PageModel
{
private readonly _2021_backend.Data.BackendContext Context;
public DeleteModel(_2021_backend.Data.BackendContext context)
{
Context = context;
}
[BindProperty]
public Session InterviewTime { get; set; }
public async Task<IActionResult> OnGetAsync(string? id)
{
Guid guid = Guid.Parse(id);
if (id == null)
{
return NotFound();
}
InterviewTime = Context.Sessions.Find(guid);
if (InterviewTime == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(string? id)
{
Guid guid = Guid.Parse(id);
if (id == null)
{
return NotFound();
}
InterviewTime = await Context.Sessions.FindAsync(guid);
if (InterviewTime != null)
{
if (InterviewTime.Students.Count > 0)
{
foreach (var x in InterviewTime.Students)
{
Student stu = Context.Students.Find(x);
if (stu != null)
{
stu.InterviewTime = Guid.Empty;
stu.Timelist.RemoveAll(it => it == guid);
}
Context.SaveChanges();
}
}
foreach(var x in Context.Students)
{
x.Timelist.RemoveAll(it => it == guid);
}
Context.Sessions.Remove(InterviewTime);
Context.SaveChanges();
}
return RedirectToPage("./Index");
}
}
}