Skip to content

Commit

Permalink
Feature/form unique code (#383)
Browse files Browse the repository at this point in the history
* -fixes get all notification bug
-fixes swagger configuration issue where authorization header is not inserted correctly

* -revert

* -revert

* -revert

* -check if form code/id already exists on form post request

* - small edits

Co-authored-by: Irina Borozan <[email protected]>
  • Loading branch information
DSVDavid and aniri authored Oct 24, 2021
1 parent 7cba59f commit b0db3e4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/api/VoteMonitor.Api.Form/Controllers/FormController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ public FormController(IMediator mediator, IOptions<ApplicationCacheOptions> cach

[HttpPost]
[Authorize("NgoAdmin")]
public async Task<int> AddForm([FromBody] FormDTO newForm)
public async Task<ActionResult<int>> AddForm([FromBody] FormDTO newForm)
{
var formExists = await _mediator.Send(new ExistsFormByCodeOrIdQuery()
{
Id = newForm.Id,
Code = newForm.Code
});

if (formExists)
{
return BadRequest($"The form with the given code/id already exists");
}

var result = await _mediator.Send(new AddFormCommand { Form = newForm });
return result.Id;
}
Expand Down
10 changes: 10 additions & 0 deletions src/api/VoteMonitor.Api.Form/Queries/ExistsFormByCodeOrIdQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MediatR;

namespace VoteMonitor.Api.Form.Queries
{
public class ExistsFormByCodeOrIdQuery : IRequest<bool>
{
public int Id { get; set; }
public string Code { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;
using VoteMonitor.Api.Form.Queries;
using VoteMonitor.Entities;

namespace VoteMonitor.Api.Form.QueryHandlers
{
public class GetFormExistsByCodeOrIdQueryHandler : IRequestHandler<ExistsFormByCodeOrIdQuery, bool>
{
private readonly VoteMonitorContext _context;
public GetFormExistsByCodeOrIdQueryHandler(VoteMonitorContext voteMonitorContext)
{
_context = voteMonitorContext;
}

public async Task<bool> Handle(ExistsFormByCodeOrIdQuery request, CancellationToken cancellationToken)
{
return await _context.Forms.AnyAsync(form => form.Code == request.Code || form.Id == request.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace VoteMonitor.Api.Form.QueryHandlers
{
public class GetFormExistsByIdQueryHandler : IRequestHandler<GetFormExistsByIdQuery, bool>
{
private VoteMonitorContext _context;
private readonly VoteMonitorContext _context;

public GetFormExistsByIdQueryHandler(VoteMonitorContext voteMonitorContext)
{
Expand Down

0 comments on commit b0db3e4

Please sign in to comment.