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 OnGetAsync(string? id) { int idx = int.Parse(id); if (id == null) { return NotFound(); } InterviewTime = Context.Sessions.Find(idx); if (InterviewTime == null) { return NotFound(); } return Page(); } public async Task OnPostAsync(string? id) { int idx = int.Parse(id); if (id == null) { return NotFound(); } InterviewTime = await Context.Sessions.FindAsync(idx); 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 = 0; stu.Timelist.RemoveAll(it => it == idx); } Context.SaveChanges(); } } foreach(var x in Context.Students) { x.Timelist.RemoveAll(it => it == idx); } Context.Sessions.Remove(InterviewTime); Context.SaveChanges(); } return RedirectToPage("./Index"); } } }