tp3
Tom RAMBEAU 1 year ago
parent 35061e7c24
commit 0e82975038

@ -8,6 +8,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

@ -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<WeatherForecast> WeathersSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=tp.WeatherForecats.db");
}
}
}

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace API_Rest
{
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :
base(options)
{ }
}
}

@ -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<IEnumerable<WeatherForecast>> GetAll()
{
WeatherForecasts = _wfs.Get();
@ -32,8 +33,19 @@ namespace API_Rest.Controllers
}
[HttpGet("GetOne/{id}")]
[HttpGet]
[Authorize]
public async Task<IActionResult> 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<ActionResult<WeatherForecast>> GetOne(long id)
{
@ -47,13 +59,14 @@ namespace API_Rest.Controllers
}
[HttpPut("Update/{id}")]
[Authorize]
public async Task<ActionResult<WeatherForecast>> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) {
_wfs.UpdateWeatherForcast(id, weatherForcast);
return Ok();
}
[HttpPost("{WeatherForecast wf}")]
[Authorize]
public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf)
{
_wfs.Post(wf);
@ -62,6 +75,7 @@ namespace API_Rest.Controllers
[HttpDelete("DeleteOne/{id}")]
[Authorize]
public async Task<ActionResult> DeleteOne(long id)
{
if (_wfs.DeleteOne(id))

@ -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<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddDbContext<ApplicationDbContext>(
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<WeatherForecastService, WeatherForecastService>();
var app = builder.Build();
app.MapIdentityApi<IdentityUser>();
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();

@ -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);
}
}
}
Loading…
Cancel
Save