diff --git a/README.md b/README.md index 94b08a7..81ae64e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # WF-PmAPI +### Paquets +NSwag.AspNetCore + diff --git a/WF_EF_Api/DTO/CharacterDTO.cs b/WF_EF_Api/DTO/CharacterDTO.cs new file mode 100644 index 0000000..00d402c --- /dev/null +++ b/WF_EF_Api/DTO/CharacterDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/CommentaryDTO.cs b/WF_EF_Api/DTO/CommentaryDTO.cs new file mode 100644 index 0000000..b901f14 --- /dev/null +++ b/WF_EF_Api/DTO/CommentaryDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/DTO.csproj b/WF_EF_Api/DTO/DTO.csproj new file mode 100644 index 0000000..bb23fb7 --- /dev/null +++ b/WF_EF_Api/DTO/DTO.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/WF_EF_Api/DTO/FavoriteDTO.cs b/WF_EF_Api/DTO/FavoriteDTO.cs new file mode 100644 index 0000000..16173a4 --- /dev/null +++ b/WF_EF_Api/DTO/FavoriteDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/ImageDTO.cs b/WF_EF_Api/DTO/ImageDTO.cs new file mode 100644 index 0000000..2037793 --- /dev/null +++ b/WF_EF_Api/DTO/ImageDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/QuestionDTO.cs b/WF_EF_Api/DTO/QuestionDTO.cs new file mode 100644 index 0000000..cdf2fb1 --- /dev/null +++ b/WF_EF_Api/DTO/QuestionDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/QuizDTO.cs b/WF_EF_Api/DTO/QuizDTO.cs new file mode 100644 index 0000000..abdeb3a --- /dev/null +++ b/WF_EF_Api/DTO/QuizDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/QuizQuestionDTO.cs b/WF_EF_Api/DTO/QuizQuestionDTO.cs new file mode 100644 index 0000000..fe5cf76 --- /dev/null +++ b/WF_EF_Api/DTO/QuizQuestionDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/QuoteDTO.cs b/WF_EF_Api/DTO/QuoteDTO.cs new file mode 100644 index 0000000..6d35842 --- /dev/null +++ b/WF_EF_Api/DTO/QuoteDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/ResultsDTO.cs b/WF_EF_Api/DTO/ResultsDTO.cs new file mode 100644 index 0000000..de28b59 --- /dev/null +++ b/WF_EF_Api/DTO/ResultsDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/SourceDTO.cs b/WF_EF_Api/DTO/SourceDTO.cs new file mode 100644 index 0000000..9db4871 --- /dev/null +++ b/WF_EF_Api/DTO/SourceDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/DTO/TypeSrcEnumDTO.cs b/WF_EF_Api/DTO/TypeSrcEnumDTO.cs new file mode 100644 index 0000000..8d5049d --- /dev/null +++ b/WF_EF_Api/DTO/TypeSrcEnumDTO.cs @@ -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 + } +} diff --git a/WF_EF_Api/DTO/UserDTO.cs b/WF_EF_Api/DTO/UserDTO.cs new file mode 100644 index 0000000..672cde3 --- /dev/null +++ b/WF_EF_Api/DTO/UserDTO.cs @@ -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; } + } +} diff --git a/WF_EF_Api/StubApi/StubApi.csproj b/WF_EF_Api/StubApi/StubApi.csproj new file mode 100644 index 0000000..de86de5 --- /dev/null +++ b/WF_EF_Api/StubApi/StubApi.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/WF_EF_Api/StubApi/UserService.cs b/WF_EF_Api/StubApi/UserService.cs new file mode 100644 index 0000000..039d8ab --- /dev/null +++ b/WF_EF_Api/StubApi/UserService.cs @@ -0,0 +1,108 @@ +using Shared; +using DTO; +namespace StubApi +{ + public class UserService : IUserService + { + private readonly List _users; + + public UserService() + { + _users = new List + { + 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 CountUser() + { + throw new NotImplementedException(); + } + + public async Task ExistEmail(string email) + { + throw new NotImplementedException(); + } + + public async Task ExistUsername(string username) + { + throw new NotImplementedException(); + } + + public async Task> GetAllUser() + { + return new PaginationResult(_users.Count,0,_users.Count, _users); + } + + public async Task GetHashPassword(string username) + { + return _users.FirstOrDefault(u => u.Pseudo == username).Password; + } + + public async Task GetLastUserId() + { + throw new NotImplementedException(); + } + + public async Task> GetSomeUser(int index, int pageSize) + { + return new PaginationResult(_users.Count, index, pageSize, _users.Skip(index* pageSize).Take(pageSize).ToList()); + } + + public async Task GetUserByEmail(string email) + { + return _users.FirstOrDefault(u => u.Email == email); + } + + public async Task GetUserById(int id) + { + return _users.FirstOrDefault(u => u.Id == id); + } + + + public async Task 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(); + } + } +} diff --git a/WF_EF_Api/WF_EF_Api.sln b/WF_EF_Api/WF_EF_Api.sln index 2b163b7..3942dd1 100644 --- a/WF_EF_Api/WF_EF_Api.sln +++ b/WF_EF_Api/WF_EF_Api.sln @@ -23,6 +23,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WfApi", "WfApi\WfApi.csproj EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{708875DC-7ED1-4FD1-9321-3E00B2E7709B}" 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WF_EF_Api/WfApi/Controllers/UsersController.cs b/WF_EF_Api/WfApi/Controllers/UsersController.cs new file mode 100644 index 0000000..7fb0e56 --- /dev/null +++ b/WF_EF_Api/WfApi/Controllers/UsersController.cs @@ -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 _user; + + private readonly ILogger _logger; + public UsersController(IUserService userService, ILogger logger) + { + _user = userService; + _logger = logger; + + } + + //===================================== ROUTE GET ===================================== + + [HttpGet("id/{id}")] // Indiquer que l'id est dans l'URL + public async Task Get(int id) + { + try + { + var result = _user.GetUserById(id); + + if(result.IsCompletedSuccessfully) + { + return await Task.FromResult(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 GetAllUsers(int index =0, int count = 5) + { + try + { + var result = _user.GetSomeUser(index, count); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(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 GetHashPassword(string username) + { + try + { + var result = _user.GetHashPassword(username); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(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 GetUserByUsername(string username) + { + try + { + var result = _user.GetUserByUsername(username); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(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 GetUserByEmail(string email) + { + try + { + var result = _user.GetUserByEmail(email); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(Ok(result)); + } + else + { + return NoContent(); + } + } + catch (Exception) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + } + } + + + + } +} diff --git a/WF_EF_Api/WfApi/Controllers/WeatherForecastController.cs b/WF_EF_Api/WfApi/Controllers/WeatherForecastController.cs deleted file mode 100644 index c618314..0000000 --- a/WF_EF_Api/WfApi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -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 _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable 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(); - } - } -} diff --git a/WF_EF_Api/WfApi/Program.cs b/WF_EF_Api/WfApi/Program.cs index 15eacee..0e7d0a2 100644 --- a/WF_EF_Api/WfApi/Program.cs +++ b/WF_EF_Api/WfApi/Program.cs @@ -1,8 +1,37 @@ +using DTO; +using Shared; +using StubApi; + var builder = WebApplication.CreateBuilder(args); +builder.Services.AddScoped, UserService>(); // Add services to the container. 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 builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); @@ -12,7 +41,7 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); + app.UseOpenApi(); // Active la documentation OpenAPI app.UseSwaggerUI(); } diff --git a/WF_EF_Api/WfApi/WfApi.csproj b/WF_EF_Api/WfApi/WfApi.csproj index ff8c292..a68150a 100644 --- a/WF_EF_Api/WfApi/WfApi.csproj +++ b/WF_EF_Api/WfApi/WfApi.csproj @@ -6,11 +6,19 @@ enable 283a610b-b95e-4b09-af22-b7d3270a917d Linux + true + $(NoWarn);1591 + + + + + +