JPHD-2021-backend/Controllers/SubmissionController.cs

126 lines
4.5 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.Guid);
}
}
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.Guid;
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 + 1).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_GUID"));
}
List<SessionDto> timelist;
try
{
timelist = JsonSerializer.Deserialize<List<SessionDto>>(selection);
}
catch (Exception ex)
{
Console.WriteLine(ex);
return StatusCode(400, ApiResponse.Error("MALFORMED_DATA"));
};
//get the selected session guids;
List<Guid> targetSessions;
var sessions = Context.Sessions.ToList();
targetSessions = sessions.FindAll((Session s) =>
{
return timelist.Any(it => it.Day == s.Day && it.BeginTime == s.BeginTime);
}).Select(it => it.Guid).ToList();
stu.Timelist = targetSessions;
stu.Status = status.;
await Context.SaveChangesAsync();
return Ok(ApiResponse.Success("success"));
//return Ok(ApiResponse.Success("报名结束了"));
}
}
}