asg_backend/asg_form/Controllers/InviteReferee.cs

122 lines
4.5 KiB
C#
Raw Normal View History

2024-10-10 21:08:04 +08:00
using Manganese.Array;
using Masuit.Tools;
2024-08-20 17:32:06 +08:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
2024-08-20 17:20:58 +08:00
using Microsoft.AspNetCore.Mvc;
2024-08-20 17:32:06 +08:00
using Microsoft.EntityFrameworkCore;
2024-10-11 19:56:53 +08:00
using System;
2024-08-20 17:32:06 +08:00
using System.Security.Claims;
2024-10-11 19:56:53 +08:00
using System.Text.Json;
2024-08-20 17:20:58 +08:00
namespace asg_form.Controllers
{
2024-08-20 17:20:58 +08:00
public class InviteReferee : ControllerBase
{
private readonly RoleManager<Role> roleManager;
private readonly UserManager<User> userManager;
public InviteReferee(
RoleManager<Role> roleManager, UserManager<User> userManager)
{
this.roleManager = roleManager;
this.userManager = userManager;
}
2024-10-11 19:56:53 +08:00
public class idRequest
{
public int invited_id { get; set; }
public int match_id { get; set; }
}
2024-10-10 09:59:47 +08:00
public class InviteBg
{
public int id { get; set; }
2024-10-10 16:08:18 +08:00
public int user_id { get; set; }
2024-10-11 19:56:53 +08:00
public string? my_request { get; set; }
2024-10-10 16:08:18 +08:00
2024-10-10 09:59:47 +08:00
}
2024-10-10 16:08:18 +08:00
public class inv
{
public int invitedId { get; set; }
public int matchId { get; set; }
}
2024-10-11 19:56:53 +08:00
2024-08-20 17:32:06 +08:00
[Route("api/v1/Invite")]
2024-10-10 16:08:18 +08:00
[HttpPost]
2024-08-20 17:32:06 +08:00
[Authorize]
2024-10-10 16:08:18 +08:00
public async Task<ActionResult<object>> GetReferee([FromBody] inv msg)
2024-08-20 17:32:06 +08:00
{
string Invitorid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(Invitorid);
2024-10-10 09:59:47 +08:00
long userId = user.Id;
2024-08-20 17:32:06 +08:00
using (TestDbContext sb = new TestDbContext())
{
2024-10-11 19:56:53 +08:00
int wrong_part = -1;
2024-10-10 09:59:47 +08:00
try
{
2024-10-10 16:08:18 +08:00
var userInvited = await userManager.Users.FirstOrDefaultAsync(x => x.Id == msg.invitedId);
2024-10-10 21:41:24 +08:00
if (userInvited == null) return Ok(new { code = 404, message = "用户未找到" });
2024-10-11 19:56:53 +08:00
2024-10-10 09:59:47 +08:00
2024-10-10 22:42:58 +08:00
var invitationRecord = sb.T_Invitation.FirstOrDefault(c => c.user_id == msg.invitedId);
2024-10-11 19:56:53 +08:00
wrong_part = 0;
string updatedJson;
2024-10-10 16:08:18 +08:00
if (invitationRecord != null)
{
2024-10-11 19:56:53 +08:00
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))
2024-10-10 16:08:18 +08:00
{
return Ok(new { code = 409, message = "邀请已存在" });
}
2024-10-11 19:56:53 +08:00
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;
2024-10-10 16:08:18 +08:00
}
2024-10-10 22:42:58 +08:00
else
2024-10-10 21:08:04 +08:00
{
2024-10-11 19:56:53 +08:00
wrong_part = 4;
updatedJson = "["+JsonSerializer.Serialize(new idRequest { invited_id = (int)userId, match_id = msg.matchId })+"]";
wrong_part = 5;
2024-10-10 22:42:58 +08:00
invitationRecord = new InviteBg
{
user_id = msg.invitedId,
2024-10-11 19:56:53 +08:00
my_request = updatedJson
2024-10-10 22:42:58 +08:00
};
sb.T_Invitation.Add(invitationRecord);
2024-10-11 19:56:53 +08:00
2024-10-10 22:42:58 +08:00
}
2024-10-11 19:56:53 +08:00
wrong_part = 6;
await sb.SaveChangesAsync();
2024-10-10 21:54:39 +08:00
var data = new
{
2024-10-10 21:55:13 +08:00
invitedId = (int)userId,
2024-10-10 21:54:39 +08:00
matchId = msg.matchId,
2024-10-11 19:56:53 +08:00
jsonId = updatedJson
2024-10-10 21:54:39 +08:00
};
2024-10-11 19:56:53 +08:00
return Ok(new { code = 200, message = "邀请成功", data });
}
2024-10-10 16:08:18 +08:00
catch (Exception ex)
2024-10-10 09:59:47 +08:00
{
2024-10-11 19:56:53 +08:00
return Ok(new { code = 500, message = "服务器错误", details = ex.Message ,wrong_part});
2024-10-10 09:59:47 +08:00
}
2024-10-10 21:54:39 +08:00
2024-08-20 17:32:06 +08:00
}
}
}
}