Mon, 08 Nov 2021 21:21:57 GMT

master
大蒟蒻 4 years ago
parent 2e49f53b11
commit 60c2e6354a

@ -0,0 +1,47 @@
using cugoj_ng_server.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace cugoj_ng_server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ApplicationFormController : ControllerBase
{
[Route("Get"), HttpGet]
public async Task<ActionResult<ApplicationForm>> GetAsync()
{
var user = HttpContext.Session.GetString("user");
if (string.IsNullOrEmpty(user))
return Unauthorized();
var form = await UserModel.GetApplicationFormAsync(user);
if (form == null) return NoContent();
return Ok(form);
}
[Route("Save"), HttpPost]
public async Task<IActionResult> SaveAsync([FromBody] ApplicationForm form)
{
var user = HttpContext.Session.GetString("user");
if (string.IsNullOrEmpty(user))
return Unauthorized();
form.UserId = user;
return Ok(await UserModel.SetApplicationFormAsync(form));
}
[Route("Submit"), HttpPost]
public async Task<IActionResult> SubmitAsync([FromBody] ApplicationForm form)
{
var user = HttpContext.Session.GetString("user");
if (string.IsNullOrEmpty(user))
return Unauthorized();
form.UserId = user;
if (!form.Validate())
return BadRequest();
return Ok(await UserModel.SetApplicationFormAsync(form));
}
}
}

@ -37,7 +37,7 @@ namespace cugoj_ng_server.Controllers
}
[HttpGet]
[Route("List/{page}")]
[Route("List/{page:int:min(1)}")]
public async Task<object> GetProblemListAsync(int page)
{
if (page <= 0) return BadRequest();
@ -52,7 +52,7 @@ namespace cugoj_ng_server.Controllers
}
[HttpGet]
[Route("{pid}")]
[Route("{pid:int}")]
public async Task<IActionResult> GetProblemAsync(int pid)
{
bool viewAll = await UserModel.Authorization.CanViewAllProblemsAsync(HttpContext.Session.GetString("user"));
@ -66,7 +66,7 @@ namespace cugoj_ng_server.Controllers
[RequestSizeLimit(1 << 20)]
[HttpPost]
[Route("Submit/{pid}")]
[Route("{pid:int}/Submit")]
public async Task<IActionResult> SubmitSolutionAsync(int pid, [FromForm] string lang, [FromForm] string code)
{
var user = HttpContext.Session.GetString("user");

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace cugoj_ng_server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StatusController : ControllerBase
{
readonly int maxStatusPerPage;
public StatusController(IConfiguration configuration)
{
maxStatusPerPage = configuration.GetValue<int>("Config:MaxStatusPerPage");
}
[HttpGet]
public object List()
{
if (Request.Query.TryGetValue("cid", out var s))
{
if (s.Any(x => !int.TryParse(x, out _)))
return BadRequest("cid must be int");
}
return 1;
}
}
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace cugoj_ng_server.Models
{
public class StatusModel
{
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using cugoj_ng_server.Utilities;
using Dapper;
@ -54,5 +55,63 @@ namespace cugoj_ng_server.Models
return (await conn.QueryAsync<string>(@"select rightstr from privilege where user_id=@id and length(rightstr)>5", new { id = user_id })).ToArray();
}
}
public static async Task<ApplicationForm> GetApplicationFormAsync(string uid)
{
using var conn = GetConnection();
return await conn.QueryFirstOrDefaultAsync<ApplicationForm>("select * from application_form where user_id=@uid", new { uid });
}
public static async Task<int> SetApplicationFormAsync(ApplicationForm form)
{
using var conn = GetConnection();
return await conn.ExecuteAsync(@"
REPLACE INTO application_form (user_id,name,student_id,college,major,mobile,qq,score,oj_accounts,text1,text2,text3,text4,status,comment)
VALUES (@UserId,@Name,@StudentId,@College,@Major,@Mobile,@QQ,@Score,@OJAccounts,@Text1,@Text2,@Text3,@Text4,@Status,@Comment)
", form);
}
}
public class ApplicationForm
{
public string UserId { get; set; }
public string Name { get; set; }
public string StudentId { get; set; }
public string College { get; set; }
public string Major { get; set; }
public string Mobile { get; set; }
public string QQ { get; set; }
public string Score { get; set; }
[Labels("Nullable")]
public string OJAccounts { get; set; }
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
[Labels("Nullable")]
public string Text4 { get; set; }
public string Status { get; set; }
[Labels("Nullable")]
public string Comment { get; set; }
static readonly PropertyInfo[] properties =
typeof(ApplicationForm).GetProperties().Where(prop => !prop.HasLabel("Nullable")).ToArray();
public bool Validate() =>
properties.All(prop =>
!string.IsNullOrWhiteSpace((string)prop.GetValue(this)));
}
}

@ -42,6 +42,7 @@ namespace cugoj_ng_server
opt.Cookie.IsEssential = true;
opt.IdleTimeout = TimeSpan.FromMinutes(sessionTimeout);
});
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
Utilities.Inject.SetPropertiesByType(typeof(Models.DbConn), null, new()
{
{ typeof(IConnectionMultiplexer), ConnectionMultiplexer.Connect(redisConnstr) },
@ -50,7 +51,7 @@ namespace cugoj_ng_server
services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.IncludeFields = true;
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace cugoj_ng_server.Utilities
{
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class LabelsAttribute : Attribute
{
private readonly HashSet<string> LabelSet = null;
public LabelsAttribute(params string[] labels)
{
LabelSet = new(labels);
}
public bool HasLabel(string label) => LabelSet.Contains(label);
}
static class LabelExtension
{
public static bool HasLabel(this PropertyInfo propertyInfo, string label) =>
propertyInfo.GetCustomAttributes<LabelsAttribute>().Any(attr => attr.HasLabel(label));
}
}

@ -9,6 +9,7 @@
"AllowedHosts": "*",
"Config": {
"SessionTimeout": 1440,
"ProblemsPerPage": 100
"ProblemsPerPage": 100,
"MaxSolutionSize": 65535
}
}

@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>cugoj_ng_server</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="5.0.1" />
<PackageReference Include="MySql.Data" Version="8.0.23" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.0" />
<PackageReference Include="MySql.Data" Version="8.0.27" />
</ItemGroup>
</Project>

Loading…
Cancel
Save