This commit is contained in:
王炜翔 2024-10-11 19:56:53 +08:00
parent f878d849f8
commit 3fd3bf346b

View File

@ -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<idRequest> existingPairs = JsonSerializer.Deserialize<List<idRequest>>(json: invitationRecord.my_request);
wrong_part = 1;
//if (!string.IsNullOrEmpty(invitationRecord.my_request))
//{
// existingPairs = JsonSerializer.Deserialize<List<idRequest>>(invitationRecord.my_request) ?? new List<idRequest>();
//}
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 });
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误", details = ex.Message });
return Ok(new { code = 500, message = "服务器错误", details = ex.Message ,wrong_part});
}
}