Merge pull request #22 from StewKI/feature-whiteboard-query-getall
Implement two queries
This commit is contained in:
@@ -3,5 +3,5 @@
|
|||||||
public interface IQueryHandler<in TQuery, TResult>
|
public interface IQueryHandler<in TQuery, TResult>
|
||||||
where TQuery : IQuery<TResult>
|
where TQuery : IQuery<TResult>
|
||||||
{
|
{
|
||||||
Task<TResult> HandleAsync(TQuery query, CancellationToken cancellationToken = default);
|
Task<TResult> Handle(TQuery query, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using AipsCore.Application.Abstract.Query;
|
||||||
|
|
||||||
|
namespace AipsCore.Application.Models.User.Query.GetUser;
|
||||||
|
|
||||||
|
public record GetUserQuery(string UserId) : IQuery<Infrastructure.Persistence.User.User>;
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using AipsCore.Application.Abstract.Query;
|
||||||
|
using AipsCore.Domain.Common.Validation;
|
||||||
|
using AipsCore.Domain.Models.User.Validation;
|
||||||
|
using AipsCore.Domain.Models.User.ValueObjects;
|
||||||
|
using AipsCore.Infrastructure.Persistence.Db;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace AipsCore.Application.Models.User.Query.GetUser;
|
||||||
|
|
||||||
|
public class GetUserQueryHandler : IQueryHandler<GetUserQuery, Infrastructure.Persistence.User.User>
|
||||||
|
{
|
||||||
|
private readonly AipsDbContext _context;
|
||||||
|
|
||||||
|
public GetUserQueryHandler(AipsDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Infrastructure.Persistence.User.User> Handle(GetUserQuery query, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var result = await _context.Users
|
||||||
|
.Where(u => u.Id.ToString() == query.UserId)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (result is null)
|
||||||
|
{
|
||||||
|
throw new ValidationException(UserErrors.NotFound(new UserId(query.UserId)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using AipsCore.Application.Abstract.Query;
|
||||||
|
|
||||||
|
namespace AipsCore.Application.Models.Whiteboard.Query.GetRecentWhiteboards;
|
||||||
|
|
||||||
|
public record GetRecentWhiteboardsQuery(string UserId): IQuery<ICollection<Infrastructure.Persistence.Whiteboard.Whiteboard>>;
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using AipsCore.Application.Abstract.Query;
|
||||||
|
using AipsCore.Infrastructure.Persistence.Db;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace AipsCore.Application.Models.Whiteboard.Query.GetRecentWhiteboards;
|
||||||
|
|
||||||
|
public class GetRecentWhiteboardsQueryHandler : IQueryHandler<GetRecentWhiteboardsQuery, ICollection<Infrastructure.Persistence.Whiteboard.Whiteboard>>
|
||||||
|
{
|
||||||
|
private readonly AipsDbContext _context;
|
||||||
|
|
||||||
|
public GetRecentWhiteboardsQueryHandler(AipsDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ICollection<Infrastructure.Persistence.Whiteboard.Whiteboard>> Handle(GetRecentWhiteboardsQuery query, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await GetQuery(query.UserId).ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<Infrastructure.Persistence.Whiteboard.Whiteboard> GetQuery(string userId)
|
||||||
|
{
|
||||||
|
Guid userIdGuid = Guid.Parse(userId);
|
||||||
|
|
||||||
|
return _context.WhiteboardMemberships
|
||||||
|
.Include(m => m.Whiteboard)
|
||||||
|
.Where(m => (
|
||||||
|
m.UserId == userIdGuid &&
|
||||||
|
m.IsBanned == false &&
|
||||||
|
m.Whiteboard != null
|
||||||
|
))
|
||||||
|
.OrderByDescending(m => m.LastInteractedAt)
|
||||||
|
.Select(m => m.Whiteboard!);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using AipsCore.Application.Abstract;
|
using AipsCore.Application.Abstract;
|
||||||
using AipsCore.Application.Models.User.Command.CreateUser;
|
using AipsCore.Application.Models.User.Command.CreateUser;
|
||||||
|
using AipsCore.Application.Models.User.Query.GetUser;
|
||||||
using AipsCore.Domain.Common.Validation;
|
using AipsCore.Domain.Common.Validation;
|
||||||
using AipsCore.Domain.Models.User.ValueObjects;
|
using AipsCore.Domain.Models.User.ValueObjects;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -10,10 +11,25 @@ namespace AipsWebApi.Controllers;
|
|||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class UserController : ControllerBase
|
public class UserController : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpPost]
|
private readonly IDispatcher _dispatcher;
|
||||||
public async Task<ActionResult<int>> CreateUser(CreateUserCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
|
||||||
|
public UserController(IDispatcher dispatcher)
|
||||||
{
|
{
|
||||||
var userId = await dispatcher.Execute(command, cancellationToken);
|
_dispatcher = dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{userId}")]
|
||||||
|
public async Task<IActionResult> GetUser([FromRoute] string userId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var query = new GetUserQuery(userId);
|
||||||
|
var result = await _dispatcher.Execute(query, cancellationToken);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ActionResult<int>> CreateUser(CreateUserCommand command, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var userId = await _dispatcher.Execute(command, cancellationToken);
|
||||||
return Ok(userId.IdValue);
|
return Ok(userId.IdValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace AipsWebApi.Controllers;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("[controller]")]
|
|
||||||
public class WeatherForecastController : ControllerBase
|
|
||||||
{
|
|
||||||
private static readonly string[] Summaries =
|
|
||||||
[
|
|
||||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
||||||
];
|
|
||||||
|
|
||||||
[HttpGet(Name = "GetWeatherForecast")]
|
|
||||||
public IEnumerable<WeatherForecast> Get()
|
|
||||||
{
|
|
||||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
|
||||||
{
|
|
||||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
||||||
TemperatureC = Random.Shared.Next(-20, 55),
|
|
||||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
|
||||||
})
|
|
||||||
.ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,6 +4,8 @@ using AipsCore.Application.Models.Whiteboard.Command.BanUserFromWhiteboard;
|
|||||||
using AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard;
|
using AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard;
|
||||||
using AipsCore.Application.Models.Whiteboard.Command.KickUserFromWhiteboard;
|
using AipsCore.Application.Models.Whiteboard.Command.KickUserFromWhiteboard;
|
||||||
using AipsCore.Application.Models.Whiteboard.Command.UnbanUserFromWhiteboard;
|
using AipsCore.Application.Models.Whiteboard.Command.UnbanUserFromWhiteboard;
|
||||||
|
using AipsCore.Application.Models.Whiteboard.Query.GetRecentWhiteboards;
|
||||||
|
using AipsCore.Domain.Models.Whiteboard;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace AipsWebApi.Controllers;
|
namespace AipsWebApi.Controllers;
|
||||||
@@ -12,39 +14,54 @@ namespace AipsWebApi.Controllers;
|
|||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class WhiteboardController : ControllerBase
|
public class WhiteboardController : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpPost]
|
private readonly IDispatcher _dispatcher;
|
||||||
public async Task<ActionResult<int>> CreateWhiteboard(CreateWhiteboardCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
|
||||||
|
public WhiteboardController(IDispatcher dispatcher)
|
||||||
{
|
{
|
||||||
var whiteboardId = await dispatcher.Execute(command, cancellationToken);
|
_dispatcher = dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ActionResult<int>> CreateWhiteboard(CreateWhiteboardCommand command, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var whiteboardId = await _dispatcher.Execute(command, cancellationToken);
|
||||||
return Ok(whiteboardId.IdValue);
|
return Ok(whiteboardId.IdValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("adduser")]
|
[HttpPost("adduser")]
|
||||||
public async Task<IActionResult> AddUser(AddUserToWhiteboardCommand command, IDispatcher dispatcher,
|
public async Task<IActionResult> AddUser(AddUserToWhiteboardCommand command,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await dispatcher.Execute(command, cancellationToken);
|
await _dispatcher.Execute(command, cancellationToken);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("recent")]
|
||||||
|
public async Task<ActionResult<ICollection<Whiteboard>>> Recent(GetRecentWhiteboardsQuery query, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var result = await _dispatcher.Execute(query, cancellationToken);
|
||||||
|
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPut("banUser")]
|
[HttpPut("banUser")]
|
||||||
public async Task<ActionResult> BanUserFromWhiteboard(BanUserFromWhiteboardCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
public async Task<ActionResult> BanUserFromWhiteboard(BanUserFromWhiteboardCommand command, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await dispatcher.Execute(command, cancellationToken);
|
await _dispatcher.Execute(command, cancellationToken);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("unbanUser")]
|
[HttpPut("unbanUser")]
|
||||||
public async Task<ActionResult> UnbanUserFromWhiteboard(UnbanUserFromWhiteboardCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
public async Task<ActionResult> UnbanUserFromWhiteboard(UnbanUserFromWhiteboardCommand command, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await dispatcher.Execute(command, cancellationToken);
|
await _dispatcher.Execute(command, cancellationToken);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("kickUser")]
|
[HttpPut("kickUser")]
|
||||||
public async Task<ActionResult> KickUserFromWhiteboard(KickUserFromWhiteboardCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
public async Task<ActionResult> KickUserFromWhiteboard(KickUserFromWhiteboardCommand command, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await dispatcher.Execute(command, cancellationToken);
|
await _dispatcher.Execute(command, cancellationToken);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace AipsWebApi;
|
|
||||||
|
|
||||||
public class WeatherForecast
|
|
||||||
{
|
|
||||||
public DateOnly Date { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureC { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
|
|
||||||
public string? Summary { get; set; }
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user