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;
}
}
}

View file

@ -8,6 +8,7 @@ namespace Sledgemapper.Clients
Task NewTile(Tile tile);
Task NewWall(Wall wall);
Task NewOverlay(Overlay overlay);
Task NewNote(Note note);
Task DeleteTile(Tile tile);
Task DeleteWall(Wall wall);
Task DeleteOverlay(Overlay overlay);

View file

@ -101,6 +101,23 @@ namespace Sledgemapper.Shared.Entities
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)
{
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)
{
if (overlay is null)

View file

@ -42,7 +42,7 @@ namespace Sledgemapper
new HttpClient(new AuthenticatedHttpClientHandler(GetToken))
{
BaseAddress = new Uri("http://hub.michelescandura.com:5000")
// BaseAddress = new Uri("http://localhost:5001")
// BaseAddress = new Uri("http://localhost:5001")
}
);
@ -107,6 +107,12 @@ namespace Sledgemapper
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) =>
{
var p = SessionData.Players.FirstOrDefault(m => m.UserId == player.UserId);
@ -165,6 +171,9 @@ namespace Sledgemapper
case Wall wall:
Queue.Enqueue(async () => await Execute(async () => await Api.NewWall(wall, SessionData.SessionName).ConfigureAwait(false)));
break;
case Note note:
Queue.Enqueue(async () => await Execute(async () => await Api.NewNote(note, SessionData.SessionName).ConfigureAwait(false)));
break;
}
break;
@ -180,6 +189,9 @@ namespace Sledgemapper
case Wall wall:
Queue.Enqueue(async () => await Execute(async () => await Api.DeleteWall(wall, SessionData.SessionName).ConfigureAwait(false)));
break;
case Note note:
Queue.Enqueue(async () => await Execute(async () => await Api.DeleteNote(note, SessionData.SessionName).ConfigureAwait(false)));
break;
}
break;
}

View file

@ -23,6 +23,9 @@ namespace Sledgemapper
[Post("/session/{sessionName}/overlay")]
Task NewOverlay([Body] Overlay overlay, string sessionName);
[Post("/session/{sessionName}/note")]
Task NewNote([Body] Note note, string sessionName);
[Post("/session/{sessionName}/wall")]
Task NewWall([Body] Wall overlay, string sessionName);
@ -35,12 +38,15 @@ namespace Sledgemapper
[Delete("/session/{sessionName}/overlay")]
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")]
Task<HttpResponseMessage> Register([Body] RegisterModel registerModel);
[Headers("Authorization")]
[Headers("Authorization")]
[Post("/users/authenticate")]
Task<AuthenticateResponse> Authenticate([Body] AuthenticateModel registerModel);
}

View file

@ -57,7 +57,7 @@ namespace Sledgemapper
_communicationManager.Connection.Reconnecting += OnHubReconnecting;
_communicationManager.Connection.Closed += OnHubDisconnected;
_state = new State();
_settings= new Settings();
_settings = new Settings();
}
private async Task OnHubDisconnected(Exception arg)
@ -137,17 +137,17 @@ namespace Sledgemapper
private void OneMenuFileSettingsSelected(object sender, EventArgs e)
{
var propertyGrid = new PropertyGrid
{
Object = _settings,
Width = 350
};
var propertyGrid = new PropertyGrid
{
Object = _settings,
Width = 350
};
var _windowEditor = new Window
{
Title = "Object Editor",
Content = propertyGrid
};
var _windowEditor = new Window
{
Title = "Object Editor",
Content = propertyGrid
};
_windowEditor.ShowModal(_desktop);
}
@ -269,9 +269,29 @@ namespace Sledgemapper
_state.SelectedNote.X = _state.HoveredTile.X;
_state.SelectedNote.Y = _state.HoveredTile.Y;
var contextMenu = new TextButton { Text = "New Note" };
contextMenu.Click += OnContextMenuNewNoteClick;
_desktop.ShowContextMenu(contextMenu, mouseState.Position);
var popup = new VerticalStackPanel { Padding = new Myra.Graphics2D.Thickness(1), Spacing = 2, Background = new SolidBrush(Color.DarkGray) };
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)
@ -590,8 +610,8 @@ namespace Sledgemapper
_spriteBatch.Draw(
_comment,
new Rectangle(
note.X * _state.TileSize + _state.TileSize - (int)(_state.TileSize / 2) + _state.TileSize / 20,
note.Y * _state.TileSize + _state.TileSize / 8 + _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 / 25,
(int)(_state.TileSize / 2.5), (int)(_state.TileSize / 2.5 / 1.136)
), Color.Black * .2f
);
@ -916,63 +936,14 @@ namespace Sledgemapper
var localWindow = (Window)container;
var localContent = localWindow.Content as NoteWindow;
if (!button.Enabled)
var note = new Note
{
return;
}
var isValid = true;
isValid &= ValidateTextbox(localContent.NoteText);
if (!isValid)
{
return;
}
var successful = false;
try
{
var note = new Note
{
X = _state.SelectedNote.X,
Y = _state.SelectedNote.Y,
Text = localContent.NoteText.Text
};
_sessionData.Notes.AddOrUpdate(note.ToString(), note, (key, oldValue) => 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();
}
X = _state.SelectedNote.X,
Y = _state.SelectedNote.Y,
Text = localContent.NoteText.Text
};
_sessionData.NewNote(note);
localWindow.Close();
}
private async void OnButtonNoteCancelClick(object sender, EventArgs e)
@ -1162,6 +1133,18 @@ namespace Sledgemapper
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)
{
_state.CurrentOverlayId = ((ImageButton)sender).Id;