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

Update county related endpoints #391

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions src/api/VoteMonitor.Api.County/Commands/CreateCounty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CSharpFunctionalExtensions;
using MediatR;
using VoteMonitor.Api.County.Models;

namespace VoteMonitor.Api.County.Commands
{
public class CreateCounty : IRequest<Result>
{
public CountyModel County { get; }

public CreateCounty(AddOrUpdateCountyRequest county)
{
County = new CountyModel
{
Name = county.Name,
Code = county.Code,
Diaspora = county.Diaspora,
Order = county.Order,
NumberOfPollingStations = county.NumberOfPollingStations
};
}
}
}
15 changes: 15 additions & 0 deletions src/api/VoteMonitor.Api.County/Commands/DeleteCounty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CSharpFunctionalExtensions;
using MediatR;

namespace VoteMonitor.Api.County.Commands
{
public class DeleteCounty : IRequest<Result>
{
public int CountyId { get; }

public DeleteCounty(int countyId)
{
CountyId = countyId;
}
}
}
6 changes: 3 additions & 3 deletions src/api/VoteMonitor.Api.County/Commands/UpdateCounty.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CSharpFunctionalExtensions;
using CSharpFunctionalExtensions;
using MediatR;
using VoteMonitor.Api.County.Models;

Expand All @@ -8,7 +8,7 @@ public class UpdateCounty : IRequest<Result>
{
public CountyModel County { get; }

public UpdateCounty(int countyId, UpdateCountyRequest county)
public UpdateCounty(int countyId, AddOrUpdateCountyRequest county)
{
County = new CountyModel
{
Expand All @@ -21,4 +21,4 @@ public UpdateCounty(int countyId, UpdateCountyRequest county)
};
}
}
}
}
41 changes: 37 additions & 4 deletions src/api/VoteMonitor.Api.County/Controllers/CountyController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.IO;
Expand Down Expand Up @@ -101,12 +101,12 @@ public async Task<IActionResult> GetCountyAsync(int countyId)
return BadRequest(new ErrorModel { Message = response.Error });
}

[HttpPost("{countyId}")]
[Authorize("NgoAdmin")]
[HttpPut("{countyId}")]
[Authorize("Organizer")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> UpdateCountyAsync(int countyId, [FromBody] UpdateCountyRequest county)
public async Task<IActionResult> UpdateCountyAsync(int countyId, [FromBody] AddOrUpdateCountyRequest county)
{
var response = await _mediator.Send(new UpdateCounty(countyId, county));
if (response.IsSuccess)
Expand All @@ -116,5 +116,38 @@ public async Task<IActionResult> UpdateCountyAsync(int countyId, [FromBody] Upda

return BadRequest(new ErrorModel { Message = response.Error });
}

[HttpPost]
[Authorize("Organizer")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> CreateCountyAsync([FromBody] AddOrUpdateCountyRequest county)
{
var response = await _mediator.Send(new CreateCounty(county));
if (response.IsSuccess)
{
return Ok();
}

return BadRequest(new ErrorModel { Message = response.Error });
}


[HttpDelete("{countyId}")]
[Authorize("Organizer")]
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> DeleteCountyAsync([FromRoute]int countyId)
{
var response = await _mediator.Send(new DeleteCounty(countyId));
if (response.IsSuccess)
{
return NoContent();
}

return BadRequest(new ErrorModel { Message = response.Error });
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -22,7 +22,9 @@ public class CountiesCommandHandler : IRequestHandler<GetCountiesForExport, Resu
IRequestHandler<CreateOrUpdateCounties, Result>,
IRequestHandler<GetAllCounties, Result<List<CountyModel>>>,
IRequestHandler<GetCounty, Result<CountyModel>>,
IRequestHandler<UpdateCounty, Result>
IRequestHandler<UpdateCounty, Result>,
IRequestHandler<CreateCounty, Result>,
IRequestHandler<DeleteCounty, Result>

{
private readonly VoteMonitorContext _context;
Expand Down Expand Up @@ -225,5 +227,65 @@ public async Task<Result> Handle(UpdateCounty request, CancellationToken cancell
return Result.Failure($"Unable to update county {request.County.Id}");
}
}

public async Task<Result> Handle(CreateCounty request, CancellationToken cancellationToken)
{
try
{
var county = new Entities.County();
if (await _context.Counties.AnyAsync(cancellationToken))
{
var maxCountyId = await _context.Counties.MaxAsync(x => x.Id, cancellationToken);
county.Id = maxCountyId + 1;
}
else
{
county.Id = 1;
}

county.Code = request.County.Code;
county.Name = request.County.Name;
county.NumberOfPollingStations = request.County.NumberOfPollingStations;
county.Diaspora = request.County.Diaspora;
county.Order = request.County.Order;
_context.Counties.Add(county);

await _context.SaveChangesAsync(cancellationToken);

return Result.Success();
}
catch (Exception e)
{
_logger.LogError($"Unable to add county {request.County.Id}", e);
return Result.Failure($"Unable to add county {request.County.Id}");
}
}

public async Task<Result> Handle(DeleteCounty request, CancellationToken cancellationToken)
{
try
{
var county = await _context.Counties.FirstOrDefaultAsync(x => x.Id == request.CountyId, cancellationToken);
if (county == null)
{
return Result.Failure($"Could not find county with id = {request.CountyId}");
}

if (await _context.PollingStations.AnyAsync(ps => ps.IdCounty == request.CountyId, cancellationToken))
{
return Result.Failure($"Could not delete county. There are polling stations assigned to it.");
}

_context.Counties.Remove(county);
await _context.SaveChangesAsync(cancellationToken);

return Result.Success();
}
catch (Exception e)
{
_logger.LogError($"Unable to delete county {request.CountyId}", e);
return Result.Failure($"Unable to delete county {request.CountyId}");
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;

namespace VoteMonitor.Api.County.Models
{
public class UpdateCountyRequest
public class AddOrUpdateCountyRequest
{
[Required]
[StringLength(100)]
Expand All @@ -16,4 +16,4 @@ public class UpdateCountyRequest
public bool Diaspora { get; set; }
public int Order { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public async Task When_updating_county_by_nonexistent_id()
{
var countiesCommandHandler = new CountiesCommandHandler(context, _fakeLogger.Object, _mapper);
var county =
await countiesCommandHandler.Handle(new UpdateCounty(588, new UpdateCountyRequest()), new CancellationToken(false));
await countiesCommandHandler.Handle(new UpdateCounty(588, new AddOrUpdateCountyRequest()), new CancellationToken(false));

county.IsFailure.ShouldBeTrue();
county.Error.ShouldBe("Could not find county with id = 588");
Expand All @@ -498,7 +498,7 @@ public async Task When_updating_county_by_existent_id()
using (var context = new VoteMonitorContext(_dbContextOptions))
{
var countiesCommandHandler = new CountiesCommandHandler(context, _fakeLogger.Object, _mapper);
var updateCountyModel = new UpdateCountyRequest(){
var updateCountyModel = new AddOrUpdateCountyRequest(){
Name = "Super Iasi",
Code = "IS",
Order = 33,
Expand Down