backend note implementation.

This commit is contained in:
Michele Scandura 2020-12-03 19:38:03 +00:00
parent 651aeb407b
commit fe3c0ed2cf
11 changed files with 185 additions and 75 deletions

View file

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewNoteCommand : BaseCommand<bool>
{
public Note Note { get; private set; }
public NewNoteCommand(string sessionName, Note note, int userId) : base(sessionName, userId)
{
Note = note;
}
}
}

View file

@ -54,6 +54,12 @@ namespace Sledgemapper.Api.Controllers
await _mediator.Send(new NewWallCommand(sessionName, wall, UserId));
}
[HttpPost("note")]
public async Task Post(string sessionName, [FromBody] Note note)
{
await _mediator.Send(new NewNoteCommand(sessionName, note, UserId));
}
[HttpDelete("tile")]
public async Task Delete(string sessionName, [FromBody] Tile tile)
{

View file

@ -36,4 +36,31 @@ 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;
}
}
}

View file

@ -19,4 +19,16 @@ 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);
}
}
}

View file

@ -56,6 +56,11 @@ namespace Sledgemapper.Api.Hubs
await Clients.Group(sessionName).NewOverlay(tile);
}
public async Task NewNote(string sessionName, Note note)
{
await Clients.Group(sessionName).NewNote(note);
}
public async Task DeleteTile(string sessionName, Tile tile)
{
await Clients.Group(sessionName).DeleteTile(tile);

View file

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Notifications
{
public class NewNoteNotification : BaseNotification
{
public Note Note { get; private set; }
public NewNoteNotification(Models.Session session, Note note, int userId) : base(session, userId)
{
Note = note;
}
}
}