diff --git a/Controllers/SubmissionController.cs b/Controllers/SubmissionController.cs index b62dfd1..5402683 100644 --- a/Controllers/SubmissionController.cs +++ b/Controllers/SubmissionController.cs @@ -70,7 +70,7 @@ namespace _2021_backend.Controllers public IActionResult GetSessions() { - var query = _context.Sessions.OrderBy(r => r.Day).ThenBy(r => r.BeginTime).ThenBy(r => r.Place).Where(e => e.Capacity > e.Students.Count + 1).Select(r => new SessionDto + var query = _context.Sessions.OrderBy(r => r.Day).ThenBy(r => r.BeginTime).ThenBy(r => r.Place).Where(e => e.Capacity > e.Students.Count).Select(r => new SessionDto { BeginTime = r.BeginTime, Day = r.Day, @@ -98,10 +98,10 @@ namespace _2021_backend.Controllers { return StatusCode(400, ApiResponse.Error("INVALID_STUDENT_GUID")); } - List timelist; + SessionDto time; try { - timelist = JsonSerializer.Deserialize>(selection); + time = JsonSerializer.Deserialize(selection); } catch (Exception ex) { @@ -109,15 +109,29 @@ namespace _2021_backend.Controllers return StatusCode(400, ApiResponse.Error("MALFORMED_DATA")); }; //get the selected session guids; - List targetSessions; + Guid targetSession = Guid.Empty; var sessions = Context.Sessions.ToList(); - targetSessions = sessions.FindAll((Session s) => + var s = sessions.Find((Session s) => { - return timelist.Any(it => it.Day == s.Day && it.BeginTime == s.BeginTime); - }).Select(it => it.Guid).ToList(); - stu.Timelist = targetSessions; + return time.Day == s.Day && time.BeginTime == s.BeginTime; + }); + var overwritten = false; + if (s.Students.Count >= s.Capacity) return Ok(ApiResponse.Error("OUT_OF_CAPACITY")); + if (Context.Sessions.Find(stu.InterviewTime) != null) + { + var olds = Context.Sessions.Find(stu.InterviewTime); + olds.Students.Remove(stu.Guid); + Context.SaveChanges(); + overwritten = true; + } + targetSession = s.Guid; + s.Students.Add(stu.Guid); + stu.Timelist = new List { targetSession}; stu.Status = status.已选时间; - await Context.SaveChangesAsync(); + stu.InterviewTime = s.Guid; + await TencentSMS.Send(Context, SMSType.TimeSet, stu, "smsbot"); + Context.SaveChanges(); + if (overwritten) return Ok(ApiResponse.Success("overwritten")); return Ok(ApiResponse.Success("success")); //return Ok(ApiResponse.Success("报名结束了"));