Compare commits

..

No commits in common. 'tp3' and 'master' have entirely different histories.
tp3 ... master

@ -3,9 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_Rest", "API_Rest\API_Rest.csproj", "{2FA20B90-3CCD-4EF2-9751-341082793745}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_UnitTest", "API_UnitTest\API_UnitTest.csproj", "{D8815A04-DE74-46F6-BD39-C946F3EE33CA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Rest", "API_Rest\API_Rest.csproj", "{2FA20B90-3CCD-4EF2-9751-341082793745}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -17,10 +15,6 @@ Global
{2FA20B90-3CCD-4EF2-9751-341082793745}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FA20B90-3CCD-4EF2-9751-341082793745}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FA20B90-3CCD-4EF2-9751-341082793745}.Release|Any CPU.Build.0 = Release|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -8,10 +8,6 @@
</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>

@ -1,22 +0,0 @@
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");
}
}
}

@ -1,13 +0,0 @@
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,88 +1,103 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
namespace API_Rest.Controllers
{
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
private static List<String> Summaries = new List<String>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public static List<WeatherForecast> Weathers = new List<WeatherForecast>
{
public WeatherForecastService _wfs { get; set; }
new WeatherForecast
{
Id = 1,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 2,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 3,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
}
};
public IEnumerable<WeatherForecast> WeatherForecasts { get; set; }
public WeatherForecast wf { get; set; }
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherForecastService wfs)
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_wfs = wfs;
_logger = logger;
}
[HttpGet("GetAll/")]
[Authorize]
public async Task<IEnumerable<WeatherForecast>> GetAll()
[HttpGet]
[Route("/api/WeatherForecast/getAll/")]
public IEnumerable<WeatherForecast> GetAll()
{
WeatherForecasts = _wfs.Get();
return WeatherForecasts;
return Weathers
.ToArray();
}
[HttpGet]
[Authorize]
public async Task<IActionResult> Get(int page, int pageSize)
{
WeatherForecasts = _wfs.Get();
var pageResuts = WeatherForecasts.Skip(page).Take(pageSize);
return Ok(pageResuts);
[Route("/api/WeatherForecast/getOne/{id}")]
}
[HttpGet("GetOne/{id}")]
[Authorize]
public async Task<ActionResult<WeatherForecast>> GetOne(long id)
{
wf = _wfs.GetOne(id);
if (wf != null)
foreach (WeatherForecast wf in Weathers)
{
return Ok(wf);
if (wf.Id == id)
{
return Ok(wf);
}
}
return BadRequest();
}
[HttpPut("Update/{id}")]
[Authorize]
public async Task<ActionResult<WeatherForecast>> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) {
_wfs.UpdateWeatherForcast(id, weatherForcast);
return Ok();
}
[HttpPost("{WeatherForecast wf}")]
[Authorize]
[HttpPost(Name = "PostWeatherForecast")]
public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf)
{
_wfs.Post(wf);
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
//_context.TodoItems.Add(todoItem);
//await _context.SaveChangesAsync();
Weathers.Add(wf);
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
}
[HttpDelete("DeleteOne/{id}")]
[Authorize]
public async Task<ActionResult> DeleteOne(long id)
[HttpDelete]
[Route("/api/WeatherForecast/delete/{id}")]
public async Task<ActionResult> Delete(long id)
{
if (_wfs.DeleteOne(id))
foreach (WeatherForecast wf in Weathers)
{
return Ok();
if (wf.Id == id)
{
Weathers.Remove(wf);
return Ok();
}
}
return BadRequest();
}
}
}

@ -1,55 +1,19 @@
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(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>();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.MapIdentityApi<IdentityUser>();
app.MapSwagger().RequireAuthorization();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@ -57,7 +21,6 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();

@ -1,91 +0,0 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.CompilerServices;
namespace API_Rest
{
public class WeatherForecastService
{
private static List<String> Summaries = new List<String>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public List<WeatherForecast> Weathers { get; set; } = new List<WeatherForecast>
{
new WeatherForecast
{
Id = 1,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 2,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 3,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
}
};
public List<WeatherForecast> Get()
{
return Weathers;
}
public WeatherForecast GetOne(long id)
{
if (Weathers != null)
{
foreach (WeatherForecast wf in Weathers)
{
if (wf.Id == id)
{
return wf;
}
}
return null;
}
return null;
}
public void UpdateWeatherForcast(int id, WeatherForecast weatherForcast)
{
weatherForcast.Id = id;
}
public void Post(WeatherForecast wf)
{
Weathers.Add(wf);
}
public bool DeleteOne(long id)
{
foreach (WeatherForecast wf in Weathers)
{
if (wf.Id == id)
{
Weathers.Remove(wf);
return true;
}
}
return false;
}
}
}

@ -1,29 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\API_Rest\API_Rest.csproj" />
</ItemGroup>
</Project>

@ -1,64 +0,0 @@
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;
namespace API_UnitTest
{
public class UnitTest
{
public ILogger<WeatherForecastController> logger { get; set; }
public WeatherForecastService wfs = new WeatherForecastService();
public WeatherForecastController wfc;
public UnitTest() {
wfc = new WeatherForecastController(logger, wfs);
}
[Fact]
public void GetAll()
{
var res = wfc.GetAll();
Assert.NotNull(res);
}
[Fact]
public async void GetOne()
{
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);
wf.TemperatureC = 5;
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