backend
This commit is contained in:
parent
24b773d845
commit
d3f8fd23f4
21 changed files with 297 additions and 49 deletions
14
Sledgemapper.Api/Commands/NewLineCommand.cs
Normal file
14
Sledgemapper.Api/Commands/NewLineCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewLineCommand : BaseCommand<bool>
|
||||
{
|
||||
public Line Line { get; private set; }
|
||||
|
||||
public NewLineCommand(string sessionName, Line line, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Line = line;
|
||||
}
|
||||
}
|
||||
}
|
14
Sledgemapper.Api/Commands/NewRoomCommand.cs
Normal file
14
Sledgemapper.Api/Commands/NewRoomCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewRoomCommand : BaseCommand<bool>
|
||||
{
|
||||
public Room Room { get; private set; }
|
||||
|
||||
public NewRoomCommand(string sessionName, Room room, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Room = room;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -60,6 +60,18 @@ namespace Sledgemapper.Api.Controllers
|
|||
await _mediator.Send(new NewNoteCommand(sessionName, note, UserId));
|
||||
}
|
||||
|
||||
[HttpPost("room")]
|
||||
public async Task Post(string sessionName, [FromBody] Room room)
|
||||
{
|
||||
await _mediator.Send(new NewRoomCommand(sessionName, room, UserId));
|
||||
}
|
||||
|
||||
[HttpPost("line")]
|
||||
public async Task Post(string sessionName, [FromBody] Line line)
|
||||
{
|
||||
await _mediator.Send(new NewLineCommand(sessionName, line, UserId));
|
||||
}
|
||||
|
||||
[HttpDelete("tile")]
|
||||
public async Task Delete(string sessionName, [FromBody] Tile tile)
|
||||
{
|
||||
|
|
39
Sledgemapper.Api/Handlers/NewLineCommandHandler.cs
Normal file
39
Sledgemapper.Api/Handlers/NewLineCommandHandler.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using MediatR;
|
||||
using Sledgemapper.Api.Data;
|
||||
using Sledgemapper.Shared.Entities;
|
||||
using Sledgemapper.Api.Commands;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Notifications;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class NewLineCommandHandler : IRequestHandler<NewLineCommand, bool>
|
||||
{
|
||||
private readonly MyDbContext _dbcontext;
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public NewLineCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
||||
|
||||
public async Task<bool> Handle(NewLineCommand notification, CancellationToken cancellationToken)
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize<Line>(notification.Line);
|
||||
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
|
||||
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
||||
{
|
||||
Operation = "N",
|
||||
SessionId = session.SessionId,
|
||||
Type = "L",
|
||||
Timestamp = notification.Timestamp,
|
||||
Object = jsonString,
|
||||
UserId = notification.UserId,
|
||||
});
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
await _mediator.Publish(new NewLineNotification(session, notification.Line, notification.UserId));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
39
Sledgemapper.Api/Handlers/NewNoteCommandHandler.cs
Normal file
39
Sledgemapper.Api/Handlers/NewNoteCommandHandler.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using MediatR;
|
||||
using Sledgemapper.Api.Data;
|
||||
using Sledgemapper.Shared.Entities;
|
||||
using Sledgemapper.Api.Commands;
|
||||
using Sledgemapper.Api.Notifications;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class NewNoteCommandHandler : IRequestHandler<NewNoteCommand, bool>
|
||||
{
|
||||
private readonly MyDbContext _dbcontext;
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public NewNoteCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
||||
|
||||
public async Task<bool> Handle(NewNoteCommand notification, CancellationToken cancellationToken)
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize<Note>(notification.Note);
|
||||
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
|
||||
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
||||
{
|
||||
Operation = "N",
|
||||
SessionId = session.SessionId,
|
||||
Type = "N",
|
||||
Timestamp = notification.Timestamp,
|
||||
Object = jsonString,
|
||||
UserId = notification.UserId,
|
||||
});
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
await _mediator.Publish(new NewNoteNotification(session, notification.Note, notification.UserId));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
39
Sledgemapper.Api/Handlers/NewRoomCommandHandler.cs
Normal file
39
Sledgemapper.Api/Handlers/NewRoomCommandHandler.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using MediatR;
|
||||
using Sledgemapper.Api.Data;
|
||||
using Sledgemapper.Shared.Entities;
|
||||
using Sledgemapper.Api.Commands;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Notifications;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class NewRoomCommandHandler : IRequestHandler<NewRoomCommand, bool>
|
||||
{
|
||||
private readonly MyDbContext _dbcontext;
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public NewRoomCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
||||
|
||||
public async Task<bool> Handle(NewRoomCommand notification, CancellationToken cancellationToken)
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize<Room>(notification.Room);
|
||||
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
|
||||
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
||||
{
|
||||
Operation = "N",
|
||||
SessionId = session.SessionId,
|
||||
Type = "R",
|
||||
Timestamp = notification.Timestamp,
|
||||
Object = jsonString,
|
||||
UserId = notification.UserId,
|
||||
});
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
await _mediator.Publish(new NewRoomNotification(session, notification.Room, notification.UserId));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,31 +36,4 @@ namespace Sledgemapper.Api.Handlers
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class NewNoteCommandHandler : IRequestHandler<NewNoteCommand, bool>
|
||||
{
|
||||
private readonly MyDbContext _dbcontext;
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public NewNoteCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
||||
|
||||
public async Task<bool> Handle(NewNoteCommand notification, CancellationToken cancellationToken)
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize<Note>(notification.Note);
|
||||
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
|
||||
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
||||
{
|
||||
Operation = "N",
|
||||
SessionId = session.SessionId,
|
||||
Type = "N",
|
||||
Timestamp = notification.Timestamp,
|
||||
Object = jsonString,
|
||||
UserId = notification.UserId,
|
||||
});
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
await _mediator.Publish(new NewNoteNotification(session, notification.Note, notification.UserId));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
22
Sledgemapper.Api/Handlers/SendNewLineMessage.cs
Normal file
22
Sledgemapper.Api/Handlers/SendNewLineMessage.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using MediatR;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Sledgemapper.Api.Notifications;
|
||||
using Sledgemapper.Clients;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Hubs;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class SendNewLineMessage : INotificationHandler<NewLineNotification>
|
||||
{
|
||||
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
|
||||
|
||||
public SendNewLineMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
|
||||
|
||||
public async Task Handle(NewLineNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
await _hub.Clients.Groups(notification.Session.SessionName).NewLine(notification.Line);
|
||||
}
|
||||
}
|
||||
}
|
22
Sledgemapper.Api/Handlers/SendNewNoteMessage.cs
Normal file
22
Sledgemapper.Api/Handlers/SendNewNoteMessage.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using MediatR;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Sledgemapper.Api.Notifications;
|
||||
using Sledgemapper.Clients;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Hubs;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class SendNewNoteMessage : INotificationHandler<NewNoteNotification>
|
||||
{
|
||||
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
|
||||
|
||||
public SendNewNoteMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
|
||||
|
||||
public async Task Handle(NewNoteNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
await _hub.Clients.Groups(notification.Session.SessionName).NewNote(notification.Note);
|
||||
}
|
||||
}
|
||||
}
|
22
Sledgemapper.Api/Handlers/SendNewRoomMessage.cs
Normal file
22
Sledgemapper.Api/Handlers/SendNewRoomMessage.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using MediatR;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Sledgemapper.Api.Notifications;
|
||||
using Sledgemapper.Clients;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Hubs;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class SendNewRoomMessage : INotificationHandler<NewRoomNotification>
|
||||
{
|
||||
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
|
||||
|
||||
public SendNewRoomMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
|
||||
|
||||
public async Task Handle(NewRoomNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
await _hub.Clients.Groups(notification.Session.SessionName).NewRoom(notification.Room);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,16 +19,4 @@ namespace Sledgemapper.Api.Handlers
|
|||
await _hub.Clients.Groups(notification.Session.SessionName).NewWall(notification.Wall);
|
||||
}
|
||||
}
|
||||
|
||||
public class SendNewNoteMessage : INotificationHandler<NewNoteNotification>
|
||||
{
|
||||
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
|
||||
|
||||
public SendNewNoteMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
|
||||
|
||||
public async Task Handle(NewNoteNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
await _hub.Clients.Groups(notification.Session.SessionName).NewNote(notification.Note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,16 @@ namespace Sledgemapper.Api.Hubs
|
|||
await Clients.Group(sessionName).NewTile(tile);
|
||||
}
|
||||
|
||||
public async Task NewRoom(string sessionName, Room room)
|
||||
{
|
||||
await Clients.Group(sessionName).NewRoom(room);
|
||||
}
|
||||
|
||||
public async Task NewLine(string sessionName, Line line)
|
||||
{
|
||||
await Clients.Group(sessionName).NewLine(line);
|
||||
}
|
||||
|
||||
public async Task NewWall(string sessionName, Wall tile)
|
||||
{
|
||||
await Clients.Group(sessionName).NewWall(tile);
|
||||
|
@ -113,7 +123,7 @@ namespace Sledgemapper.Api.Hubs
|
|||
var userId = int.Parse(Context.User.Identity.Name);
|
||||
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == sessionId).OrderBy(m => m.UserId).ToList();
|
||||
var user = _datacontext.Users.First(u => u.Id == userId);
|
||||
var player = new Player { UserId = userId, Initials = user.Initials, Position = tile, Color = UserColors[userId]};
|
||||
var player = new Player { UserId = userId, Initials = user.Initials, Position = tile, Color = UserColors[userId] };
|
||||
await Clients.Group(sessionName).PlayerUpdate(player);
|
||||
}
|
||||
|
||||
|
@ -131,7 +141,7 @@ namespace Sledgemapper.Api.Hubs
|
|||
public override async Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
var userConnection = _dbContext.UserConnections.FirstOrDefault(m => m.ConnectionId == Context.ConnectionId);
|
||||
var userId=userConnection.UserId;
|
||||
var userId = userConnection.UserId;
|
||||
if (userConnection != null)
|
||||
{
|
||||
_dbContext.UserConnections.Remove(userConnection);
|
||||
|
@ -142,8 +152,8 @@ namespace Sledgemapper.Api.Hubs
|
|||
foreach (var userSession in userSessions)
|
||||
{
|
||||
var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionId == userSession.SessionId);
|
||||
|
||||
await Clients.Group(session.SessionName).RemovePlayer(new Player{UserId=userId}); //send remove player
|
||||
|
||||
await Clients.Group(session.SessionName).RemovePlayer(new Player { UserId = userId }); //send remove player
|
||||
_dbContext.SessionUsers.Remove(userSession);
|
||||
}
|
||||
}
|
||||
|
|
14
Sledgemapper.Api/Notifications/NewLineNotification.cs
Normal file
14
Sledgemapper.Api/Notifications/NewLineNotification.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Notifications
|
||||
{
|
||||
public class NewLineNotification : BaseNotification
|
||||
{
|
||||
public Line Line { get; private set; }
|
||||
|
||||
public NewLineNotification(Models.Session session, Line line, int userId) : base(session, userId)
|
||||
{
|
||||
Line = line;
|
||||
}
|
||||
}
|
||||
}
|
14
Sledgemapper.Api/Notifications/NewRoomNotification.cs
Normal file
14
Sledgemapper.Api/Notifications/NewRoomNotification.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Notifications
|
||||
{
|
||||
public class NewRoomNotification : BaseNotification
|
||||
{
|
||||
public Room Room { get; private set; }
|
||||
|
||||
public NewRoomNotification(Models.Session session, Room room, int userId) : base(session, userId)
|
||||
{
|
||||
Room = room;
|
||||
}
|
||||
}
|
||||
}
|
BIN
Sledgemapper.Api/db/LocalDatabase.db
Normal file
BIN
Sledgemapper.Api/db/LocalDatabase.db
Normal file
Binary file not shown.
BIN
Sledgemapper.Api/db/sledgemapper.db
Normal file
BIN
Sledgemapper.Api/db/sledgemapper.db
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue