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-10-11 21:28:37 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
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-10-11 21:28:37 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2024-08-20 17:20:58 +08:00
|
|
|
|
|
|
|
|
|
namespace asg_form.Controllers
|
2024-08-17 15:38:44 +08:00
|
|
|
|
{
|
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-11 21:28:37 +08:00
|
|
|
|
public async Task<ActionResult<object>> Toinvite([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-17 15:38:44 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-11 21:28:37 +08:00
|
|
|
|
|
|
|
|
|
public class idToString
|
|
|
|
|
{
|
|
|
|
|
public string invitedName { get; set; }
|
|
|
|
|
public string matchName { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("api/v1/myInvitation")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public async Task<ActionResult<object>> GetMyInvitor([FromQuery]short page = 1, short limit = 6)
|
|
|
|
|
{
|
|
|
|
|
string Forid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
|
|
|
var user = await userManager.FindByIdAsync(Forid);
|
|
|
|
|
long userId = user.Id;
|
|
|
|
|
|
|
|
|
|
using (TestDbContext sub = new TestDbContext())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var query = sub.T_Invitation.FirstOrDefault(c => c.user_id == userId);
|
|
|
|
|
if (query != null)
|
|
|
|
|
{
|
|
|
|
|
List<idRequest> existingPairs = JsonSerializer.Deserialize<List<idRequest>>(json: query.my_request);
|
|
|
|
|
|
|
|
|
|
var TotalRecords = existingPairs.Count;
|
|
|
|
|
int skip = (page - 1) * limit;
|
|
|
|
|
|
|
|
|
|
List<idToString> Invintors = new List<idToString>();
|
|
|
|
|
|
|
|
|
|
var queryForMatchName = sub.team_Games;
|
|
|
|
|
idToString dataAdd=new idToString();
|
|
|
|
|
|
|
|
|
|
foreach (var pair in existingPairs)
|
|
|
|
|
{
|
|
|
|
|
var userInvited = await userManager.Users.FirstOrDefaultAsync(x => x.Id ==pair.invited_id);
|
|
|
|
|
dataAdd.invitedName = userInvited.chinaname;
|
|
|
|
|
var matchFind = queryForMatchName.FirstOrDefault(c => c.id == pair.match_id);
|
|
|
|
|
dataAdd.matchName = matchFind.team1_name + "VS" + matchFind.team2_name + " " + matchFind.opentime.ToString();
|
|
|
|
|
Invintors.Add(dataAdd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invintors
|
|
|
|
|
.Skip(skip)
|
|
|
|
|
.Take(limit)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var result = new
|
|
|
|
|
{
|
|
|
|
|
rows = Invintors,
|
|
|
|
|
total = TotalRecords,
|
|
|
|
|
};
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
code = 200,
|
|
|
|
|
message = "成功获取邀请",
|
|
|
|
|
result
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return Ok(new { code = 200, message = "没有收到邀请" });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new { code = 500, message = "服务器错误", details = ex.Message});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("api/v1/toInvite")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public async Task<ActionResult<object>> ToInvite()
|
|
|
|
|
{
|
|
|
|
|
string Myid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
|
|
|
var user = await userManager.FindByIdAsync(Myid);
|
|
|
|
|
long userId = user.Id;
|
|
|
|
|
return Ok(new { code = 200, message = "没有完成的功能" });
|
|
|
|
|
}
|
2024-08-17 15:38:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|