diff --git a/API_Rest/API_Rest/API_Rest.csproj b/API_Rest/API_Rest/API_Rest.csproj index 213fa15..832285b 100644 --- a/API_Rest/API_Rest/API_Rest.csproj +++ b/API_Rest/API_Rest/API_Rest.csproj @@ -8,6 +8,10 @@ + + + + diff --git a/API_Rest/API_Rest/ApiDbContext.cs b/API_Rest/API_Rest/ApiDbContext.cs new file mode 100644 index 0000000..f0fe4ae --- /dev/null +++ b/API_Rest/API_Rest/ApiDbContext.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using API_Rest; + + +namespace TestStub +{ + public class LibraryContext : DbContext + { + public DbSet WeathersSet { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=tp.WeatherForecats.db"); + } + + } +} \ No newline at end of file diff --git a/API_Rest/API_Rest/ApplicationDbContext.cs b/API_Rest/API_Rest/ApplicationDbContext.cs new file mode 100644 index 0000000..75559fd --- /dev/null +++ b/API_Rest/API_Rest/ApplicationDbContext.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; + +namespace API_Rest +{ + public class ApplicationDbContext : IdentityDbContext + { + public ApplicationDbContext(DbContextOptions options) : + base(options) + { } + } +} diff --git a/API_Rest/API_Rest/Controllers/WeatherForecastController.cs b/API_Rest/API_Rest/Controllers/WeatherForecastController.cs index 332f1fa..2fdae08 100644 --- a/API_Rest/API_Rest/Controllers/WeatherForecastController.cs +++ b/API_Rest/API_Rest/Controllers/WeatherForecastController.cs @@ -1,10 +1,11 @@ +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; namespace API_Rest.Controllers { - + [Authorize] [ApiController] [Route("api/[controller]")] public class WeatherForecastController : ControllerBase @@ -24,7 +25,7 @@ namespace API_Rest.Controllers } [HttpGet("GetAll/")] - + [Authorize] public async Task> GetAll() { WeatherForecasts = _wfs.Get(); @@ -32,8 +33,19 @@ namespace API_Rest.Controllers } - [HttpGet("GetOne/{id}")] + [HttpGet] + [Authorize] + public async Task Get(int page, int pageSize) + { + + WeatherForecasts = _wfs.Get(); + var pageResuts = WeatherForecasts.Skip(page).Take(pageSize); + return Ok(pageResuts); + + } + [HttpGet("GetOne/{id}")] + [Authorize] public async Task> GetOne(long id) { @@ -47,13 +59,14 @@ namespace API_Rest.Controllers } [HttpPut("Update/{id}")] - + [Authorize] public async Task> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) { _wfs.UpdateWeatherForcast(id, weatherForcast); return Ok(); } [HttpPost("{WeatherForecast wf}")] + [Authorize] public async Task> Post(WeatherForecast wf) { _wfs.Post(wf); @@ -62,6 +75,7 @@ namespace API_Rest.Controllers [HttpDelete("DeleteOne/{id}")] + [Authorize] public async Task DeleteOne(long id) { if (_wfs.DeleteOne(id)) diff --git a/API_Rest/API_Rest/Program.cs b/API_Rest/API_Rest/Program.cs index d693ba5..f47e2ce 100644 --- a/API_Rest/API_Rest/Program.cs +++ b/API_Rest/API_Rest/Program.cs @@ -1,21 +1,55 @@ using API_Rest; using API_Rest.Controllers; +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; +using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); +builder.Services.AddIdentityApiEndpoints() + .AddEntityFrameworkStores(); +builder.Services.AddDbContext( + options => options.UseInMemoryDatabase("AppDb")); // Add services to the container. - +builder.Services.AddAuthorization(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(option => +{ + option.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" }); + option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + In = ParameterLocation.Header, + Description = "Please enter a valid token", + Name = "Authorization", + Type = SecuritySchemeType.Http, + BearerFormat = "JWT", + Scheme = "Bearer" + }); + option.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type=ReferenceType.SecurityScheme, + Id="Bearer" + } + }, + new string[]{} + } + }); +}); + builder.Services.AddSingleton(); var app = builder.Build(); +app.MapIdentityApi(); - - +app.MapSwagger().RequireAuthorization(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -23,6 +57,7 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } + app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/API_Rest/API_UnitTest/UnitTest.cs b/API_Rest/API_UnitTest/UnitTest.cs index 95c1414..3e811a8 100644 --- a/API_Rest/API_UnitTest/UnitTest.cs +++ b/API_Rest/API_UnitTest/UnitTest.cs @@ -1,5 +1,7 @@ using API_Rest; using API_Rest.Controllers; +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System.Security.Cryptography.X509Certificates; @@ -20,18 +22,29 @@ namespace API_UnitTest { var res = wfc.GetAll(); Assert.NotNull(res); + } [Fact] - public void GetOne() + public async void GetOne() { - var res = wfc.GetOne(1); + var res = await wfc.GetOne(1) ; Assert.NotNull(res.Result); + } + [Fact] + public async void GetOneFail() + { + var res = await wfc.GetOne(1); + Assert.Equal(new BadRequestResult(), res); + + } + [Fact] public async void Post() { + WeatherForecast wf = new WeatherForecast(); wf.Id = 4; wf.Date = DateOnly.FromDateTime(DateTime.Today); @@ -39,5 +52,13 @@ namespace API_UnitTest await wfc.Post(wf); Assert.Contains(wf,wfs.Weathers); } + + [Fact] + public async void Delete() + { + var res = await wfc.DeleteOne(1); + Assert.NotNull(res); + } + } } \ No newline at end of file