Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 70114d7

Browse files
committed
feat(enrolling): use mediatR to handle query
This clean-up the controller
1 parent ef5cb0a commit 70114d7

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using MediatR;
5+
using Microsoft.EntityFrameworkCore;
6+
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
7+
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
8+
9+
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Queries
10+
{
11+
public class FindAllEnrollmentsHandler
12+
: IRequestHandler<FindAllEnrollmentsQuery, IEnumerable<Enrollment>>
13+
{
14+
private readonly EnrollingContext _context;
15+
16+
public FindAllEnrollmentsHandler(EnrollingContext context)
17+
{
18+
_context = context ?? throw new System.ArgumentNullException(nameof(context));
19+
}
20+
21+
public async Task<IEnumerable<Enrollment>> Handle(FindAllEnrollmentsQuery request, CancellationToken cancellationToken)
22+
{
23+
return await _context.Enrollments.ToListAsync();
24+
}
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
using MediatR;
3+
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
4+
5+
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Queries
6+
{
7+
public class FindAllEnrollmentsQuery
8+
: IRequest<IEnumerable<Enrollment>>
9+
{
10+
}
11+
}

src/Services/Enrolling/Enrolling.API/Controllers/EnrollmentsController.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,30 @@
22
using System.Threading.Tasks;
33
using MediatR;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.Extensions.Logging;
75
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Commands;
6+
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Queries;
87
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
9-
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
108

119
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Controllers
1210
{
1311
[ApiController]
1412
[Route("[controller]")]
1513
public class EnrollmentsController : ControllerBase
1614
{
17-
private readonly ILogger<EnrollmentsController> _logger;
1815
private readonly IMediator _mediator;
19-
private readonly EnrollingContext _context;
2016

21-
public EnrollmentsController(
22-
ILogger<EnrollmentsController> logger,
23-
IMediator mediator,
24-
EnrollingContext context)
17+
public EnrollmentsController(IMediator mediator)
2518
{
26-
_logger = logger;
2719
_mediator = mediator;
28-
_context = context;
2920
}
3021

3122
[HttpGet]
32-
public async Task<List<Enrollment>> Get()
33-
{
34-
_logger.LogInformation("Getting all Enrollments");
35-
var enrollments = await _context.Enrollments.ToListAsync();
36-
37-
_logger.LogInformation("Total {NumberOfEnrollment} enrollments retrived", enrollments.Count);
38-
return enrollments;
39-
}
23+
public async Task<IEnumerable<Enrollment>> Get()
24+
=> await _mediator.Send(new FindAllEnrollmentsQuery());
4025

4126
[HttpPost]
4227
public async Task<IActionResult> Post([FromBody] EnrollmentApplicationCommand command)
4328
{
44-
_logger.LogInformation(
45-
"Sending command: {CommandName} - ({@Command})",
46-
command.GetType().Name,
47-
command);
48-
4929
await _mediator.Send(command);
5030
return Ok();
5131
}

0 commit comments

Comments
 (0)