API instanciée + route GET

pull/4/head
lebeaulato 1 month ago
parent 7ff1bacb96
commit 0a91cae33c

@ -1,2 +1,5 @@
# WF-PmAPI # WF-PmAPI
### Paquets
NSwag.AspNetCore

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class CharacterDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string imagePath { get; set; }
}
}

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class CommentaryDTO
{
public int Id { get; set; }
public string Comment { get; set; }
public DateTime Date { get; set; }
public string User { get; set; }
public string ImagePath { get; set; }
}
}

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

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class FavoriteDTO
{
public int IdUser { get; set; }
public int IdQuote { get; set; }
}
}

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class ImageDTO
{
public int IdImage { get; set; }
public string ImagePath { get; set; }
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class QuestionDTO
{
public int Id { get; set; }
public string Question { get; set; }
public string AnswerA { get; set; }
public string AnswerB { get; set; }
public string AnswerC { get; set; }
public string AnswerD { get; set; }
public string CorrectAnswer { get; set; }
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class QuizDTO
{
public int Id { get; set; }
public int NbQuestion { get; set; }
public string ImagePath { get; set; }
public string Title { get; set; }
}
}

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class QuizQuestionDTO
{
public int IdQuestion { get; set; }
public int IdQuiz { get; set; }
}
}

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class QuoteDTO
{
public int Id { get; set; }
public string Content { get; set; }
public string Character { get; set; }
public string ImagePath { get; set; }
public string TitleSource { get; set; }
public DateTime DateSource { get; set; }
public int Like { get; set; }
public string Langage { get; set; }
public TypeSrcEnumDTO Type { get; set; }
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class ResultsDTO
{
public int IdQuiz { get; set; }
public int IdUser { get; set; }
public int Time { get; set; }
public int NbPoint { get; set; }
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class SourceDTO
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public TypeSrcEnumDTO Type { get; set; }
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public enum TypeSrcEnumDTO
{
movie,
serie,
book,
videogame
}
}

@ -0,0 +1,12 @@
namespace DTO
{
public class UserDTO
{
public int Id { get; set; }
public string Pseudo { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Years { get; set; }
public string ImageProfil { get; set; }
}
}

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

@ -0,0 +1,108 @@
using Shared;
using DTO;
namespace StubApi
{
public class UserService : IUserService<UserDTO>
{
private readonly List<UserDTO> _users;
public UserService()
{
_users = new List<UserDTO>
{
new UserDTO { Id = 1, Pseudo = "dev", Password = "1234", Email = "testeur@example.com", Years = "2023", ImageProfil = "https://image_profil_1" },
new UserDTO { Id = 2, Pseudo = "admin", Password = "admin123", Email = "admin@example.com", Years = "2024", ImageProfil = "https://image_profil_2" },
new UserDTO { Id = 3, Pseudo = "user123", Password = "password123", Email = "user123@example.com", Years = "2022", ImageProfil = "https://image_profil_3" },
new UserDTO { Id = 4, Pseudo = "testuser", Password = "testpass", Email = "testuser@example.com", Years = "2025", ImageProfil = "https://image_profil_4" },
new UserDTO { Id = 5, Pseudo = "johnDoe", Password = "john123", Email = "johndoe@example.com", Years = "2021", ImageProfil = "https://image_profil_5" },
new UserDTO { Id = 6, Pseudo = "janedoe", Password = "jane123", Email = "janedoe@example.com", Years = "2023", ImageProfil = "https://image_profil_6" },
new UserDTO { Id = 7, Pseudo = "mark_smith", Password = "mark1234", Email = "marks@example.com", Years = "2022", ImageProfil = "https://image_profil_7" },
new UserDTO { Id = 8, Pseudo = "alice", Password = "alicepass", Email = "alice@example.com", Years = "2024", ImageProfil = "https://image_profil_8" },
new UserDTO { Id = 9, Pseudo = "bob_lee", Password = "bob123", Email = "bob@example.com", Years = "2021", ImageProfil = "https://image_profil_9" },
new UserDTO { Id = 10, Pseudo = "lucas", Password = "lucaspass", Email = "lucas@example.com", Years = "2023", ImageProfil = "https://image_profil_10" },
new UserDTO { Id = 11, Pseudo = "emily", Password = "emily2024", Email = "emily@example.com", Years = "2022", ImageProfil = "https://image_profil_11" },
new UserDTO { Id = 12, Pseudo = "chris", Password = "chris1234", Email = "chris@example.com", Years = "2024", ImageProfil = "https://image_profil_12" },
new UserDTO { Id = 13, Pseudo = "susan", Password = "susan123", Email = "susan@example.com", Years = "2023", ImageProfil = "https://image_profil_13" },
new UserDTO { Id = 14, Pseudo = "michael", Password = "michael2025", Email = "michael@example.com", Years = "2022", ImageProfil = "https://image_profil_14" },
new UserDTO { Id = 15, Pseudo = "olivia", Password = "olivia2024", Email = "olivia@example.com", Years = "2024", ImageProfil = "https://image_profil_15" },
new UserDTO { Id = 16, Pseudo = "david", Password = "david123", Email = "david@example.com", Years = "2021", ImageProfil = "https://image_profil_16" },
new UserDTO { Id = 17, Pseudo = "laura", Password = "laura456", Email = "laura@example.com", Years = "2023", ImageProfil = "https://image_profil_17" },
new UserDTO { Id = 18, Pseudo = "steve", Password = "steve789", Email = "steve@example.com", Years = "2022", ImageProfil = "https://image_profil_18" },
new UserDTO { Id = 19, Pseudo = "tina", Password = "tina321", Email = "tina@example.com", Years = "2024", ImageProfil = "https://image_profil_19" },
new UserDTO { Id = 20, Pseudo = "peter", Password = "peter123", Email = "peter@example.com", Years = "2025", ImageProfil = "https://image_profil_20" }
};
}
public async Task AddUser(UserDTO user)
{
throw new NotImplementedException();
}
public async Task<int> CountUser()
{
throw new NotImplementedException();
}
public async Task<bool> ExistEmail(string email)
{
throw new NotImplementedException();
}
public async Task<bool> ExistUsername(string username)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<UserDTO>> GetAllUser()
{
return new PaginationResult<UserDTO>(_users.Count,0,_users.Count, _users);
}
public async Task<string> GetHashPassword(string username)
{
return _users.FirstOrDefault(u => u.Pseudo == username).Password;
}
public async Task<int> GetLastUserId()
{
throw new NotImplementedException();
}
public async Task<PaginationResult<UserDTO>> GetSomeUser(int index, int pageSize)
{
return new PaginationResult<UserDTO>(_users.Count, index, pageSize, _users.Skip(index* pageSize).Take(pageSize).ToList());
}
public async Task<UserDTO> GetUserByEmail(string email)
{
return _users.FirstOrDefault(u => u.Email == email);
}
public async Task<UserDTO> GetUserById(int id)
{
return _users.FirstOrDefault(u => u.Id == id);
}
public async Task<UserDTO> GetUserByUsername(string username)
{
return _users.FirstOrDefault(u => u.Pseudo == username);
}
public async Task RemoveUser(int userId)
{
throw new NotImplementedException();
}
public async Task SetAdminRole(bool isAdmin)
{
throw new NotImplementedException();
}
public async Task UpdateUser(int userId, UserDTO user)
{
throw new NotImplementedException();
}
}
}

@ -23,6 +23,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WfApi", "WfApi\WfApi.csproj
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{708875DC-7ED1-4FD1-9321-3E00B2E7709B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{708875DC-7ED1-4FD1-9321-3E00B2E7709B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubApi", "StubApi\StubApi.csproj", "{4C5F324C-28C3-4153-B402-A07B099C24C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTO", "DTO\DTO.csproj", "{99F43B70-4143-4807-9EE7-B8A799B1E702}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -69,6 +73,14 @@ Global
{708875DC-7ED1-4FD1-9321-3E00B2E7709B}.Debug|Any CPU.Build.0 = Debug|Any CPU {708875DC-7ED1-4FD1-9321-3E00B2E7709B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{708875DC-7ED1-4FD1-9321-3E00B2E7709B}.Release|Any CPU.ActiveCfg = Release|Any CPU {708875DC-7ED1-4FD1-9321-3E00B2E7709B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{708875DC-7ED1-4FD1-9321-3E00B2E7709B}.Release|Any CPU.Build.0 = Release|Any CPU {708875DC-7ED1-4FD1-9321-3E00B2E7709B}.Release|Any CPU.Build.0 = Release|Any CPU
{4C5F324C-28C3-4153-B402-A07B099C24C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C5F324C-28C3-4153-B402-A07B099C24C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C5F324C-28C3-4153-B402-A07B099C24C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C5F324C-28C3-4153-B402-A07B099C24C6}.Release|Any CPU.Build.0 = Release|Any CPU
{99F43B70-4143-4807-9EE7-B8A799B1E702}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99F43B70-4143-4807-9EE7-B8A799B1E702}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99F43B70-4143-4807-9EE7-B8A799B1E702}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99F43B70-4143-4807-9EE7-B8A799B1E702}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,140 @@
using DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared;
using System.Net;
namespace WfApi.Controllers
{
[ApiController]
[Route("api/v1/users")] //Version API
public class UsersController : ControllerBase
{
private readonly IUserService<UserDTO> _user;
private readonly ILogger<UsersController> _logger;
public UsersController(IUserService<UserDTO> userService, ILogger<UsersController> logger)
{
_user = userService;
_logger = logger;
}
//===================================== ROUTE GET =====================================
[HttpGet("id/{id}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> Get(int id)
{
try
{
var result = _user.GetUserById(id);
if(result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
[HttpGet("all")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetAllUsers(int index =0, int count = 5)
{
try
{
var result = _user.GetSomeUser(index, count);
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
[HttpGet("hashpassword/{username}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetHashPassword(string username)
{
try
{
var result = _user.GetHashPassword(username);
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
[HttpGet("username/{username}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetUserByUsername(string username)
{
try
{
var result = _user.GetUserByUsername(username);
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
[HttpGet("email/{email}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetUserByEmail(string email)
{
try
{
var result = _user.GetUserByEmail(email);
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
}
}

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace WfApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

@ -1,8 +1,37 @@
using DTO;
using Shared;
using StubApi;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IUserService<UserDTO>, UserService>();
// Add services to the container. // Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddOpenApiDocument(options => {
options.PostProcess = document =>
{
document.Info = new NSwag.OpenApiInfo
{
Version = "v1",
Title = "WtfAPI",
Description = "What The Fantasy Site API",
TermsOfService = "",
Contact = new NSwag.OpenApiContact
{
Name = "The PM Mobile What The Fantasy team",
Url = ""
},
License = new NSwag.OpenApiLicense
{
Name = "No license",
Url = "https://license.fr"
}
};
};
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
@ -12,7 +41,7 @@ var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseOpenApi(); // Active la documentation OpenAPI
app.UseSwaggerUI(); app.UseSwaggerUI();
} }

@ -6,11 +6,19 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>283a610b-b95e-4b09-af22-b7d3270a917d</UserSecretsId> <UserSecretsId>283a610b-b95e-4b09-af22-b7d3270a917d</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="NSwag.AspNetCore" Version="14.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\StubApi\StubApi.csproj" />
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save