From 175a8c7b52e8ebbb82136fcad81452d4cee920cf Mon Sep 17 00:00:00 2001 From: DSVDavid Date: Sun, 3 Oct 2021 14:33:56 +0300 Subject: [PATCH 1/4] -fixes get all notification bug -fixes swagger configuration issue where authorization header is not inserted correctly --- .../Controllers/NotificationController.cs | 4 ++-- .../Extensions/SwaggerConfiguration.cs | 23 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/api/VoteMonitor.Api.Notification/Controllers/NotificationController.cs b/src/api/VoteMonitor.Api.Notification/Controllers/NotificationController.cs index fea81cfe..ade4afa6 100644 --- a/src/api/VoteMonitor.Api.Notification/Controllers/NotificationController.cs +++ b/src/api/VoteMonitor.Api.Notification/Controllers/NotificationController.cs @@ -74,7 +74,7 @@ public async Task SendToAll([FromBody]NotificationForAllNewModel model) [HttpGet] [Authorize("NgoAdmin")] [Route("get/all")] - public async Task GetAll(PagingModel query) + public async Task GetAll([FromQuery]PagingModel query) { var idNgo = this.GetIdOngOrDefault(_configuration.GetValue("DefaultIdOng")); var organizer = this.GetOrganizatorOrDefault(false); @@ -92,4 +92,4 @@ public async Task GetAll(PagingModel query) return Ok(result); } } -} \ No newline at end of file +} diff --git a/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs b/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs index c954826c..17b43293 100644 --- a/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs +++ b/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs @@ -29,20 +29,21 @@ public static IServiceCollection ConfigureSwagger(this IServiceCollection servic }, }); - options.AddSecurityDefinition("bearer", new OpenApiSecurityScheme - { - Name = "Authorization", - In = ParameterLocation.Header, - Type = SecuritySchemeType.ApiKey - }); + var securitySchema= new OpenApiSecurityScheme{ + Name="Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.Http, + Scheme = "bearer", + Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "Bearer"} + }; + + options.AddSecurityDefinition("Bearer",securitySchema); + options.AddSecurityRequirement(new OpenApiSecurityRequirement { { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "bearer"} - }, - new[] {"readAccess", "writeAccess"} + securitySchema, + new[] {"Bearer","readAccess", "writeAccess"} } } From 8fdb464bcb7b6bddc71ee8dd45ad3bedb69b7397 Mon Sep 17 00:00:00 2001 From: DSVDavid Date: Tue, 5 Oct 2021 23:16:31 +0300 Subject: [PATCH 2/4] -revert --- .../Extensions/SwaggerConfiguration.cs | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs b/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs index 17b43293..5585e529 100644 --- a/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs +++ b/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs @@ -29,25 +29,23 @@ public static IServiceCollection ConfigureSwagger(this IServiceCollection servic }, }); - var securitySchema= new OpenApiSecurityScheme{ - Name="Authorization", - In = ParameterLocation.Header, - Type = SecuritySchemeType.Http, - Scheme = "bearer", - Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "Bearer"} - }; - - options.AddSecurityDefinition("Bearer",securitySchema); - + options.AddSecurityDefinition("bearer", new OpenApiSecurityScheme + { + Name = "Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey + }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { - securitySchema, - new[] {"Bearer","readAccess", "writeAccess"} + new OpenApiSecurityScheme + { + Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "bearer"} + }, + new[] {"readAccess", "writeAccess"} } } - ); //options.OperationFilter(); var baseDocPath = Directory.GetCurrentDirectory(); From 498dcef6e0c2a946c75b94a7b52c031e9e229923 Mon Sep 17 00:00:00 2001 From: DSVDavid Date: Sun, 16 Jan 2022 18:16:58 +0200 Subject: [PATCH 3/4] Delete endpoint for question options --- .../Commands/DeleteQuestionOptionCommand.cs | 17 ++++ .../Controllers/FormController.cs | 23 +++++ .../Handlers/DeleteQuestionOptionHandler.cs | 94 +++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/api/VoteMonitor.Api.Form/Commands/DeleteQuestionOptionCommand.cs create mode 100644 src/api/VoteMonitor.Api.Form/Handlers/DeleteQuestionOptionHandler.cs diff --git a/src/api/VoteMonitor.Api.Form/Commands/DeleteQuestionOptionCommand.cs b/src/api/VoteMonitor.Api.Form/Commands/DeleteQuestionOptionCommand.cs new file mode 100644 index 00000000..41180747 --- /dev/null +++ b/src/api/VoteMonitor.Api.Form/Commands/DeleteQuestionOptionCommand.cs @@ -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 + { + public int SectionId { get; set; } + + public int QuestionId { get; set; } + + public int OptionId { get; set; } + } +} diff --git a/src/api/VoteMonitor.Api.Form/Controllers/FormController.cs b/src/api/VoteMonitor.Api.Form/Controllers/FormController.cs index 67522ed8..0d063d99 100644 --- a/src/api/VoteMonitor.Api.Form/Controllers/FormController.cs +++ b/src/api/VoteMonitor.Api.Form/Controllers/FormController.cs @@ -174,5 +174,28 @@ public async Task 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 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(); + } + } } diff --git a/src/api/VoteMonitor.Api.Form/Handlers/DeleteQuestionOptionHandler.cs b/src/api/VoteMonitor.Api.Form/Handlers/DeleteQuestionOptionHandler.cs new file mode 100644 index 00000000..c0761b58 --- /dev/null +++ b/src/api/VoteMonitor.Api.Form/Handlers/DeleteQuestionOptionHandler.cs @@ -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 + { + private readonly VoteMonitorContext _context; + public DeleteQuestionOptionHandler(VoteMonitorContext context) + { + _context = context; + } + + public async Task 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 OptionToBeDeleted(int optiondId) + { + var atMostOnOneQuestion = await _context.OptionsToQuestions + .Where(o => o.IdOption == optiondId).CountAsync() <= 1; + + return atMostOnOneQuestion; + + } + + + private async Task 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); + } + } +} From a0372d1fe9daa55c25c262be56f5c61ce5eb4f0f Mon Sep 17 00:00:00 2001 From: DSVDavid Date: Sun, 16 Jan 2022 18:19:48 +0200 Subject: [PATCH 4/4] fix --- src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs b/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs index 5585e529..c954826c 100644 --- a/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs +++ b/src/api/VoteMonitor.Api/Extensions/SwaggerConfiguration.cs @@ -46,6 +46,7 @@ public static IServiceCollection ConfigureSwagger(this IServiceCollection servic } } + ); //options.OperationFilter(); var baseDocPath = Directory.GetCurrentDirectory();