asg_backend/asg_form/Controllers/InviteReferee.cs

75 lines
2.4 KiB
C#
Raw Normal View History

2024-08-20 17:32:06 +08:00
using Masuit.Tools;
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);
if (userInvited != null) return Ok(new { code = 404, message = "用户未找到" });
2024-10-10 09:59:47 +08:00
2024-10-10 16:08:18 +08:00
var invitationRecord = await sb.T_Invitation.FindAsync(msg.invitedId);
2024-10-10 09:59:47 +08:00
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 = "邀请已存在" });
}
}
}
catch (Exception ex)
2024-10-10 09:59:47 +08:00
{
return Ok(new { code = 500, message = "服务器错误", details = ex.Message });
}
2024-10-10 16:08:18 +08:00
return Ok(new { code = 200, message = "邀请成功" });
2024-08-20 17:32:06 +08:00
}
}
}
}