140 lines
5.0 KiB
C#
140 lines
5.0 KiB
C#
using _2021_backend.Data;
|
|
using _2021_backend.Models;
|
|
using _2021_backend.Utils;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace _2021_backend.Controllers
|
|
{
|
|
[Route("api/submit")]
|
|
public class SubmissionController : Controller
|
|
{
|
|
public BackendContext Context;
|
|
|
|
public SubmissionController(BackendContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
[HttpPost]
|
|
public async Task<IActionResult> Post([FromForm] string dto)
|
|
{
|
|
var aDto = JsonSerializer.Deserialize<SubmissionDto>(dto);
|
|
string ip = Request.Headers["X-Real-IP"].FirstOrDefault();
|
|
if (aDto.Iscomplete() == false)
|
|
return StatusCode(400, ApiResponse.Error("TICKET_INFO_INCOMPLETE"));
|
|
if (aDto.Check() == false)
|
|
return StatusCode(400, ApiResponse.Error("TICKET_NOT_LEGEAL"));
|
|
Submission sub = new Submission(aDto, ip);
|
|
foreach (var tm in Context.Sessions)
|
|
{
|
|
if(aDto.Timelist != null)if (aDto.Timelist.Any(it => it.Day.Day == tm.Day.Day && it.BeginTime.TimeOfDay == tm.BeginTime.TimeOfDay))
|
|
{
|
|
sub.Timelist.Add(tm.Id);
|
|
}
|
|
}
|
|
var q = Context.Students.Where(stu => (stu.Name == aDto.Name || stu.Stuid == aDto.Stuid));
|
|
Student stu;
|
|
if (q.Count() == 0)
|
|
{
|
|
stu = Student.create(sub);
|
|
stu.Status = status.刚报名;
|
|
Context.Students.Add(stu);
|
|
}
|
|
else
|
|
{
|
|
stu = q.FirstOrDefault();
|
|
stu.Update(sub);
|
|
}
|
|
sub.Host = stu.Id;
|
|
Context.Submissions.Add(sub);
|
|
Context.SaveChanges();
|
|
await TencentSMS.Send(Context, SMSType.Signed, stu, _2021_backend.Models.User.Bot.stuID);
|
|
return Ok(ApiResponse.Success("success"));
|
|
|
|
}
|
|
}
|
|
[ApiController]
|
|
[Route("api/sessionlist")]
|
|
public class SessionlistContoller : Controller
|
|
{
|
|
private readonly BackendContext _context;
|
|
public SessionlistContoller(BackendContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
[HttpGet]
|
|
|
|
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).Select(r => new SessionDto
|
|
{
|
|
BeginTime = r.BeginTime,
|
|
Day = r.Day,
|
|
});
|
|
return Ok(ApiResponse.Success(query.ToList().FindAll(it => it.Day.Add(it.BeginTime.TimeOfDay).CompareTo(DateTime.Now) > 0).Distinct(new SessionDtoComparer()).ToList()));
|
|
//return Ok(ApiResponse.Success("报名结束了"));
|
|
}
|
|
}
|
|
|
|
[ApiController]
|
|
[Route("api/postsession")]
|
|
public class PostsessionController : Controller
|
|
{
|
|
private readonly BackendContext Context;
|
|
public PostsessionController(BackendContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
[HttpPost]
|
|
|
|
public async Task<IActionResult> Post([FromQuery] string stuid, [FromQuery] string selection)
|
|
{
|
|
Student stu = Context.Students.FirstOrDefault(r => r.Stuid == stuid);
|
|
if (stu == null)
|
|
{
|
|
return StatusCode(400, ApiResponse.Error("INVALID_STUDENT_int"));
|
|
}
|
|
SessionDto time;
|
|
try
|
|
{
|
|
time = JsonSerializer.Deserialize<SessionDto>(selection);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
return StatusCode(400, ApiResponse.Error("MALFORMED_DATA"));
|
|
};
|
|
//get the selected session ints;
|
|
int targetSession = 0;
|
|
var sessions = Context.Sessions.ToList();
|
|
var s = sessions.Find((Session s) =>
|
|
{
|
|
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.Id);
|
|
Context.SaveChanges();
|
|
overwritten = true;
|
|
}
|
|
targetSession = s.Id;
|
|
s.Students.Add(stu.Id);
|
|
stu.Timelist = new List<int> { targetSession};
|
|
stu.Status = status.已选时间;
|
|
stu.InterviewTime = s.Id;
|
|
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("报名结束了"));
|
|
|
|
}
|
|
}
|
|
} |