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 CurGuid { get; set; }
[BindProperty]
public string ChiefSearch { get; set; }
public List<SelectListItem> ChiefList { get; set; }
public List<SelectListItem> Stulist { get; set; }
[BindProperty]
public Guid 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)
{
Guid guid = Guid.Parse(id);
if (id == null)
{
return NotFound();
}
CurGuid = id;
CurSession = Context.Sessions.Find(guid);
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.Guid.ToString(), Text = e.Name, Selected = false, Disabled = false });
ChiefList = qlst.ToList().Distinct().ToList();
var slst = from e in Context.Students where e.InterviewTime == Guid.Empty select new SelectListItem { Value = e.Guid.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(Guid.Parse(CurGuid));
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(Guid.Parse(CurGuid));
var guid = Guid.Parse(id);
if (!string.IsNullOrEmpty(id))
{
if (Context.Students.Any(it => it.Guid == guid))
{
Context.Students.Find(guid).InterviewTime = Guid.Empty;
CurSession.Students.RemoveAll(it => it == guid);
}
Context.SaveChanges();
}
return Construct(CurGuid);
}
public async Task<IActionResult> OnPostAddAsync()
{
CurSession = Context.Sessions.Find(Guid.Parse(CurGuid));
var guid = Guid.Parse(addedStu);
if (!string.IsNullOrEmpty(addedStu))
{
if (Context.Students.Any(it => it.Guid == guid))
{
var stu = Context.Students.Find(guid);
if(stu.InterviewTime != Guid.Empty)
{
var oldtime = Context.Sessions.Find(stu.InterviewTime);
if(oldtime != null)
{
oldtime.Students.RemoveAll(s => s == stu.Guid);
Context.SaveChanges();
}
}
stu.InterviewTime = CurSession.Guid;
stu.Status = status.;
CurSession.Students.Add(guid);
}
Context.SaveChanges();
}
return Construct(CurGuid);
}
public async Task<IActionResult> OnPostAddChiefAsync()
{
CurSession = Context.Sessions.Find(Guid.Parse(CurGuid));
if (Context.Users.Any(it => it.Guid == addedChief))
{
CurSession.Chiefs.Add(addedChief);
}
Context.SaveChanges();
return Construct(CurGuid);
}
public async Task<IActionResult> OnPostDeleteChiefAsync(string id)
{
var guid = Guid.Parse(id);
CurSession = Context.Sessions.Find(Guid.Parse(CurGuid));
if (CurSession.Chiefs.Contains(guid))
{
CurSession.Chiefs.Remove(guid);
}
Context.SaveChanges();
return Construct(CurGuid);
}
private bool InterviewTimeExists(Guid id)
{
return Context.Sessions.Any(e => e.Guid == id);
}
}
}