diff --git a/asg_form/Controllers/InviteReferee.cs b/asg_form/Controllers/InviteReferee.cs index eb91ba5..eefb4a2 100644 --- a/asg_form/Controllers/InviteReferee.cs +++ b/asg_form/Controllers/InviteReferee.cs @@ -1,4 +1,5 @@ -using Manganese.Array; +using AngleSharp.Text; +using Manganese.Array; using Masuit.Tools; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; @@ -9,6 +10,7 @@ using System; using System.Security.Claims; using System.Text.Json; using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace asg_form.Controllers { @@ -33,9 +35,19 @@ namespace asg_form.Controllers { public int id { get; set; } public int user_id { get; set; } - public string? my_request { get; set; } + public int invited_id { get; set; } + public int match_id { get; set; } + public int status { get; set; } + public string? create_time { get; set; } + } + public class InviteForUsers + { + public string invitedChinaname { get; set; } + public schedule.team_game match { get; set; } + + public string? create_time { get; set; } } public class inv @@ -56,77 +68,42 @@ namespace asg_form.Controllers 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) + var invited = await userManager.Users.FirstOrDefaultAsync(u => u.Id == msg.invitedId); + if (invited != null) return Ok(new { code = 404, message = "用户未找到" }); + var isInvited = await sb.T_Invitation.AnyAsync(i => i.user_id == userId && i.invited_id==msg.invitedId && i.match_id==msg.matchId); + if (!isInvited) { - 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)) + string createTime = DateTime.Now.ToString(); + var invitation = new InviteBg { - 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 + user_id = (int)userId, + invited_id = msg.invitedId, + match_id = msg.matchId, + status = 0, + create_time = createTime, }; - sb.T_Invitation.Add(invitationRecord); - + sb.Add(invitation); + sb.SaveChanges(); + var match = sb.team_Games.FirstOrDefault(n => n.id == msg.matchId); + var data = new + { + userName = user.chinaname, + invitedName = invited.chinaname, + match + }; + return Ok(new { code = 200, message = "成功发送邀请" ,data}); } - wrong_part = 6; - await sb.SaveChangesAsync(); - var data = new - { - invitedId = (int)userId, - matchId = msg.matchId, - jsonId = updatedJson - }; - return Ok(new { code = 200, message = "邀请成功", data }); - } + return Ok(new { code = 200, message = "已经在本场次邀请过这个人" }); + } catch (Exception ex) { - return Ok(new { code = 500, message = "服务器错误", details = ex.Message ,wrong_part}); + return Ok(new { code = 500, message = "服务器错误", details = ex }); } - } } - 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] @@ -143,74 +120,133 @@ namespace asg_form.Controllers { try { - wrongPart = 0; - var query = sub.T_Invitation.FirstOrDefault(c => c.user_id == userId); - if (query != null) + var query = sub.T_Invitation + .AsQueryable(); + var TotalRecords = await query.CountAsync(); + var invitationRecordId = query + .Where(x => x.user_id == userId) + .OrderByDescending(t => t.status) + .ThenBy(t => t.create_time) + .Skip((page - 1) * limit) + .Take(limit) + .ToList(); + + List invitationRecord = new List(); + foreach (var record in invitationRecordId) { - 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 invited = await userManager.Users.FirstOrDefaultAsync(u => u.Id == record.invited_id); + var addData = new InviteForUsers { - 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, + invitedChinaname = invited.chinaname, + match = sub.team_Games.FirstOrDefault(i => i.id == record.match_id), + create_time = record.create_time, }; - return Ok(new - { - code = 200, - message = "成功获取邀请列表", - result - }); - } - return Ok(new { code = 200, message = "没有收到过邀请" }); + } + var data = new + { + rows = invitationRecord, + total = TotalRecords, + }; + return Ok(new { code = 200, message = "邀请记录如下", data }); } catch (Exception ex) { - return Ok(new { code = 500, message = "服务器错误", details = ex.Message,wrongPart}); + return Ok(new { code = 500, message = "服务器错误", details = ex }); } } } + public class BeInviteForUsers + { + public string invitorChinaname { get; set; } + public schedule.team_game match { get; set; } + + public string? create_time { get; set; } + } [Route("api/v1/toInvite")] [HttpGet] [Authorize] - public async Task> ToInvite() + public async Task> ToInvite([FromQuery]short page = 1, short limit = 6) { string Myid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Myid); long userId = user.Id; - return Ok(new { code = 200, message = "没有完成的功能" }); + + int wrongPart = -1; + using (TestDbContext sub = new TestDbContext()) + { + try + { + var query = sub.T_Invitation + .AsQueryable(); + var TotalRecords = await query.CountAsync(); + var invitationRecordId = query + .Where(x => x.invited_id == userId && x.status==0) + .OrderBy(t => t.create_time) + .Skip((page - 1) * limit) + .Take(limit) + .ToList(); + List invitationRecord = new List(); + foreach (var record in invitationRecordId) + { + var invitor = await userManager.Users.FirstOrDefaultAsync(u => u.Id == record.user_id); + var addData = new BeInviteForUsers + { + invitorChinaname = invitor.chinaname, + match = sub.team_Games.FirstOrDefault(i => i.id == record.match_id), + create_time = record.create_time, + }; + } + var data = new + { + rows = invitationRecord, + total = TotalRecords, + }; + return Ok(new { code = 200, message = "收到的未处理的邀请如下", data }); + } + catch (Exception ex) + { + return Ok(new { code = 500, message = "服务器错误", details = ex }); + } + } + } + + public class msg2 + { + public int id { get; set; } + public int status { get; set; } + } + [Route("api/v1/recordAgreeOrDisagree")] + [HttpPost] + [Authorize] + public async Task> PassAssign([FromBody] msg2 askMsg) + { + using (TestDbContext sub = new TestDbContext()) + { + try + { + var query = sub.T_Invitation.FirstOrDefault(n => n.id == askMsg.id); + if (query != null) + { + string message = "无效操作"; + query.status = askMsg.status; + if(askMsg.status == 1) + { + message = "你同意了邀请,记得及时到位哦~"; + }else if (askMsg.status == 2) + { + message = "你拒绝了邀请"; + } + sub.SaveChanges(); + return Ok(new { code = 200, message}); + } + return Ok(new { code = 404, message = "没有对应记录" }); + } + catch (Exception ex) + { + return Ok(new { code = 500, message = "服务器错误", details = ex }); + } + } } } }