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

Feature/delete question option endpoint #389

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace VoteMonitor.Api.Form.Commands
{
public class DeleteQuestionOptionCommand : IRequest<bool>
{
public int SectionId { get; set; }

public int QuestionId { get; set; }

public int OptionId { get; set; }
}
}
23 changes: 23 additions & 0 deletions src/api/VoteMonitor.Api.Form/Controllers/FormController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,28 @@ public async Task<IActionResult> DeleteQuestionAsync(int sectionId, int question

return Ok();
}

[HttpDelete("section/{sectionId}/question/{questionId}/option/{optionId}")]
[Authorize("Organizer")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteQuestionOptionAsync(int sectionId, int questionId, int optionId)
{

var deleted = await _mediator.Send(new DeleteQuestionOptionCommand()
{
QuestionId = questionId,
SectionId = sectionId,
OptionId = optionId
});

if (!deleted)
{
return BadRequest("The option could not be deleted. Make sure the option exists.");
}

return Ok();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using VoteMonitor.Api.Form.Commands;
using VoteMonitor.Entities;

namespace VoteMonitor.Api.Form.Handlers
{
public class DeleteQuestionOptionHandler : IRequestHandler<DeleteQuestionOptionCommand, bool>
{
private readonly VoteMonitorContext _context;
public DeleteQuestionOptionHandler(VoteMonitorContext context)
{
_context = context;
}

public async Task<bool> Handle(DeleteQuestionOptionCommand request, CancellationToken cancellationToken)
{
using (var transaction = await _context.Database.BeginTransactionAsync(cancellationToken))
{

var optionToBeRemoved = _context.Options.FirstOrDefault(o => o.Id == request.OptionId);

if (optionToBeRemoved == null)
{
return false;
}


if (await OptionToBeDeleted(request.OptionId))
{
if (await OptionHasAnswers(request.OptionId))
{
return false;
}

await DeleteQuestionsToOption(request.OptionId);
DeleteOption(optionToBeRemoved);

}
else
{
RemoveOptionFromQuestion(request.SectionId, request.QuestionId, request.OptionId);
}

await _context.SaveChangesAsync();
await transaction.CommitAsync(cancellationToken);

return true;
}
}

private void RemoveOptionFromQuestion(int sectionId, int questionId, int optionId)
{
var questionOptionLinkToBeRemoved = _context.Questions.Where(q => q.IdSection == sectionId && q.Id == questionId)
.SelectMany(q => q.OptionsToQuestions)
.Where(otq => otq.IdOption == optionId).FirstOrDefault();

_context.Remove(questionOptionLinkToBeRemoved);
}

private async Task<bool> OptionToBeDeleted(int optiondId)
{
var atMostOnOneQuestion = await _context.OptionsToQuestions
.Where(o => o.IdOption == optiondId).CountAsync() <= 1;

return atMostOnOneQuestion;

}


private async Task<bool> OptionHasAnswers(int optionId)
{
return await _context
.Answers.Where(a => a.OptionAnswered.IdOption == optionId).AnyAsync();
}

private async Task DeleteQuestionsToOption(int optionId)
{
var questionsToOptions = await _context.OptionsToQuestions.Where(otq => otq.IdOption == optionId).ToListAsync();

_context.RemoveRange(questionsToOptions);
}

private void DeleteOption(Option option)
{
_context.Remove(option);
}
}
}