Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup code to align with our code style #94

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions Src/DeUrgenta.Admin.Api/CommandHandlers/CreateEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Threading;
using System;
using System.Threading;
using System.Threading.Tasks;
using CSharpFunctionalExtensions;
using DeUrgenta.Admin.Api.Commands;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Events;
using DeUrgenta.Common.Validation;
using DeUrgenta.Domain;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace DeUrgenta.Admin.Api.CommandHandlers
{
public class CreateEventHandler : IRequestHandler<CreateEvent, Result<EventModel>>
public class CreateEventHandler : IRequestHandler<CreateEvent, Result<EventResponseModel>>
{
private readonly IValidateRequest<CreateEvent> _validator;
private readonly DeUrgentaContext _context;
Expand All @@ -20,40 +22,45 @@ public CreateEventHandler(IValidateRequest<CreateEvent> validator, DeUrgentaCont
_context = context;
}

public async Task<Result<EventModel>> Handle(CreateEvent request, CancellationToken cancellationToken)
public async Task<Result<EventResponseModel>> Handle(CreateEvent request, CancellationToken cancellationToken)
{
var isValid = await _validator.IsValidAsync(request);
if (!isValid)
{
return Result.Failure<EventModel>("Validation failed");
return Result.Failure<EventResponseModel>("Validation failed");
}
var eventType = await _context.EventTypes.FirstAsync(et => et.Id == request.Event.EventTypeId, cancellationToken);

var ev = new Domain.Entities.Event
{
var @event = new Domain.Entities.Event
{
Title = request.Event.Title,
Author = request.Event.Author,
City = request.Event.City,
ContentBody = request.Event.ContentBody,
EventTypeId = request.Event.EventTypeId,
EventType = eventType,
OccursOn = request.Event.OccursOn,
OrganizedBy = request.Event.OrganizedBy,
Address = request.Event.Address,
IsArchived = request.Event.IsArchived,
PublishedOn = DateTime.UtcNow
};

await _context.Events.AddAsync(ev, cancellationToken);
await _context.Events.AddAsync(@event, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);

return new EventModel
return new EventResponseModel
{
Id = ev.Id,
Title = ev.Title,
Address = ev.Address,
Author = ev.Author,
City = ev.City,
ContentBody = ev.ContentBody,
EventTypeId = ev.EventTypeId,
OccursOn = ev.OccursOn,
OrganizedBy = ev.OrganizedBy
Id = @event.Id,
Title = @event.Title,
Address = @event.Address,
Author = @event.Author,
City = @event.City,
IsArchived = @event.IsArchived,
ContentBody = @event.ContentBody,
EventType = eventType.Name,
OccursOn = @event.OccursOn,
OrganizedBy = @event.OrganizedBy,
PublishedOn = @event.PublishedOn
};
}
}
Expand Down
53 changes: 30 additions & 23 deletions Src/DeUrgenta.Admin.Api/CommandHandlers/UpdateEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using System.Threading.Tasks;
using CSharpFunctionalExtensions;
using DeUrgenta.Admin.Api.Commands;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Events;
using DeUrgenta.Common.Validation;
using DeUrgenta.Domain;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace DeUrgenta.Admin.Api.CommandHandlers
{
public class UpdateEventHandler : IRequestHandler<UpdateEvent, Result<EventModel>>
public class UpdateEventHandler : IRequestHandler<UpdateEvent, Result<EventResponseModel>>
{
private readonly IValidateRequest<UpdateEvent> _validator;
private readonly DeUrgentaContext _context;
Expand All @@ -22,36 +22,43 @@ public UpdateEventHandler(IValidateRequest<UpdateEvent> validator, DeUrgentaCont
_context = context;
}

public async Task<Result<EventModel>> Handle(UpdateEvent request, CancellationToken cancellationToken)
public async Task<Result<EventResponseModel>> Handle(UpdateEvent request, CancellationToken cancellationToken)
{
var isValid = await _validator.IsValidAsync(request);
if (!isValid)
{
return Result.Failure<EventModel>("Validation failed");
return Result.Failure<EventResponseModel>("Validation failed");
}
var ev = await _context.Events.FirstAsync(b => b.Id == request.EventId, cancellationToken);
ev.Address = request.Event.Address;
ev.Author = request.Event.Author;
ev.City = request.Event.City;
ev.ContentBody = request.Event.ContentBody;
ev.EventTypeId = request.Event.EventTypeId;
ev.OccursOn = request.Event.OccursOn;
ev.OrganizedBy = request.Event.OrganizedBy;
ev.Title = request.Event.Title;

var eventType = await _context.EventTypes.FirstAsync(et => et.Id == request.Event.EventTypeId, cancellationToken);
var @event = await _context.Events.FirstAsync(e => e.Id == request.EventId, cancellationToken);

@event.Address = request.Event.Address;
@event.Author = request.Event.Author;
@event.City = request.Event.City;
@event.ContentBody = request.Event.ContentBody;
@event.EventType = eventType;
@event.OccursOn = request.Event.OccursOn;
@event.OrganizedBy = request.Event.OrganizedBy;
@event.Title = request.Event.Title;
@event.IsArchived = request.Event.IsArchived;
@event.PublishedOn = DateTime.UtcNow;

await _context.SaveChangesAsync(cancellationToken);

return new EventModel
return new EventResponseModel
{
Id = ev.Id,
Title = ev.Title,
Address = ev.Address,
Author = ev.Author,
City = ev.City,
ContentBody = ev.ContentBody,
EventTypeId = ev.EventTypeId,
OccursOn = ev.OccursOn,
OrganizedBy = ev.OrganizedBy
Id = @event.Id,
Title = @event.Title,
Address = @event.Address,
Author = @event.Author,
City = @event.City,
ContentBody = @event.ContentBody,
EventType = eventType.Name,
OccursOn = @event.OccursOn,
OrganizedBy = @event.OrganizedBy,
IsArchived = @event.IsArchived,
PublishedOn = @event.PublishedOn
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions Src/DeUrgenta.Admin.Api/Commands/CreateEvent.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using CSharpFunctionalExtensions;
using DeUrgenta.Admin.Api.Models;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Events;
using MediatR;

namespace DeUrgenta.Admin.Api.Commands
{
public class CreateEvent : IRequest<Result<EventModel>>
public class CreateEvent : IRequest<Result<EventResponseModel>>
{
public EventRequest Event { get; }

Expand Down
4 changes: 2 additions & 2 deletions Src/DeUrgenta.Admin.Api/Commands/UpdateEvent.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using CSharpFunctionalExtensions;
using DeUrgenta.Admin.Api.Models;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Events;
using MediatR;

namespace DeUrgenta.Admin.Api.Commands
{
public class UpdateEvent : IRequest<Result<EventModel>>
public class UpdateEvent : IRequest<Result<EventResponseModel>>
{
public Guid EventId { get; }
public EventRequest Event { get; }
Expand Down
7 changes: 3 additions & 4 deletions Src/DeUrgenta.Admin.Api/Controller/AdminBlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading.Tasks;
using DeUrgenta.Admin.Api.Models;
using DeUrgenta.Admin.Api.Swagger.Blog;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Pagination;
using DeUrgenta.Common.Swagger;
using MediatR;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -14,10 +14,10 @@
namespace DeUrgenta.Admin.Api.Controller
{
[ApiController]
[Authorize]
[Produces("application/json")]
[Consumes("application/json")]
[Route("blog")]
[Authorize]
[Route("admin/blog")]
public class AdminBlogController : ControllerBase
{
private readonly IMediator _mediator;
Expand All @@ -32,7 +32,6 @@ public AdminBlogController(IMediator mediator)
/// </summary>
/// <returns></returns>
[HttpGet("posts")]
[AllowAnonymous]
[SwaggerResponse(StatusCodes.Status200OK, "Blog posts", typeof(PagedResult<BlogPostModel>))]
[SwaggerResponse(StatusCodes.Status500InternalServerError, "Something bad happened", typeof(ProblemDetails))]

Expand Down
26 changes: 13 additions & 13 deletions Src/DeUrgenta.Admin.Api/Controller/AdminEventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using DeUrgenta.Admin.Api.Models;
using DeUrgenta.Admin.Api.Queries;
using DeUrgenta.Admin.Api.Swagger.Events;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Events;
using DeUrgenta.Common.Models.Pagination;
using DeUrgenta.Common.Swagger;
using MediatR;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -16,9 +17,10 @@
namespace DeUrgenta.Admin.Api.Controller
{
[ApiController]
[Authorize]
[Produces("application/json")]
[Consumes("application/json")]
[Route("event")]
[Route("admin/event")]
public class AdminEventsController : ControllerBase
{
private readonly IMediator _mediator;
Expand All @@ -29,20 +31,18 @@ public AdminEventsController(IMediator mediator)
}

/// <summary>
/// Gets upcoming events
/// Get all events events
/// </summary>
/// <returns></returns>
[HttpGet("get")]
[AllowAnonymous]

[SwaggerResponse(StatusCodes.Status200OK, "Upcoming events", typeof(PagedResult<EventModel>))]
[HttpGet("/admin/events")]
[SwaggerResponse(StatusCodes.Status200OK, "Events", typeof(PagedResult<EventResponseModel>))]
[SwaggerResponse(StatusCodes.Status500InternalServerError, "Something bad happened", typeof(ProblemDetails))]

[SwaggerResponseExample(StatusCodes.Status200OK, typeof(GetEventsResponseExample))]
[SwaggerResponseExample(StatusCodes.Status500InternalServerError, typeof(ApplicationErrorResponseExample))]
public async Task<ActionResult<PagedResult<EventModel>>> GetEventsAsync([FromQuery] PaginationQueryModel pagination)
public async Task<ActionResult<PagedResult<EventResponseModel>>> GetEventsAsync([FromQuery] PaginationQueryModel pagination)
{
var query = new GetEvents();
var query = new GetEvents(pagination);
var result = await _mediator.Send(query);

if (result.IsFailure)
Expand All @@ -58,15 +58,15 @@ public async Task<ActionResult<PagedResult<EventModel>>> GetEventsAsync([FromQue
/// </summary>
/// <returns></returns>
[HttpPost]
[SwaggerResponse(StatusCodes.Status200OK, "New event", typeof(EventModel))]
[SwaggerResponse(StatusCodes.Status200OK, "New event", typeof(EventResponseModel))]
[SwaggerResponse(StatusCodes.Status400BadRequest, "A business rule was violated", typeof(ProblemDetails))]
[SwaggerResponse(StatusCodes.Status500InternalServerError, "Something bad happened", typeof(ProblemDetails))]

[SwaggerRequestExample(typeof(EventRequest), typeof(AddOrUpdateEventRequestExample))]
[SwaggerResponseExample(StatusCodes.Status200OK, typeof(AddOrUpdateEventResponseExample))]
[SwaggerResponseExample(StatusCodes.Status400BadRequest, typeof(BusinessRuleViolationResponseExample))]
[SwaggerResponseExample(StatusCodes.Status500InternalServerError, typeof(ApplicationErrorResponseExample))]
public async Task<ActionResult<EventModel>> CreateNewEventAsync([FromBody] EventRequest eventModel)
public async Task<ActionResult<EventResponseModel>> CreateNewEventAsync([FromBody] EventRequest eventModel)
{
var command = new CreateEvent(eventModel);
var result = await _mediator.Send(command);
Expand All @@ -85,15 +85,15 @@ public async Task<ActionResult<EventModel>> CreateNewEventAsync([FromBody] Event
[HttpPut]
[Route("{eventId:guid}")]

[SwaggerResponse(StatusCodes.Status200OK, "Updated event", typeof(EventModel))]
[SwaggerResponse(StatusCodes.Status200OK, "Updated event", typeof(EventResponseModel))]
[SwaggerResponse(StatusCodes.Status400BadRequest, "A business rule was violated", typeof(ProblemDetails))]
[SwaggerResponse(StatusCodes.Status500InternalServerError, "Something bad happened", typeof(ProblemDetails))]

[SwaggerRequestExample(typeof(EventRequest), typeof(AddOrUpdateEventRequestExample))]
[SwaggerResponseExample(StatusCodes.Status200OK, typeof(AddOrUpdateEventResponseExample))]
[SwaggerResponseExample(StatusCodes.Status400BadRequest, typeof(BusinessRuleViolationResponseExample))]
[SwaggerResponseExample(StatusCodes.Status500InternalServerError, typeof(ApplicationErrorResponseExample))]
public async Task<ActionResult<EventModel>> UpdateEventAsync([FromRoute] Guid eventId, [FromBody] EventRequest eventModel)
public async Task<ActionResult<EventResponseModel>> UpdateEventAsync([FromRoute] Guid eventId, [FromBody] EventRequest eventModel)
{
var command = new UpdateEvent(eventId, eventModel);
var result = await _mediator.Send(command);
Expand Down
3 changes: 3 additions & 0 deletions Src/DeUrgenta.Admin.Api/Models/EventRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public sealed record EventRequest
public string City { get; init; }

public int EventTypeId { get; init; }

public string Address { get; init; }

public bool IsArchived { get; init; }
}
}
14 changes: 10 additions & 4 deletions Src/DeUrgenta.Admin.Api/Queries/GetEvents.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System.Collections.Immutable;
using CSharpFunctionalExtensions;
using CSharpFunctionalExtensions;
using MediatR;
using DeUrgenta.Common.Models;
using DeUrgenta.Common.Models.Events;
using DeUrgenta.Common.Models.Pagination;

namespace DeUrgenta.Admin.Api.Queries
{
public class GetEvents : IRequest<Result<IImmutableList<EventModel>>>
public class GetEvents : IRequest<Result<PagedResult<EventResponseModel>>>
{
public PaginationQueryModel Pagination { get; }

public GetEvents(PaginationQueryModel pagination)
{
Pagination = pagination;
}
}
}
Loading