using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using RadicadeServer.API.Notifications; using RadicadeServer.DB; using RadicadeServer.Extensions; using RadicadeServer.RecNet.Enums.Messages; using RadicadeServer.RecNet.Messages; using RadicadeServer.RecNet.Notifications; using RadicadeServer.RecNet.Relationships; namespace RadicadeServer.API.Relationships.V2; [Route("api/relationships/v2")] public class RelationshipsV2(RecDBContext db) : Controller { [HttpGet("get"), Authorize] public async Task> GetRelationships() { var userId = User.GetUserId(); var rows = await db.Relationships.AsNoTracking() .Where(x => x.AccountId == userId) .ToListAsync(); return rows.Select(relationship => new Relationship(relationship)).ToList(); } [HttpGet("friends"), Authorize] public async Task> GetFriends() { var userId = User.GetUserId(); var rows = await db.Relationships.AsNoTracking() .Where(x => x.AccountId == userId && x.RelationshipType == RelationshipType.Friend) .OrderBy(x => x.RelationshipId) .ToListAsync(); return rows.Select(relationship => new Relationship(relationship)).ToList(); } [HttpGet("sendfriendrequest"), Authorize] [HttpPost("sendfriendrequest"), Authorize] public async Task SendFriendRequest(long id) { var userId = User.GetUserId(); if (userId == null || id <= 0 || id == userId) return BadRequest(new { error = "invalid player id" }); var mine = await RelationshipsShared.GetOrCreate(db, (long)userId, id); var theirs = await RelationshipsShared.GetOrCreate(db, id, (long)userId); if (mine.RelationshipType != RelationshipType.None) return Ok(new Relationship(mine)); mine.RelationshipType = RelationshipType.FriendRequestSent; theirs.RelationshipType = RelationshipType.FriendRequestReceived; await db.SaveChangesAsync(); await Notify.SendNotify((long)userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) }); await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) }); await Notify.SendMessage(id, new Message { FromPlayerId = (long)userId, Type = MessageType.FriendInvite }); return Ok(new Relationship(mine)); } [HttpGet("acceptfriendrequest"), Authorize] [HttpPost("acceptfriendrequest"), Authorize] public async Task AcceptFriendRequest(long id) { var userId = User.GetUserId(); if (userId == null || id <= 0 || id == userId) return BadRequest(new { error = "invalid player id" }); var mine = await db.Relationships .FirstOrDefaultAsync(x => x.AccountId == userId && x.PlayerId == id); var theirs = await db.Relationships .FirstOrDefaultAsync(x => x.AccountId == id && x.PlayerId == userId); if (mine == null || theirs == null || mine.RelationshipType != RelationshipType.FriendRequestReceived || theirs.RelationshipType != RelationshipType.FriendRequestSent) { return Ok(mine != null ? new Relationship(mine) : new Relationship()); } mine.RelationshipType = RelationshipType.Friend; theirs.RelationshipType = RelationshipType.Friend; await db.SaveChangesAsync(); await Notify.SendNotify((long)userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) }); await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) }); await Notify.SendMessage(id, new Message { FromPlayerId = (long)userId, Type = MessageType.FriendRequestAccepted }); return Ok(new Relationship(mine)); } [HttpGet("addfriend"), Authorize] [HttpPost("addfriend"), Authorize] public async Task AddFriend(long id) { var userId = User.GetUserId(); if (userId == null || id <= 0 || id == userId) return BadRequest(new { error = "invalid player id" }); var mine = await RelationshipsShared.GetOrCreate(db, (long)userId, id); var theirs = await RelationshipsShared.GetOrCreate(db, id, (long)userId); if (mine.RelationshipType == RelationshipType.FriendRequestReceived || theirs.RelationshipType == RelationshipType.FriendRequestReceived) { mine.RelationshipType = RelationshipType.Friend; theirs.RelationshipType = RelationshipType.Friend; } else if (mine.RelationshipType == RelationshipType.None && theirs.RelationshipType == RelationshipType.None) { mine.RelationshipType = RelationshipType.FriendRequestSent; theirs.RelationshipType = RelationshipType.FriendRequestReceived; } await db.SaveChangesAsync(); await Notify.SendNotify((long)userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) }); await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) }); await Notify.SendMessage(id, new Message { FromPlayerId = (long)userId, Type = MessageType.FriendInvite }); return Ok(new Relationship(mine)); } [HttpGet("removefriend"), Authorize] [HttpPost("removefriend"), Authorize] public async Task RemoveFriend(long id) { var userId = User.GetUserId(); if (userId == null || id <= 0 || id == userId) return BadRequest(new { error = "invalid player id" }); var mine = await RelationshipsShared.GetOrCreate(db, (long)userId, id); var theirs = await RelationshipsShared.GetOrCreate(db, id, (long)userId); mine.RelationshipType = RelationshipType.None; theirs.RelationshipType = RelationshipType.None; await db.SaveChangesAsync(); await Notify.SendNotify((long)userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) }); await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) }); return Ok(new Relationship(mine)); } }