Admin controller, Locations controller, requests to add available countries, request to get available countries
This commit is contained in:
		| @@ -0,0 +1,37 @@ | ||||
| using Microsoft.Extensions.Options; | ||||
| using Microsoft.OpenApi.Models; | ||||
| using Swashbuckle.AspNetCore.SwaggerGen; | ||||
|  | ||||
| namespace SchengenVisaApi.Common | ||||
| { | ||||
|     public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions> | ||||
|     { | ||||
|         void IConfigureOptions<SwaggerGenOptions>.Configure(SwaggerGenOptions options) | ||||
|         { | ||||
|             options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | ||||
|             { | ||||
|                 In = ParameterLocation.Header, | ||||
|                 Description = "Provide a JWT-token.", | ||||
|                 Name = "Authorization", | ||||
|                 Type = SecuritySchemeType.Http, | ||||
|                 BearerFormat = "JWT", | ||||
|                 Scheme = "Bearer" | ||||
|             }); | ||||
|  | ||||
|             options.AddSecurityRequirement(new OpenApiSecurityRequirement | ||||
|             { | ||||
|                 { | ||||
|                     new OpenApiSecurityScheme | ||||
|                     { | ||||
|                         Reference = new OpenApiReference | ||||
|                         { | ||||
|                             Type = ReferenceType.SecurityScheme, | ||||
|                             Id = "Bearer" | ||||
|                         } | ||||
|                     }, | ||||
|                     Array.Empty<string>() | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								SchengenVisaApi/SchengenVisaApi/Common/PolicyConstants.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								SchengenVisaApi/SchengenVisaApi/Common/PolicyConstants.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| namespace SchengenVisaApi.Common | ||||
| { | ||||
| #pragma warning disable CS1591 | ||||
|     public static class PolicyConstants | ||||
|     { | ||||
|         public const string AdminPolicy = "AdminPolicy"; | ||||
|         public const string ApplicantPolicy = "ApplicantPolicy"; | ||||
|         public const string ApprovingAuthorityPolicy = "ApprovingAuthorityPolicy"; | ||||
|     } | ||||
| #pragma warning enable CS1591 | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,22 @@ | ||||
| using ApplicationLayer.DataAccessingServices.Locations.RequestHandlers.AdminRequests; | ||||
| using ApplicationLayer.DataAccessingServices.Locations.Requests; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using SchengenVisaApi.Common; | ||||
|  | ||||
| namespace SchengenVisaApi.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     [Route("admin")] | ||||
|     [Authorize(policy: PolicyConstants.AdminPolicy)] | ||||
|     public class AdminController(IEditLocationsRequestsHandler requestsHandler) : ControllerBase | ||||
|     { | ||||
|         [HttpPost] | ||||
|         [Route("country")] | ||||
|         public async Task<IActionResult> AddCountry(AddCountryRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await requestsHandler.AddCountryAsync(request, cancellationToken); | ||||
|             return Ok(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using ApplicationLayer.DataAccessingServices.Locations.RequestHandlers.ApplicantRequests; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace SchengenVisaApi.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     [Route("countries")] | ||||
|     public class LocationsController(ILocationRequestsHandler requestsHandler) : ControllerBase | ||||
|     { | ||||
|         [HttpGet] | ||||
|         public async Task<IActionResult> GetAvailableLocations(CancellationToken cancellationToken) | ||||
|         { | ||||
|             return Ok(await requestsHandler.HandleGetRequestAsync(cancellationToken)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,14 +1,13 @@ | ||||
| using ApplicationLayer.AuthServices.LoginService; | ||||
| using ApplicationLayer.AuthServices.RegisterService; | ||||
| using ApplicationLayer.AuthServices.Requests; | ||||
| using Microsoft.AspNetCore.Identity.Data; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.LoginService; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.RegisterService; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace SchengenVisaApi.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     [Route("auth")] | ||||
|     public class UsersController(IRegisterService registerService, ILoginService loginService) : Controller | ||||
|     public class UsersController(IRegisterService registerService, ILoginService loginService) : ControllerBase | ||||
|     { | ||||
|         [HttpPost] | ||||
|         public async Task<IActionResult> Register(RegisterApplicantRequest request, CancellationToken cancellationToken) | ||||
|   | ||||
| @@ -1,23 +1,29 @@ | ||||
| using System.Security.Claims; | ||||
| using ApplicationLayer.DataAccessingServices.VisaApplications.Handlers; | ||||
| using ApplicationLayer.DataAccessingServices.VisaApplications.Requests; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using SchengenVisaApi.Common; | ||||
|  | ||||
| namespace SchengenVisaApi.Controllers; | ||||
|  | ||||
| [ApiController] | ||||
| [Route("[controller]")] | ||||
| public class VisaApplicationController(IVisaApplicationsRequestHandler visaApplicationsRequestHandler) : ControllerBase | ||||
| [Authorize(policy: PolicyConstants.ApplicantPolicy)] | ||||
| public class VisaApplicationController(IVisaApplicationRequestsHandler visaApplicationRequestsHandler) : ControllerBase | ||||
| { | ||||
|     //TODO remove | ||||
|     [HttpGet] | ||||
|     public async Task<IActionResult> Get(CancellationToken cancellationToken) | ||||
|     { | ||||
|         var result = await visaApplicationsRequestHandler.Get(cancellationToken); | ||||
|         var result = await visaApplicationRequestsHandler.Get(cancellationToken); | ||||
|         return Ok(result); | ||||
|     } | ||||
|  | ||||
|     [HttpPost] | ||||
|     public void Create(VisaApplicationCreateRequest request, CancellationToken cancellationToken) | ||||
|     { | ||||
|         visaApplicationsRequestHandler.HandleCreateRequest(request, cancellationToken); | ||||
|         var userId = Guid.Parse(HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value); | ||||
|         visaApplicationRequestsHandler.HandleCreateRequest(userId, request, cancellationToken); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,10 +1,16 @@ | ||||
| using System.Reflection; | ||||
| using System.Security.Claims; | ||||
| using System.Text; | ||||
| using ApplicationLayer; | ||||
| using Domains.Users; | ||||
| using Infrastructure; | ||||
| using Infrastructure.Auth; | ||||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.Extensions.Options; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
| using SchengenVisaApi.Common; | ||||
| using Swashbuckle.AspNetCore.SwaggerGen; | ||||
|  | ||||
| namespace SchengenVisaApi; | ||||
|  | ||||
| @@ -19,7 +25,7 @@ public static class DependencyInjection | ||||
|  | ||||
|         builder.Services | ||||
|             .AddInfrastructure(config, environment.IsDevelopment()) | ||||
|             .AddApplicationLayer() | ||||
|             .AddApplicationLayer(environment.IsDevelopment()) | ||||
|             .AddAuth(config) | ||||
|             .AddPresentation(environment); | ||||
|     } | ||||
| @@ -52,7 +58,7 @@ public static class DependencyInjection | ||||
|  | ||||
|         services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||||
|             .AddJwtBearer(opts => opts.TokenValidationParameters = parameters); | ||||
|         services.AddAuthorization(); | ||||
|         services.AddAuthorizationBuilder().ConfigureAuthorizationPolicies(); | ||||
|  | ||||
|         services.AddTokenGenerator(new TokenGeneratorOptions( | ||||
|             Issuer: parameters.ValidIssuer!, | ||||
| @@ -64,9 +70,24 @@ public static class DependencyInjection | ||||
|         return services; | ||||
|     } | ||||
|  | ||||
|     /// Configure roles | ||||
|     private static void ConfigureAuthorizationPolicies(this AuthorizationBuilder builder) | ||||
|     { | ||||
|         builder.AddPolicy( | ||||
|                 PolicyConstants.AdminPolicy, | ||||
|                 p => p.RequireClaim(ClaimTypes.Role, Role.Admin.ToString())) | ||||
|             .AddPolicy( | ||||
|                 PolicyConstants.ApprovingAuthorityPolicy, | ||||
|                 p => p.RequireClaim(ClaimTypes.Role, Role.ApprovingAuthority.ToString())) | ||||
|             .AddPolicy( | ||||
|                 PolicyConstants.ApplicantPolicy, | ||||
|                 p => p.RequireClaim(ClaimTypes.Role, Role.Applicant.ToString())); | ||||
|     } | ||||
|  | ||||
|     /// Add swagger | ||||
|     private static void AddSwagger(this IServiceCollection services) | ||||
|     { | ||||
|         services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>(); | ||||
|         services.AddSwaggerGen(options => | ||||
|         { | ||||
|             var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user