🎨 Add API in EF repository

WORK-WebAPI
Antoine PEREDERII 1 year ago
parent f9053b73e3
commit 9489242cfb

@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
namespace Dto;
public class AthleteDto
{
public int IdAthlete { get; set; }
[MaxLength(100)]
public required string Username { get; set; }
[MaxLength(150)]
public required string LastName { get; set; }
[MaxLength(100)]
public required string FirstName { get; set; }
public required string Email { get; set; }
public required string Sexe { get; set; }
public double Lenght { get; set; }
public float Weight { get; set; }
public required string Password { get; set; }
public DateTime DateOfBirth { get; set; }
public bool IsCoach { get; set; }
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -15,6 +15,20 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestEntities", "Test
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestRelationships", "Tests\ConsoleTestRelationships\ConsoleTestRelationships.csproj", "{2D166FAD-4934-474B-96A8-6C0635156EC2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestRelationships", "Tests\ConsoleTestRelationships\ConsoleTestRelationships.csproj", "{2D166FAD-4934-474B-96A8-6C0635156EC2}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dto", "Dto\Dto.csproj", "{562019BC-0F61-41B0-9BAE-259B92C6BFBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeartTrackAPI", "HeartTrackAPI\HeartTrackAPI.csproj", "{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedDtoLib", "StubbedDtoLib\StubbedDtoLib.csproj", "{87F65E21-0555-4772-88B4-C8B43146A02C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestsAPI", "TestsAPI", "{30FC2BE9-7397-445A-84AD-043CE70F4281}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientTests", "Tests\TestsAPI\ClientTests\ClientTests.csproj", "{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsXUnit", "Tests\TestsAPI\TestsXUnit\TestsXUnit.csproj", "{44C367DC-5FE0-4CF2-9E76-A0282E931853}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -44,9 +58,36 @@ Global
{2D166FAD-4934-474B-96A8-6C0635156EC2}.Debug|Any CPU.Build.0 = Debug|Any CPU {2D166FAD-4934-474B-96A8-6C0635156EC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D166FAD-4934-474B-96A8-6C0635156EC2}.Release|Any CPU.ActiveCfg = Release|Any CPU {2D166FAD-4934-474B-96A8-6C0635156EC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D166FAD-4934-474B-96A8-6C0635156EC2}.Release|Any CPU.Build.0 = Release|Any CPU {2D166FAD-4934-474B-96A8-6C0635156EC2}.Release|Any CPU.Build.0 = Release|Any CPU
{562019BC-0F61-41B0-9BAE-259B92C6BFBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{562019BC-0F61-41B0-9BAE-259B92C6BFBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{562019BC-0F61-41B0-9BAE-259B92C6BFBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{562019BC-0F61-41B0-9BAE-259B92C6BFBA}.Release|Any CPU.Build.0 = Release|Any CPU
{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}.Release|Any CPU.Build.0 = Release|Any CPU
{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}.Release|Any CPU.Build.0 = Release|Any CPU
{87F65E21-0555-4772-88B4-C8B43146A02C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87F65E21-0555-4772-88B4-C8B43146A02C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87F65E21-0555-4772-88B4-C8B43146A02C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87F65E21-0555-4772-88B4-C8B43146A02C}.Release|Any CPU.Build.0 = Release|Any CPU
{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}.Release|Any CPU.Build.0 = Release|Any CPU
{44C367DC-5FE0-4CF2-9E76-A0282E931853}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44C367DC-5FE0-4CF2-9E76-A0282E931853}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44C367DC-5FE0-4CF2-9E76-A0282E931853}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44C367DC-5FE0-4CF2-9E76-A0282E931853}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} {477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18}
{2D166FAD-4934-474B-96A8-6C0635156EC2} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} {2D166FAD-4934-474B-96A8-6C0635156EC2} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18}
{30FC2BE9-7397-445A-84AD-043CE70F4281} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18}
{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A} = {30FC2BE9-7397-445A-84AD-043CE70F4281}
{44C367DC-5FE0-4CF2-9E76-A0282E931853} = {30FC2BE9-7397-445A-84AD-043CE70F4281}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

@ -0,0 +1,26 @@
using Dto;
using Microsoft.AspNetCore.Mvc;
using Shared;
[ApiController]
[Route("api/athletes")]
public class AthletesController : ControllerBase
{
private readonly ILogger<AthletesController> _logger;
IAthleteService _stubbedDto;
private const int DEFAULT_INDEX = 0, DEFAULT_COUNT = 5;
public AthletesController(ILogger<AthletesController> logger, IAthleteService athletesService)
{
_logger = logger;
_stubbedDto = athletesService;
}
[HttpGet("all")]
[ProducesResponseType(typeof(IEnumerable<AthleteDto>), 200)]
public async Task<IActionResult> GetAllAthletesAsync()
{
var athletes = await _stubbedDto.GetAllAthletesAsync();
return Ok(athletes);
}
}

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Share\Share.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,6 @@
@HeartTrackAPI_HostAddress = http://localhost:5030
GET {{HeartTrackAPI_HostAddress}}/weatherforecast/
Accept: application/json
###

@ -0,0 +1,44 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:13881",
"sslPort": 44333
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5030",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7233;http://localhost:5030",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

@ -0,0 +1,11 @@
namespace Shared
{
public enum AthleteOrderCriteria
{
None,
ByUsername,
ByEmail,
ByIsCoach
}
}

@ -0,0 +1,23 @@
using Dto;
namespace Shared;
public interface IAthleteService
{
public Task<IEnumerable<AthleteDto>> GetAllAthletesAsync();
public Task<IEnumerable<AthleteDto>> GetSomeAthletesAsync(int index, int count, AthleteOrderCriteria criterium);
public Task<AthleteDto?> GetAthleteByIdAsync(int id);
public Task<IEnumerable<AthleteDto>> GetAthleteByUsernameAsync(string username, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByEmailAsync(string email, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByFirstNameAsync(string firstName, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByLastNameAsync(string lastName, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesBySexeAsync(string sexe, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByLenghtAsync(double lenght, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByWeightAsync(float weight, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByDateOfBirthAsync(DateTime dateOfBirth, int index, int count, AthleteOrderCriteria criterium);
public Task<IEnumerable<AthleteDto>> GetAthletesByIsCoachAsync(bool isCoach, int index, int count, AthleteOrderCriteria criterium);
public Task<AthleteDto> AddAthleteAsync(AthleteDto athlete, AthleteOrderCriteria criterium);
public Task<AthleteDto?> UpdateAthleteAsync(int id, AthleteDto athlete, AthleteOrderCriteria criterium);
public Task<bool> DeleteAthleteAsync(int id, AthleteOrderCriteria criterium);
}

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Dto\Dto.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,135 @@
using System.ComponentModel;
using Dto;
using Share;
namespace StubbedDtoLib;
public class AthleteStubDto : IAthleteService
{
private static List<AthleteDto> AthleteList = new List<AthleteDto>()
{
new AthleteDto { IdAthlete = 1, Username = "Doe", LastName = "John", FirstName = "Doe", Email = "john.doe@example.com", Password = "password123", Sexe = "M", Lenght = 1.80, Weight = 75, DateOfBirth = new DateTime(), IsCoach = true },
new AthleteDto { IdAthlete = 2, Username = "Smith", LastName = "Jane", FirstName = "Smith", Email = "jane.smith@example.com", Password = "secure456", Sexe = "F", Lenght = 1.65, Weight = 60, DateOfBirth = new DateTime(), IsCoach = false },
new AthleteDto { IdAthlete = 3, Username = "Martin", LastName = "Paul", FirstName = "Martin", Email = "paul.martin@example.com", Password = "super789", Sexe = "M", Lenght = 1.75, Weight = 68, DateOfBirth = new DateTime(), IsCoach = true },
new AthleteDto { IdAthlete = 4, Username = "Brown", LastName = "Anna", FirstName = "Brown", Email = "anna.brown@example.com", Password = "test000", Sexe = "M", Lenght = 1.70, Weight = 58, DateOfBirth = new DateTime(), IsCoach = false },
new AthleteDto { IdAthlete = 5, Username = "Lee", LastName = "Bruce", FirstName = "Lee", Email = "bruce.lee@example.com", Password = "hello321", Sexe = "M", Lenght = 2.0, Weight = 90, DateOfBirth = new DateTime(), IsCoach = false }
};
public async Task<IEnumerable<AthleteDto>> GetAllAthletesAsync()
{
return AthleteList;
}
public async Task<IEnumerable<AthleteDto>> GetSomeAthletesAsync(int index, int count)
{
return AthleteList.Skip(count*index).Take(count);
}
public async Task<AthleteDto?> GetAthleteByIdAsync(int id) => AthleteList.Find(a => a.IdAthlete == id);
public async Task<IEnumerable<AthleteDto>> GetAthleteByUsernameAsync(string username, int index, int count)
{
return AthleteList.FindAll(a => a.Username.Contains(username, StringComparison.OrdinalIgnoreCase)).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByEmailAsync(string email, int index, int count)
{
return AthleteList.FindAll(a => a.Email.Contains(email, StringComparison.OrdinalIgnoreCase)).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByFirstNameAsync(string firstName, int index, int count)
{
return AthleteList.FindAll(a => a.FirstName.Contains(firstName, StringComparison.OrdinalIgnoreCase)).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByLastNameAsync(string lastName, int index, int count)
{
return AthleteList.FindAll(a => a.LastName.Contains(lastName, StringComparison.OrdinalIgnoreCase)).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesBySexeAsync(string sexe, int index, int count)
{
return AthleteList.FindAll(a => a.Sexe.Contains(sexe, StringComparison.OrdinalIgnoreCase)).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByLenghtAsync(double lenght, int index, int count)
{
return AthleteList.FindAll(a => a.Lenght == lenght).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByWeightAsync(float weight, int index, int count)
{
return AthleteList.FindAll(a => a.Weight == weight).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByDateOfBirthAsync(DateTime dateOfBirth, int index, int count)
{
return AthleteList.FindAll(a => a.DateOfBirth == dateOfBirth).Skip(index*count).Take(count);
}
public async Task<IEnumerable<AthleteDto>> GetAthletesByIsCoachAsync(bool isCoach, int index, int count)
{
return AthleteList.FindAll(a => a.IsCoach == isCoach).Skip(index*count).Take(count);
}
public async Task<AthleteDto> AddAthleteAsync(AthleteDto athlete)
{
var newAthlete = new AthleteDto()
{
IdAthlete = AthleteList.Max(a => a.IdAthlete) + 1,
Username = athlete.Username,
LastName = athlete.LastName,
FirstName = athlete.FirstName,
Email = athlete.Email,
Password = athlete.Password,
Sexe = athlete.Sexe,
Lenght = athlete.Lenght,
Weight = athlete.Weight,
DateOfBirth = athlete.DateOfBirth,
IsCoach = athlete.IsCoach
};
AthleteList.Add(newAthlete);
return newAthlete;
}
public async Task<AthleteDto?> UpdateAthleteAsync(int id, AthleteDto athlete)
{
var theId = AthleteList.FindIndex(a => a.IdAthlete == id);
if (theId >= 0)
{
var a = AthleteList[theId];
a.Username = athlete.Username;
a.LastName = athlete.LastName;
a.FirstName = athlete.FirstName;
a.Email = athlete.Email;
a.Password = athlete.Password;
a.Sexe = athlete.Sexe;
a.Lenght = athlete.Lenght;
a.Weight = athlete.Weight;
a.DateOfBirth = athlete.DateOfBirth;
a.IsCoach = athlete.IsCoach;
AthleteList[theId] = a;
return a;
}
else
{
return null;
}
}
public async Task<bool> DeleteAthleteAsync(int id)
{
var theId = AthleteList.FindIndex(a => a.IdAthlete == id);
if (theId >= 0)
{
AthleteList.RemoveAt(theId);
return true;
} else
{
return false;
}
}
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Dto\Dto.csproj" />
<ProjectReference Include="..\Share\Share.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

@ -0,0 +1,25 @@
<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.6.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>
</Project>

@ -0,0 +1,10 @@
namespace TestsXUnit;
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
Loading…
Cancel
Save