asg_backend/asg_form/Controllers/InviteReferee.cs

92 lines
3.3 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;
using System.Security.Claims;
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-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; }
public List<(int invitor_id, int match_id)>? my_request { get; set; } = new List<(int, int)>();
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-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-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-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-10 16:08:18 +08:00
if (invitationRecord != null)
{
if (invitationRecord.my_request.Any(req => req.invitor_id == userId && req.match_id == msg.matchId))
{
return Ok(new { code = 409, message = "邀请已存在" });
}
2024-10-10 22:42:58 +08:00
invitationRecord.my_request.Add((invitor_id: (int)userId, match_id: msg.matchId));
await sb.SaveChangesAsync();
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-10 22:42:58 +08:00
invitationRecord = new InviteBg
{
user_id = msg.invitedId,
my_request = new List<(int, int)>()
};
invitationRecord.my_request.Add((invitor_id: (int)userId, match_id: msg.matchId));
sb.T_Invitation.Add(invitationRecord);
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,
};
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
{
return Ok(new { code = 500, message = "服务器错误", details = ex.Message });
}
2024-10-10 21:54:39 +08:00
2024-08-20 17:32:06 +08:00
}
}
}
}