From 24881ae1db6a180ba8a4d1be6929bb304f53376d Mon Sep 17 00:00:00 2001 From: prtsie Date: Wed, 4 Sep 2024 15:59:36 +0300 Subject: [PATCH] Fixed errors and wrong model --- .../VisaApplicationRequestsHandler.cs | 6 +++- .../VisaApplicationCreateRequestValidator.cs | 11 +++--- .../Requests/VisaApplicationCreateRequest.cs | 2 -- .../Controllers/UsersController.cs | 2 -- .../Controllers/VisaApplicationController.cs | 1 + SchengenVisaApi/VisaApiClient/Client.cs | 35 +++++++------------ 6 files changed, 24 insertions(+), 33 deletions(-) diff --git a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs index 23d93b6..4ece238 100644 --- a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs +++ b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs @@ -53,6 +53,10 @@ public class VisaApplicationRequestsHandler( { var applicantId = await applicants.GetApplicantIdByUserId(userIdProvider.GetUserId(), cancellationToken); var application = await applications.GetByApplicantAndApplicationIdAsync(applicantId, applicationId, cancellationToken); + if (application.Status is ApplicationStatus.Approved or ApplicationStatus.Rejected) + { + throw new ApplicationAlreadyProcessedException(); + } application.Status = ApplicationStatus.Closed; await applications.UpdateAsync(application, cancellationToken); @@ -95,4 +99,4 @@ public class VisaApplicationRequestsHandler( return model; } -} \ No newline at end of file +} diff --git a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/Validation/VisaApplicationCreateRequestValidator.cs b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/Validation/VisaApplicationCreateRequestValidator.cs index 84e8bc6..c0cb9d0 100644 --- a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/Validation/VisaApplicationCreateRequestValidator.cs +++ b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/Validation/VisaApplicationCreateRequestValidator.cs @@ -17,6 +17,12 @@ public class VisaApplicationCreateRequestValidator : AbstractValidator r.PermissionToDestCountry) + .NotEmpty() + .WithMessage("For transit you must provide permission to destination country") + .SetValidator(permissionToDestCountryModelValidator) + .When(r => r.VisaCategory is VisaCategory.Transit); + RuleFor(r => r.ReentryPermit) .NotEmpty() .WithMessage("Non-residents must provide re-entry permission") @@ -43,11 +49,6 @@ public class VisaApplicationCreateRequestValidator : AbstractValidator r.PastVisas) .SetValidator(pastVisaModelValidator); - When(r => r.VisaCategory == VisaCategory.Transit, - () => - RuleFor(r => r.PermissionToDestCountry) - .SetValidator(permissionToDestCountryModelValidator)); - RuleForEach(r => r.PastVisits) .SetValidator(pastVisitModelValidator); } diff --git a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/VisaApplicationCreateRequest.cs b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/VisaApplicationCreateRequest.cs index a617022..16be84b 100644 --- a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/VisaApplicationCreateRequest.cs +++ b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Requests/VisaApplicationCreateRequest.cs @@ -32,8 +32,6 @@ public class VisaApplicationCreateRequest [Required] public PastVisaModel[] PastVisas { get; set; } = null!; - //todo remove attribute - [Required] public PermissionToDestCountryModel? PermissionToDestCountry { get; set; } [Required] diff --git a/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs b/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs index 4522cfc..aca41d0 100644 --- a/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs +++ b/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs @@ -26,7 +26,6 @@ public class UsersController( /// Adds applicant with user account [HttpPost("register")] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task Register(RegisterApplicantRequest request, CancellationToken cancellationToken) { @@ -40,7 +39,6 @@ public class UsersController( /// Accessible only for admins [HttpPost("authorities")] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status400BadRequest)] diff --git a/SchengenVisaApi/SchengenVisaApi/Controllers/VisaApplicationController.cs b/SchengenVisaApi/SchengenVisaApi/Controllers/VisaApplicationController.cs index cb4b164..2f98b40 100644 --- a/SchengenVisaApi/SchengenVisaApi/Controllers/VisaApplicationController.cs +++ b/SchengenVisaApi/SchengenVisaApi/Controllers/VisaApplicationController.cs @@ -66,6 +66,7 @@ public class VisaApplicationController( [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [Authorize(policy: PolicyConstants.ApplicantPolicy)] public async Task CloseApplication(Guid applicationId, CancellationToken cancellationToken) { diff --git a/SchengenVisaApi/VisaApiClient/Client.cs b/SchengenVisaApi/VisaApiClient/Client.cs index c65b9de..7cbe2e8 100644 --- a/SchengenVisaApi/VisaApiClient/Client.cs +++ b/SchengenVisaApi/VisaApiClient/Client.cs @@ -131,16 +131,6 @@ namespace VisaApiClient return; } else - if (status_ == 409) - { - var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); - if (objectResponse_.Object == null) - { - throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); - } - throw new ApiException("Conflict", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); - } - else if (status_ == 400) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); @@ -239,16 +229,6 @@ namespace VisaApiClient return; } else - if (status_ == 409) - { - var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); - if (objectResponse_.Object == null) - { - throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); - } - throw new ApiException("Conflict", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); - } - else if (status_ == 403) { var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); @@ -1220,6 +1200,16 @@ namespace VisaApiClient throw new ApiException("Not Found", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); } else + if (status_ == 400) + { + var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); + if (objectResponse_.Object == null) + { + throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); + } + throw new ApiException("Bad Request", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); + } + else { var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null); @@ -1938,9 +1928,8 @@ namespace VisaApiClient [System.ComponentModel.DataAnnotations.Required] public System.Collections.Generic.ICollection PastVisas { get; set; } = new System.Collections.ObjectModel.Collection(); - [Newtonsoft.Json.JsonProperty("permissionToDestCountry", Required = Newtonsoft.Json.Required.Always)] - [System.ComponentModel.DataAnnotations.Required] - public PermissionToDestCountryModel PermissionToDestCountry { get; set; } = new PermissionToDestCountryModel(); + [Newtonsoft.Json.JsonProperty("permissionToDestCountry", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + public PermissionToDestCountryModel PermissionToDestCountry { get; set; } = default!; [Newtonsoft.Json.JsonProperty("pastVisits", Required = Newtonsoft.Json.Required.Always)] [System.ComponentModel.DataAnnotations.Required]