backend note implementation.
This commit is contained in:
parent
651aeb407b
commit
fe3c0ed2cf
11 changed files with 185 additions and 75 deletions
14
Sledgemapper.Api/Commands/NewNoteCommand.cs
Normal file
14
Sledgemapper.Api/Commands/NewNoteCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,6 +54,12 @@ namespace Sledgemapper.Api.Controllers
|
||||||
await _mediator.Send(new NewWallCommand(sessionName, wall, UserId));
|
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")]
|
[HttpDelete("tile")]
|
||||||
public async Task Delete(string sessionName, [FromBody] Tile tile)
|
public async Task Delete(string sessionName, [FromBody] Tile tile)
|
||||||
{
|
{
|
||||||
|
|
|
@ -36,4 +36,31 @@ namespace Sledgemapper.Api.Handlers
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,4 +19,16 @@ namespace Sledgemapper.Api.Handlers
|
||||||
await _hub.Clients.Groups(notification.Session.SessionName).NewWall(notification.Wall);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,11 @@ namespace Sledgemapper.Api.Hubs
|
||||||
await Clients.Group(sessionName).NewOverlay(tile);
|
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)
|
public async Task DeleteTile(string sessionName, Tile tile)
|
||||||
{
|
{
|
||||||
await Clients.Group(sessionName).DeleteTile(tile);
|
await Clients.Group(sessionName).DeleteTile(tile);
|
||||||
|
|
14
Sledgemapper.Api/Notifications/NewNoteNotification.cs
Normal file
14
Sledgemapper.Api/Notifications/NewNoteNotification.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ namespace Sledgemapper.Clients
|
||||||
Task NewTile(Tile tile);
|
Task NewTile(Tile tile);
|
||||||
Task NewWall(Wall wall);
|
Task NewWall(Wall wall);
|
||||||
Task NewOverlay(Overlay overlay);
|
Task NewOverlay(Overlay overlay);
|
||||||
|
Task NewNote(Note note);
|
||||||
Task DeleteTile(Tile tile);
|
Task DeleteTile(Tile tile);
|
||||||
Task DeleteWall(Wall wall);
|
Task DeleteWall(Wall wall);
|
||||||
Task DeleteOverlay(Overlay overlay);
|
Task DeleteOverlay(Overlay overlay);
|
||||||
|
|
|
@ -101,6 +101,23 @@ namespace Sledgemapper.Shared.Entities
|
||||||
OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newWall));
|
OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newWall));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void NewNote(Note selectedNote)
|
||||||
|
{
|
||||||
|
if (selectedNote is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var noteExists = Notes.TryGetValue(selectedNote.ToString(), out var note);
|
||||||
|
var newNote = new Note { X = selectedNote.X, Y = selectedNote.Y, Text=selectedNote.Text };
|
||||||
|
if (noteExists)
|
||||||
|
{
|
||||||
|
Walls.TryRemove(note.ToString(), out var _);
|
||||||
|
}
|
||||||
|
|
||||||
|
Notes.TryAdd(newNote.ToString(), newNote);
|
||||||
|
OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newNote));
|
||||||
|
}
|
||||||
|
|
||||||
public void DeleteWall(Wall wall)
|
public void DeleteWall(Wall wall)
|
||||||
{
|
{
|
||||||
if (wall is null)
|
if (wall is null)
|
||||||
|
@ -114,6 +131,19 @@ namespace Sledgemapper.Shared.Entities
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DeleteNote(Note note)
|
||||||
|
{
|
||||||
|
if (note is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var removed = Notes.TryRemove(note.ToString(), out var _);
|
||||||
|
if (removed)
|
||||||
|
{
|
||||||
|
OnRaiseMapEntityDeletedEvent(new MapEntityDeletedEventArgs(note));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void DeleteOverlay(Overlay overlay)
|
public void DeleteOverlay(Overlay overlay)
|
||||||
{
|
{
|
||||||
if (overlay is null)
|
if (overlay is null)
|
||||||
|
|
|
@ -107,6 +107,12 @@ namespace Sledgemapper
|
||||||
SessionData.Overlays.TryAdd(tile.ToString(), tile);
|
SessionData.Overlays.TryAdd(tile.ToString(), tile);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Connection.On<Note>("NewNote", (note) =>
|
||||||
|
{
|
||||||
|
//SessionData.Notes.Remove(note.ToString(), out var _);
|
||||||
|
SessionData.Notes.AddOrUpdate(note.ToString(), note, (key, oldnote) => note);
|
||||||
|
});
|
||||||
|
|
||||||
Connection.On<Player>("NewPlayer", (player) =>
|
Connection.On<Player>("NewPlayer", (player) =>
|
||||||
{
|
{
|
||||||
var p = SessionData.Players.FirstOrDefault(m => m.UserId == player.UserId);
|
var p = SessionData.Players.FirstOrDefault(m => m.UserId == player.UserId);
|
||||||
|
@ -165,6 +171,9 @@ namespace Sledgemapper
|
||||||
case Wall wall:
|
case Wall wall:
|
||||||
Queue.Enqueue(async () => await Execute(async () => await Api.NewWall(wall, SessionData.SessionName).ConfigureAwait(false)));
|
Queue.Enqueue(async () => await Execute(async () => await Api.NewWall(wall, SessionData.SessionName).ConfigureAwait(false)));
|
||||||
break;
|
break;
|
||||||
|
case Note note:
|
||||||
|
Queue.Enqueue(async () => await Execute(async () => await Api.NewNote(note, SessionData.SessionName).ConfigureAwait(false)));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -180,6 +189,9 @@ namespace Sledgemapper
|
||||||
case Wall wall:
|
case Wall wall:
|
||||||
Queue.Enqueue(async () => await Execute(async () => await Api.DeleteWall(wall, SessionData.SessionName).ConfigureAwait(false)));
|
Queue.Enqueue(async () => await Execute(async () => await Api.DeleteWall(wall, SessionData.SessionName).ConfigureAwait(false)));
|
||||||
break;
|
break;
|
||||||
|
case Note note:
|
||||||
|
Queue.Enqueue(async () => await Execute(async () => await Api.DeleteNote(note, SessionData.SessionName).ConfigureAwait(false)));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,9 @@ namespace Sledgemapper
|
||||||
[Post("/session/{sessionName}/overlay")]
|
[Post("/session/{sessionName}/overlay")]
|
||||||
Task NewOverlay([Body] Overlay overlay, string sessionName);
|
Task NewOverlay([Body] Overlay overlay, string sessionName);
|
||||||
|
|
||||||
|
[Post("/session/{sessionName}/note")]
|
||||||
|
Task NewNote([Body] Note note, string sessionName);
|
||||||
|
|
||||||
[Post("/session/{sessionName}/wall")]
|
[Post("/session/{sessionName}/wall")]
|
||||||
Task NewWall([Body] Wall overlay, string sessionName);
|
Task NewWall([Body] Wall overlay, string sessionName);
|
||||||
|
|
||||||
|
@ -35,6 +38,9 @@ namespace Sledgemapper
|
||||||
[Delete("/session/{sessionName}/overlay")]
|
[Delete("/session/{sessionName}/overlay")]
|
||||||
Task DeleteOverlay([Body] Overlay overlay, string sessionName);
|
Task DeleteOverlay([Body] Overlay overlay, string sessionName);
|
||||||
|
|
||||||
|
[Delete("/session/{sessionName}/note")]
|
||||||
|
Task DeleteNote([Body] Note overlay, string sessionName);
|
||||||
|
|
||||||
|
|
||||||
[Headers("Authorization")]
|
[Headers("Authorization")]
|
||||||
[Post("/users/register")]
|
[Post("/users/register")]
|
||||||
|
|
|
@ -269,9 +269,29 @@ namespace Sledgemapper
|
||||||
_state.SelectedNote.X = _state.HoveredTile.X;
|
_state.SelectedNote.X = _state.HoveredTile.X;
|
||||||
_state.SelectedNote.Y = _state.HoveredTile.Y;
|
_state.SelectedNote.Y = _state.HoveredTile.Y;
|
||||||
|
|
||||||
var contextMenu = new TextButton { Text = "New Note" };
|
var popup = new VerticalStackPanel { Padding = new Myra.Graphics2D.Thickness(1), Spacing = 2, Background = new SolidBrush(Color.DarkGray) };
|
||||||
contextMenu.Click += OnContextMenuNewNoteClick;
|
|
||||||
_desktop.ShowContextMenu(contextMenu, mouseState.Position);
|
if (!_sessionData.Notes.ContainsKey(_state.SelectedNote.ToString()))
|
||||||
|
{
|
||||||
|
var newNoteButton = new TextButton { Text = "New Note", Width = 80, Height = 20, Padding = new Myra.Graphics2D.Thickness(2), HorizontalAlignment = HorizontalAlignment.Left };
|
||||||
|
newNoteButton.Click += OnContextMenuNewNoteClick;
|
||||||
|
popup.AddChild(newNoteButton);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_sessionData.Notes.TryGetValue(_state.SelectedNote.ToString(), out var n);
|
||||||
|
_state.SelectedNote = n;
|
||||||
|
var viewNoteButton = new TextButton { Text = "View Note", Width = 80, Height = 20, Padding = new Myra.Graphics2D.Thickness(2), HorizontalAlignment = HorizontalAlignment.Left };
|
||||||
|
var deleteNoteButton = new TextButton { Text = "Delete Note", Width = 80, Height = 20, Padding = new Myra.Graphics2D.Thickness(2), HorizontalAlignment = HorizontalAlignment.Left };
|
||||||
|
viewNoteButton.Click += OnContextMenuViewNoteClick;
|
||||||
|
deleteNoteButton.Click += OnContextMenuDeleteNoteClick;
|
||||||
|
|
||||||
|
|
||||||
|
popup.AddChild(viewNoteButton);
|
||||||
|
popup.AddChild(deleteNoteButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
_desktop.ShowContextMenu(popup, mouseState.Position);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newState.IsKeyDown(Keys.LeftControl)
|
if (newState.IsKeyDown(Keys.LeftControl)
|
||||||
|
@ -590,8 +610,8 @@ namespace Sledgemapper
|
||||||
_spriteBatch.Draw(
|
_spriteBatch.Draw(
|
||||||
_comment,
|
_comment,
|
||||||
new Rectangle(
|
new Rectangle(
|
||||||
note.X * _state.TileSize + _state.TileSize - (int)(_state.TileSize / 2) + _state.TileSize / 20,
|
note.X * _state.TileSize + _state.TileSize - (int)(_state.TileSize / 2) + _state.TileSize / 25,
|
||||||
note.Y * _state.TileSize + _state.TileSize / 8 + _state.TileSize / 20,
|
note.Y * _state.TileSize + _state.TileSize / 8 + _state.TileSize / 25,
|
||||||
(int)(_state.TileSize / 2.5), (int)(_state.TileSize / 2.5 / 1.136)
|
(int)(_state.TileSize / 2.5), (int)(_state.TileSize / 2.5 / 1.136)
|
||||||
), Color.Black * .2f
|
), Color.Black * .2f
|
||||||
);
|
);
|
||||||
|
@ -916,64 +936,15 @@ namespace Sledgemapper
|
||||||
|
|
||||||
var localWindow = (Window)container;
|
var localWindow = (Window)container;
|
||||||
var localContent = localWindow.Content as NoteWindow;
|
var localContent = localWindow.Content as NoteWindow;
|
||||||
|
|
||||||
if (!button.Enabled)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var isValid = true;
|
|
||||||
isValid &= ValidateTextbox(localContent.NoteText);
|
|
||||||
|
|
||||||
|
|
||||||
if (!isValid)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var successful = false;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var note = new Note
|
var note = new Note
|
||||||
{
|
{
|
||||||
X = _state.SelectedNote.X,
|
X = _state.SelectedNote.X,
|
||||||
Y = _state.SelectedNote.Y,
|
Y = _state.SelectedNote.Y,
|
||||||
Text = localContent.NoteText.Text
|
Text = localContent.NoteText.Text
|
||||||
};
|
};
|
||||||
_sessionData.Notes.AddOrUpdate(note.ToString(), note, (key, oldValue) => note);
|
_sessionData.NewNote(note);
|
||||||
|
|
||||||
button.Text = "Wait...";
|
|
||||||
// localContent.LblLoginError.Text = "";
|
|
||||||
// localContent.LblLoginError.Visible = false;
|
|
||||||
// _authResponse = await _communicationManager.Login(new AuthenticateModel
|
|
||||||
// {
|
|
||||||
// Username = localContent.TxtEmail.Text,
|
|
||||||
// Password = localContent.TxtPassword.Text
|
|
||||||
// });
|
|
||||||
// successful = _authResponse != null;
|
|
||||||
successful = true;
|
|
||||||
}
|
|
||||||
catch (Refit.ApiException refitException)
|
|
||||||
{
|
|
||||||
// localContent.LblLoginError.Text = refitException.Content;
|
|
||||||
// localContent.LblLoginError.Visible = true;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
// localContent.LblLoginError.Text = "Can't connect to the server";
|
|
||||||
// localContent.LblLoginError.Visible = true;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
button.Enabled = true;
|
|
||||||
button.Text = "OK";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (successful)
|
|
||||||
{
|
|
||||||
localWindow.Close();
|
localWindow.Close();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private async void OnButtonNoteCancelClick(object sender, EventArgs e)
|
private async void OnButtonNoteCancelClick(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
@ -1162,6 +1133,18 @@ namespace Sledgemapper
|
||||||
window.ShowModal(_desktop);
|
window.ShowModal(_desktop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnContextMenuDeleteNoteClick(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
_desktop.HideContextMenu();
|
||||||
|
_sessionData.DeleteNote(_state.SelectedNote);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnContextMenuViewNoteClick(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
_desktop.HideContextMenu();
|
||||||
|
EditNote(_state.SelectedNote);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnOverlayButtonClicked(object sender, EventArgs e)
|
private void OnOverlayButtonClicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
_state.CurrentOverlayId = ((ImageButton)sender).Id;
|
_state.CurrentOverlayId = ((ImageButton)sender).Id;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue