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

81 lines
2.8 KiB
C#

using _2021_backend.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace _2021_backend.Pages.Sessions
{
public class CreateModel : PageModel
{
private readonly _2021_backend.Data.BackendContext Context;
[Display(Name = "同时创建200和204场次")]
[BindProperty]
public bool CreateTwo { get; set; }
[BindProperty]
public Session InterviewTime { get; set; }
[BindProperty]
[Display(Name = "活动官")]
public string ChiefGuid { get; set; }
public List<SelectListItem> ChiefList { get; set; }
public CreateModel(_2021_backend.Data.BackendContext context)
{
Context = context;
}
public async Task<IActionResult> OnGet()
{
var qlst = from e in Context.Users select (new SelectListItem { Value = e.Guid.ToString(), Text = e.Name, Selected = false, Disabled = false });
var lst = qlst.ToList().Distinct().ToList();
ChiefList = lst;
return Page();
}
// 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()
{
if (!ModelState.IsValid)
{
return Page();
}
InterviewTime.Students = new List<Guid>();
InterviewTime.Chiefs = new List<Guid>();
Session interviewTimeSecond = new Session();
if (CreateTwo)
{
string anotherPlace = InterviewTime.Place == "200" ? "204" : "200";
Session tmp = Context.Sessions.FirstOrDefault(r => r.Place == anotherPlace && r.Day == InterviewTime.Day && InterviewTime.BeginTime == r.BeginTime);
if (tmp == null)
{
interviewTimeSecond = new Session()
{
Day = InterviewTime.Day,
BeginTime = InterviewTime.BeginTime,
Place = anotherPlace,
Chiefs = new List<Guid>(),
SendSMS = InterviewTime.SendSMS,
Students = new List<Guid>(),
Capacity = InterviewTime.Capacity
};
Context.Add(interviewTimeSecond);
Context.SaveChanges();
}
}
Context.Sessions.Add(InterviewTime);
Context.SaveChanges();
return RedirectToPage("./Index");
}
}
}