This commit is contained in:
杨毅 2024-11-24 00:18:51 +08:00
parent 9832eaebdb
commit a942f3ad7d

View File

@ -25,12 +25,154 @@ using static
using Manganese.Array;
using Microsoft.AspNetCore.Identity;
using Flurl.Http;
using System.Security.Claims;
namespace asg_form.Controllers
{
public class form_cs : ControllerBase
{
[Route("api/v3/form/all")]
[HttpGet]
public async Task<ActionResult<object>> GetFormById(short page, short limit, int sort, int eventId)
{
using (var db = new TestDbContext())
{
try
{
var query = db.Forms.Where(f => f.events.Id == eventId);
query = sort switch
{
0 => query.OrderByDescending(m => m.piaoshu).ThenByDescending(m => m.time),
1 => query.OrderBy(m => m.piaoshu),
2 => query.OrderByDescending(m => m.piaoshu),
3 => query.OrderBy(m => m.time),
4 => query.OrderByDescending(m => m.time),
_ => query
};
if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin") || this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
try
{
var results = await query
.Skip((page - 1) * limit)
.Take(limit)
.Select(f => new AdminTeamFind
{
form = f,
roles = findRolesByFormId(f.Id, 0)
})
.ToListAsync();
int total = await query.CountAsync();
return Ok(new { code = 200, message = "成功", data = results, total });
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误", error = ex.Message });
}
}
else
{
var results = await query
.Skip((page - 1) * limit)
.Take(limit)
.Select(f => new AdminTeamFind
{
form = new form
{
Id = f.Id,
team_name = f.team_name,
piaoshu = f.piaoshu,
},
roles = findRolesByFormId(f.Id, 1)
})
.ToListAsync();
int total = await query.CountAsync();
return Ok(new { code = 200, message = "成功", data = results, total });
}
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误", ex = ex.Message });
}
}
}
public class tmpRole
{
public string? roleName { get; set; }
public int? roleRank { get; set; }
public string? commonRoles { get; set; }
public string? roleLin { get; set; }
public string? roleId { get; set; }
public string? gameName { get; set; }
public string? idCard { get; set; }
public string? phoneNumber { get; set; }
public string? idCardName { get; set; }
}
public class TeamFind
{
public long formId { get; set; }
public string teamName { get; set; }
public int voteOfLikes { get; set; }
public List<tmpRole> roles { get; set; } = new List<tmpRole>();
}
public class AdminTeamFind
{
public form? form { get; set; }
public List<tmpRole>? roles { get; set; } = new List<tmpRole>();
}
private static List<tmpRole> findRolesByFormId(long formId, int y)
{
var roles = new List<tmpRole>();
var db = new TestDbContext();
try
{
var query = db.Roles.Where(x => x.form.Id == formId);
if (y == 1)
{
roles = query
.Select(f => new tmpRole
{
roleName = f.role_name,
roleRank = f.Historical_Ranks,
commonRoles = f.Common_Roles,
roleLin = f.role_lin
})
.ToList();
}
else
{
roles = query
.Select(f => new tmpRole
{
roleName = f.role_name,
roleRank = f.Historical_Ranks,
commonRoles = f.Common_Roles,
roleLin = f.role_lin,
roleId = f.role_id,
gameName = f.Game_Name,
idCard = f.Id_Card,
phoneNumber = f.Phone_Number,
idCardName = f.Id_Card_Name
})
.ToList();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error in BuildTree: {ex.Message}");
throw;
}
return roles;
}
}
[ApiController]
[Route("[controller]")]
@ -281,7 +423,7 @@ namespace asg_form.Controllers
if (imageFile == null || imageFile.Length == 0)
return BadRequest("Invalid image file.");
// ½«Îļþ±£´æµ½´ÅÅÌ
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"loge/{for1.events_name}/{for1.events_name}/", $"{imageFile.FileName}");
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"loge/{for1.events_name}/", $"{imageFile.FileName}");
using (var stream = new FileStream(filePath, FileMode.Create))
{
await imageFile.CopyToAsync(stream);