From 3fd3bf346bcdd19cc65e78e89567b40f9d528de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=82=9C=E7=BF=94?= <2307953404@qq.com> Date: Fri, 11 Oct 2024 19:56:53 +0800 Subject: [PATCH] 3 --- asg_form/Controllers/InviteReferee.cs | 54 +++++++++++++++++++++------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/asg_form/Controllers/InviteReferee.cs b/asg_form/Controllers/InviteReferee.cs index be14bdd..a0e5660 100644 --- a/asg_form/Controllers/InviteReferee.cs +++ b/asg_form/Controllers/InviteReferee.cs @@ -4,7 +4,9 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using System; using System.Security.Claims; +using System.Text.Json; namespace asg_form.Controllers { @@ -20,11 +22,16 @@ namespace asg_form.Controllers 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 List<(int invitor_id, int match_id)>? my_request { get; set; } = new List<(int, int)>(); + public string? my_request { get; set; } } @@ -34,6 +41,8 @@ namespace asg_form.Controllers public int invitedId { get; set; } public int matchId { get; set; } } + + [Route("api/v1/Invite")] [HttpPost] [Authorize] @@ -45,44 +54,65 @@ 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) { - if (invitationRecord.my_request.Any(req => req.invitor_id == userId && req.match_id == msg.matchId)) + 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 = "邀请已存在" }); } - invitationRecord.my_request.Add((invitor_id: (int)userId, match_id: msg.matchId)); - await sb.SaveChangesAsync(); + 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 = new List<(int, int)>() + my_request = updatedJson }; - invitationRecord.my_request.Add((invitor_id: (int)userId, match_id: msg.matchId)); sb.T_Invitation.Add(invitationRecord); - await sb.SaveChangesAsync(); + } - + 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 = "邀请成功", data }); + } catch (Exception ex) { - return Ok(new { code = 500, message = "服务器错误", details = ex.Message }); + return Ok(new { code = 500, message = "服务器错误", details = ex.Message ,wrong_part}); } }