Merge branch 'refactoring' of https://git.michelescandura.com/michele/Map into refactoring

This commit is contained in:
Michele 2020-11-13 22:07:04 +00:00
commit ea9cc32534
15 changed files with 135 additions and 44 deletions

View file

@ -0,0 +1,15 @@
using MediatR;
namespace Sledgemapper.Api.Commands
{
public class NewSessionCommand : IRequest<bool>
{
public string SessionName { get; set; }
public int UserId { get; }
public NewSessionCommand(string sessionName, int userId)
{
SessionName = sessionName;
UserId = userId;
}
}
}

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Handlers;
using Sledgemapper.Shared.Entities;
using System.Threading.Tasks;
@ -8,13 +9,22 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Controllers
{
[Authorize]
[Route("[controller]/{sessionName}")]
[Route("[controller]/{sessionName}")]
public class SessionController : ControllerBase
{
private readonly IMediator _mediator;
public SessionController(IMediator mediator) { _mediator = mediator; }
[HttpPost]
public async Task Post(string sessionName)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
var result = await _mediator.Send(new NewSessionCommand(sessionName, userId));
}
[HttpPost("tile")]
public async Task Post(string sessionName, [FromBody]Tile tile)
{

View file

@ -0,0 +1,32 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Data;
using Sledgemapper.Api.Models;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class StartNewSessionHandler : IRequestHandler<NewSessionCommand, bool>
{
private readonly IMediator _mediator;
private readonly MyDbContext _dbcontext;
public StartNewSessionHandler(IMediator mediator, MyDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<bool> Handle(NewSessionCommand notification, CancellationToken cancellationToken)
{
// _dbcontext.MapLogs.Add(new Session
// {
// SessionName = notification.SessionName,
// OwnerUserId = notification.UserId
// });
await _dbcontext.SaveChangesAsync();
return true;
}
}
}

View file

@ -15,12 +15,15 @@ using Microsoft.AspNetCore.Authorization;
namespace SignalRChat.Hubs
{
[Authorize]
public class SledgemapperHub : Hub<ISledgemapperClient>
{
public SledgemapperHub() {
}
public SledgemapperHub()
{
}
private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
public List<string> Colors = new List<string>{"CC0000",
"CC3300",
@ -113,6 +116,10 @@ namespace SignalRChat.Hubs
public async Task<Session> NewSession(string sessionName, string initials)
{
var userId = int.Parse(Context.User.Identity.Name);
// var user = this.Context.GetHttpContext().;
var session = new Session();
session.Colors = new List<string>(Colors);
session.Colors.Shuffle();
@ -120,7 +127,7 @@ namespace SignalRChat.Hubs
session.Players.Add(player);
_sessions.Add(sessionName, session);
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
return session;
}

View file

@ -0,0 +1,17 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Sledgemapper.Api.Models
{
public class Session
{
[Key]
public int SessionId { get; set; }
[Required]
public string SessionName{get;set;}
[Required]
public int OwnerUserId { get; set; }
}
}

Binary file not shown.

View file

@ -0,0 +1,8 @@
namespace Sledgemapper.Api.Handlers
{
public class NewSessionNotification : BaseNotification
{
public int UserId { get; }
public NewSessionNotification(string sessionName, int userId) : base(sessionName) => UserId = userId;
}
}

View file

@ -19,7 +19,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Text;
using AutoMapper;
using Sledgemapper.Services;
using System.Security.Claims;
namespace SignalRChat
{
@ -79,6 +79,8 @@ namespace SignalRChat
// return unauthorized if user no longer exists
context.Fail("Unauthorized");
}
context.HttpContext.User.Claims.Append(new Claim(ClaimTypes.Name, user.Username));
context.HttpContext.User.Claims.Append(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
return Task.CompletedTask;
}
};

Binary file not shown.