using Manganese.Array; using Masuit.Tools; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.EntityFrameworkCore; using System; using System.Security.Claims; using System.Text.Json; using System.Threading.Tasks; namespace asg_form.Controllers { public class InviteReferee : ControllerBase { private readonly RoleManager roleManager; private readonly UserManager userManager; public InviteReferee( RoleManager roleManager, UserManager userManager) { this.roleManager = roleManager; this.userManager = userManager; } public class idRequest { public int invited_id { get; set; } public int match_id { get; set; } } public class InviteBg { public int id { get; set; } public int user_id { get; set; } public string? my_request { get; set; } } public class inv { public int invitedId { get; set; } public int matchId { get; set; } } [Route("api/v1/Invite")] [HttpPost] [Authorize] public async Task> Toinvite([FromBody] inv msg) { string Invitorid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Invitorid); long userId = user.Id; using (TestDbContext sb = new TestDbContext()) { int wrong_part = -1; try { var userInvited = await userManager.Users.FirstOrDefaultAsync(x => x.Id == msg.invitedId); if (userInvited == null) return Ok(new { code = 404, message = "用户未找到" }); var invitationRecord = sb.T_Invitation.FirstOrDefault(c => c.user_id == msg.invitedId); wrong_part = 0; string updatedJson; if (invitationRecord != null) { List existingPairs = JsonSerializer.Deserialize>(json: invitationRecord.my_request); wrong_part = 1; //if (!string.IsNullOrEmpty(invitationRecord.my_request)) //{ // existingPairs = JsonSerializer.Deserialize>(invitationRecord.my_request) ?? new List(); //} if (existingPairs.Any(req => req.invited_id == userId && req.match_id == msg.matchId)) { return Ok(new { code = 409, message = "邀请已存在" }); } wrong_part=2; existingPairs.Add(new idRequest { invited_id=(int)userId,match_id=msg.matchId }); updatedJson = JsonSerializer.Serialize(existingPairs); invitationRecord.my_request = updatedJson; //sb.T_Invitation.Update(invitationRecord); wrong_part =3; } else { wrong_part = 4; updatedJson = "["+JsonSerializer.Serialize(new idRequest { invited_id = (int)userId, match_id = msg.matchId })+"]"; wrong_part = 5; invitationRecord = new InviteBg { user_id = msg.invitedId, my_request = updatedJson }; sb.T_Invitation.Add(invitationRecord); } wrong_part = 6; await sb.SaveChangesAsync(); var data = new { invitedId = (int)userId, matchId = msg.matchId, jsonId = updatedJson }; return Ok(new { code = 200, message = "邀请成功", data }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex.Message ,wrong_part}); } } } public class idToString { public string invitedName { get; set; } public string matchTeam1 { get; set; } public string matchTeam2 { get; set; } public string openTime { get; set; } } [Route("api/v1/myInvitation")] [HttpGet] [Authorize] public async Task> GetMyInvitor([FromQuery]short page = 1, short limit = 6) { string Forid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Forid); long userId = user.Id; int wrongPart = -1; using (TestDbContext sub = new TestDbContext()) { try { wrongPart = 0; var query = sub.T_Invitation.FirstOrDefault(c => c.user_id == userId); if (query != null) { wrongPart = 1; List existingPairs = JsonSerializer.Deserialize>(json: query.my_request); var TotalRecords = 0; int skip = (page - 1) * limit; List Invintors = new List(); wrongPart = 2; idToString dataAdd=new idToString(); wrongPart = 3; foreach (var pair in existingPairs) { var userInvited = await userManager.Users.FirstOrDefaultAsync(x => x.Id ==pair.invited_id); wrongPart = 5; dataAdd.invitedName = userInvited.chinaname; wrongPart = 6; var matchFind = sub.team_Games.FirstOrDefault(c => c.id == pair.match_id); wrongPart = 7; if (matchFind == null || matchFind.team1_name == null || matchFind.team2_name == null) continue; dataAdd.matchTeam1 = matchFind.team1_name; dataAdd.matchTeam2 = matchFind.team2_name; wrongPart = 8; dataAdd.openTime = matchFind.opentime.ToString(); Invintors.Add(dataAdd); TotalRecords++; } wrongPart = 4; Invintors .Skip(skip) .Take(limit) .ToList(); var result = new { rows = Invintors, total = TotalRecords, }; return Ok(new { code = 200, message = "成功获取邀请列表", result }); } return Ok(new { code = 200, message = "没有收到过邀请" }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex.Message,wrongPart}); } } } [Route("api/v1/toInvite")] [HttpGet] [Authorize] public async Task> ToInvite() { string Myid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Myid); long userId = user.Id; return Ok(new { code = 200, message = "没有完成的功能" }); } } }