Major update
This commit is contained in:
28
games/Handlers/AddGamesCommandHandler.cs
Normal file
28
games/Handlers/AddGamesCommandHandler.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Games.Commands;
|
||||
using Games.DbContexts;
|
||||
using Games.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace Games.Handlers
|
||||
{
|
||||
public class AddGamesCommandHandler : IRequestHandler<AddGamesCommand, Unit>
|
||||
{
|
||||
private readonly GamesContext _dbContext;
|
||||
|
||||
public AddGamesCommandHandler(GamesContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(AddGamesCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
await _dbContext.Games.AddRangeAsync(request.games, cancellationToken);
|
||||
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
26
games/Handlers/GetGameByIdQueryHandler.cs
Normal file
26
games/Handlers/GetGameByIdQueryHandler.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Games.DbContexts;
|
||||
using Games.Entities;
|
||||
using Games.Helpers;
|
||||
using Games.Queries;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Games.Handlers
|
||||
{
|
||||
public class GetGameByIdQueryHandler : IRequestHandler<GetGameByIdQuery, Game>
|
||||
{
|
||||
private readonly GamesContext _dbContext;
|
||||
|
||||
public GetGameByIdQueryHandler(GamesContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
}
|
||||
|
||||
public async Task<Game> Handle(GetGameByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _dbContext.Games.Where(g => g.GamePk == request.GameId).FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
28
games/Handlers/GetGamesQueryHandler.cs
Normal file
28
games/Handlers/GetGamesQueryHandler.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Games.DbContexts;
|
||||
using Games.Entities;
|
||||
using Games.Helpers;
|
||||
using Games.Queries;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Games.Handlers
|
||||
{
|
||||
public class GetGamesQueryHandler : IRequestHandler<GetGamesQuery, PagedList<Game>>
|
||||
{
|
||||
private readonly GamesContext _dbContext;
|
||||
|
||||
public GetGamesQueryHandler(GamesContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
}
|
||||
|
||||
public async Task<PagedList<Game>> Handle(GetGamesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var games = await _dbContext.Games.ToListAsync();
|
||||
|
||||
return PagedList<Game>.ToPagedList(await _dbContext.Games.ToListAsync(), request.PageNumber, request.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user