-
Notifications
You must be signed in to change notification settings - Fork 40
WIP: Feature course registration #364
base: main
Are you sure you want to change the base?
Changes from all commits
a4e82df
53c7176
155d120
940fc6b
16a1b49
eaf4ba2
b891d74
d0ce04f
69e6248
2c7c8cd
f9b0673
b326a7a
5f8de23
233a1e7
ecd5754
ba4acd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CourseRegistration.API.Application.Behaviors | ||
ratanparai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
{ | ||
|
||
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger; | ||
|
||
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger) | ||
{ | ||
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | ||
{ | ||
_logger.LogInformation("Handling request {RequestName} ({@Request})", request.GetType().Name, request); | ||
|
||
var response = await next(); | ||
|
||
_logger.LogInformation( | ||
"Request {RequestName} handled. Response: {@Response}", | ||
request.GetType().Name, | ||
response); | ||
|
||
return response; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using MediatR; | ||
|
||
namespace CourseRegistration.API.Application.Commands | ||
ratanparai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public class CourseRegistrationCommand : IRequest<bool> | ||
{ | ||
public string CourseCode { get; set; } | ||
public string CourseName { get; set; } | ||
public string Description { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CourseRegistration.Infrastructure; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CourseRegistration.API.Application.Commands | ||
{ | ||
public class CourseRegistrationCommandHandler : IRequestHandler<CourseRegistrationCommand, bool> | ||
{ | ||
private readonly ILogger<CourseRegistrationCommandHandler> _logger; | ||
private readonly CourseRegistrationContext _context; | ||
|
||
public CourseRegistrationCommandHandler(CourseRegistrationContext context, | ||
ILogger<CourseRegistrationCommandHandler> logger) | ||
{ | ||
_context = context ?? throw new ArgumentException(nameof(context)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task<bool> Handle(CourseRegistrationCommand command, CancellationToken cancellationToken) | ||
{ | ||
var courseRegistration = new CourseRegistration.Domain.AggregatesModel.CourseRegistrationAggregate.CourseRegistration(command.CourseCode, command.CourseName, command.Description); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do one of choosing different aggregate root name or changing the service name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is your suggestion for the name? We should follow a common convention. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mahedee: We have decided use suffix |
||
_context.CourseRegistrations.Add(courseRegistration); | ||
await _context.SaveChangesAsync(); | ||
return true; | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CourseRegistration.Infrastructure; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace CourseRegistration.API.Application.Queries | ||
{ | ||
public class FindAllCourseRegistrationHandler | ||
: IRequestHandler<FindAllCourseRegistrationQuery, IEnumerable<CourseRegistration.Domain.AggregatesModel.CourseRegistrationAggregate.CourseRegistration>> | ||
{ | ||
private readonly CourseRegistrationContext _context; | ||
|
||
|
||
public FindAllCourseRegistrationHandler(CourseRegistrationContext context) | ||
{ | ||
_context = context ?? throw new System.ArgumentNullException(nameof(context)); | ||
} | ||
|
||
|
||
public async Task<IEnumerable<Domain.AggregatesModel.CourseRegistrationAggregate.CourseRegistration>> Handle(FindAllCourseRegistrationQuery request, CancellationToken cancellationToken) | ||
{ | ||
return await _context.CourseRegistrations.ToListAsync(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
|
||
namespace CourseRegistration.API.Application.Queries | ||
{ | ||
public class FindAllCourseRegistrationQuery | ||
:IRequest<IEnumerable<CourseRegistration.Domain.AggregatesModel.CourseRegistrationAggregate.CourseRegistration>> | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CourseRegistration.API.Application.Commands; | ||
using CourseRegistration.API.Application.Queries; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace CourseRegistration.API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class CourseRegistrationController : Controller | ||
{ | ||
private readonly IMediator _mediator; | ||
|
||
public CourseRegistrationController(IMediator mediator) | ||
{ | ||
_mediator = mediator; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IEnumerable<CourseRegistration.Domain.AggregatesModel.CourseRegistrationAggregate.CourseRegistration>> Get() | ||
=> await _mediator.Send(new FindAllCourseRegistrationQuery()); | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Post([FromBody] CourseRegistrationCommand command) | ||
{ | ||
await _mediator.Send(command); | ||
return Ok(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CourseRegistration.API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
var rng = new Random(); | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = rng.Next(-20, 55), | ||
Summary = Summaries[rng.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.