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

189 lines
6.1 KiB
C#

using _2021_backend.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _2021_backend.Pages.Sessions
{
public class EditModel : PageModel
{
public _2021_backend.Data.BackendContext Context;
public EditModel(_2021_backend.Data.BackendContext context)
{
Context = context;
}
[BindProperty]
public Session CurSession { get; set; }
[BindProperty]
public string Curint { get; set; }
[BindProperty]
public string ChiefSearch { get; set; }
public List<SelectListItem> ChiefList { get; set; }
public List<SelectListItem> Stulist { get; set; }
[BindProperty]
public int addedChief { get; set; }
[BindProperty]
public string addedStu { get; set; }
[BindProperty]
public string ChiefName { get; set; }
[BindProperty]
[Display(Name ="活动地点")]
[RegularExpression(@"^20[04]$", ErrorMessage = "200204")]
[Required()]
public string newPlace { get; set; }
[BindProperty]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[Required()]
public DateTime newDay { get; set; }
[BindProperty]
[DataType(DataType.Time)]
[DisplayFormat(DataFormatString = "{0:t}", ApplyFormatInEditMode = true)]
[Required()]
public DateTime newTime { get; set; }
[BindProperty]
[Required()]
public bool sendSMS { get; set; }
[BindProperty]
public int newCapacity { get; set; }
public IActionResult Construct(string id)
{
int idx = int.Parse(id);
if (id == null)
{
return NotFound();
}
Curint = id;
CurSession = Context.Sessions.Find(idx);
newDay = CurSession.Day;
newTime = CurSession.BeginTime;
newCapacity = CurSession.Capacity;
sendSMS = CurSession.SendSMS;
newPlace = CurSession.Place;
if (CurSession == null) return RedirectToPage("./Index", new { errmsg = "未找到该活动场次" });
var qlst = from e in Context.Users select (new SelectListItem { Value = e.Id.ToString(), Text = e.Name, Selected = false, Disabled = false });
ChiefList = qlst.ToList().Distinct().ToList();
var slst = from e in Context.Students where e.InterviewTime == 0 select new SelectListItem { Value = e.Id.ToString(), Text = e.Name, Selected = false, Disabled = false };
Stulist = slst.ToList().Distinct().ToList();
return Page();
}
public async Task<IActionResult> OnGetAsync(string id)
{
return Construct(id);
}
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
var ssn = Context.Sessions.Find(int.Parse(Curint));
ssn.SendSMS = sendSMS;
ssn.Day = newDay;
ssn.BeginTime = newTime;
ssn.Capacity = newCapacity;
ssn.Place = newPlace;
Context.SaveChanges();
return RedirectToPage("./Index");
}
public async Task<IActionResult> OnPostDeleteAsync(string id)
{
CurSession = Context.Sessions.Find(int.Parse(Curint));
var idx = int.Parse(id);
if (!string.IsNullOrEmpty(id))
{
if (Context.Students.Any(it => it.Id == idx))
{
Context.Students.Find(idx).InterviewTime = 0;
CurSession.Students.RemoveAll(it => it == idx);
}
Context.SaveChanges();
}
return Construct(Curint);
}
public async Task<IActionResult> OnPostAddAsync()
{
CurSession = Context.Sessions.Find(int.Parse(Curint));
var idx = int.Parse(addedStu);
if (!string.IsNullOrEmpty(addedStu))
{
if (Context.Students.Any(it => it.Id == idx))
{
var stu = Context.Students.Find(idx);
if(stu.InterviewTime != 0)
{
var oldtime = Context.Sessions.Find(stu.InterviewTime);
if(oldtime != null)
{
oldtime.Students.RemoveAll(s => s == stu.Id);
Context.SaveChanges();
}
}
stu.InterviewTime = CurSession.Id;
stu.Status = status.;
CurSession.Students.Add(idx);
}
Context.SaveChanges();
}
return Construct(Curint);
}
public async Task<IActionResult> OnPostAddChiefAsync()
{
CurSession = Context.Sessions.Find(int.Parse(Curint));
if (Context.Users.Any(it => it.Id == addedChief))
{
CurSession.Chiefs.Add(addedChief);
}
Context.SaveChanges();
return Construct(Curint);
}
public async Task<IActionResult> OnPostDeleteChiefAsync(string id)
{
var idx = int.Parse(id);
CurSession = Context.Sessions.Find(int.Parse(Curint));
if (CurSession.Chiefs.Contains(idx))
{
CurSession.Chiefs.Remove(idx);
}
Context.SaveChanges();
return Construct(Curint);
}
private bool InterviewTimeExists(int id)
{
return Context.Sessions.Any(e => e.Id == id);
}
}
}