From d3b014c5661cb57536cfb85f05e97b621e048c2f Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 17 Jul 2021 20:21:11 +0300 Subject: [PATCH] Add Feedback: Infer FeedbackReceiverId from the generated Token. --- .../TestFeedbackReceiversController.cs | 2 +- .../TestFeedbackReceiverController.cs | 8 ++--- .../FeedbackReceiversController.cs | 29 ++++++++++--------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs index bc2aed3..ea0f32c 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs @@ -428,7 +428,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers Rating = 4 }; var content = JsonSerializer.Serialize(feedback); - var response = await _client.PostAsync($"api/v1/feedback_receivers/{feedbackReceiver.Id}/feedbacks", + var response = await _client.PostAsync($"api/v1/feedback_receivers/feedbacks", new StringContent(content, Encoding.UTF8, "application/json")); // Assert diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs index cfe8886..0dc176f 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs @@ -219,7 +219,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers var controller = new FeedbackReceiversController(feedbackReceiversService.Object, tokensService.Object, feedbacksService.Object, mapper, null, logger.Object); - var result = await controller.AddFeedback("guid-test", requestBody); + var result = await controller.AddFeedback(requestBody); // Assert Assert.IsType(result); @@ -243,7 +243,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers var controller = new FeedbackReceiversController(feedbackReceiversService.Object, tokensService.Object, feedbacksService.Object, mapper, null, logger.Object); - var result = await controller.AddFeedback("guid-test", requestBody); + var result = await controller.AddFeedback(requestBody); // Assert Assert.IsType(result); @@ -277,7 +277,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers var controller = new FeedbackReceiversController(feedbackReceiversService.Object, tokensService.Object, feedbacksService.Object, mapper, null, logger.Object); - var result = await controller.AddFeedback("guid-test", requestBody); + var result = await controller.AddFeedback(requestBody); // Assert Assert.IsType(result); @@ -314,7 +314,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers // Test var controller = new FeedbackReceiversController(feedbackReceiversService.Object, tokensService.Object, feedbacksService.Object, mapper, null, logger.Object); - var result = await controller.AddFeedback("guid-test", requestBody); + var result = await controller.AddFeedback(requestBody); // Assert Assert.IsType(result); diff --git a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs index 4f694fe..d6d1672 100644 --- a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs +++ b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs @@ -172,31 +172,21 @@ namespace Retroactiune.Controllers /// /// Add Feedback to a FeedbackReceiver. /// - /// The guid of the FeedbackReceiver to add feedback. /// The feedback dto. /// The feedback has been added. /// The request is invalid. /// - [HttpPost("{guid}/feedbacks")] + [HttpPost("feedbacks")] [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)] - public async Task AddFeedback(string guid, [FromBody] FeedbackInDto feedbackInDto) + public async Task AddFeedback([FromBody] FeedbackInDto feedbackInDto) { - // TODO: Remove guid from request and infer it from the given token. - var receivers = await _feedbackReceiversService.FindAsync(new[] {guid}, limit: 1); var tokenEnum = await _tokensService.FindAsync(new TokenListFilters { Ids = new[] {feedbackInDto.TokenId} }); - var feedbackReceivers = receivers as FeedbackReceiver[] ?? receivers.ToArray(); var tokens = (tokenEnum as Token[] ?? tokenEnum.ToArray()); - if (!feedbackReceivers.Any()) - { - return BadRequest(new BasicResponse - { - Message = $"FeedbackReceiver with id {guid} not found." - }); - } + if (tokens.Length == 0) { @@ -207,6 +197,19 @@ namespace Retroactiune.Controllers } var token = tokens[0]; + + var receivers = await _feedbackReceiversService.FindAsync( + new[] {token.FeedbackReceiverId}, limit: 1 + ); + var feedbackReceivers = receivers as FeedbackReceiver[] ?? receivers.ToArray(); + if (!feedbackReceivers.Any()) + { + return BadRequest(new BasicResponse + { + Message = $"FeedbackReceiver with id {token.FeedbackReceiverId} not found." + }); + } + if (!token.IsValid(feedbackReceivers[0])) { return BadRequest(new BasicResponse