From 5f9899276a2292ac337a6365af8b95409ea28d9f Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Thu, 22 Feb 2024 10:40:01 +0100 Subject: [PATCH] =?UTF-8?q?mise=20=C3=A0=20jour=20de=20la=20structure=20av?= =?UTF-8?q?ec=20ajout=20de=20service=20pour=20la=20partie=20EntityFramewor?= =?UTF-8?q?k=20et=20ajout=20de=20la=20class=20DbDataManager=20pour=20commu?= =?UTF-8?q?niquer=20avec=20la=20BDD,=20changement=20des=20methode=20dans?= =?UTF-8?q?=20l'API=20qui=20sont=20maintenant=20en=20asynchrone.=20Changem?= =?UTF-8?q?ent=20du=20DbContextLib=20poureffectuer=20les=20migrations=20:?= =?UTF-8?q?=20ajout=20d'un=20constructeur=20et=20indication=20cl=C3=A9=20?= =?UTF-8?q?=C3=A9trang=C3=A8re=20pour=20inquiryEntity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/API.csproj | 1 + API_SQLuedo/API/Controllers/UserController.cs | 16 +- API_SQLuedo/API/Program.cs | 3 + API_SQLuedo/API_SQLuedo.sln | 20 ++- API_SQLuedo/DbContextLib/UserDbContext.cs | 2 + API_SQLuedo/ModelToEntity/DbDataManager.cs | 162 ++++++++++++++++++ API_SQLuedo/ModelToEntity/Extension.cs | 16 ++ API_SQLuedo/ModelToEntity/IDataServiceEF.cs | 33 ++++ .../ModelToEntity/ModelToEntity.csproj | 28 +++ API_SQLuedo/Services/IDataService.cs | 12 +- API_SQLuedo/Services/Services.csproj | 1 + API_SQLuedo/Services/UserDataService.cs | 77 +++------ 12 files changed, 295 insertions(+), 76 deletions(-) create mode 100644 API_SQLuedo/ModelToEntity/DbDataManager.cs create mode 100644 API_SQLuedo/ModelToEntity/Extension.cs create mode 100644 API_SQLuedo/ModelToEntity/IDataServiceEF.cs create mode 100644 API_SQLuedo/ModelToEntity/ModelToEntity.csproj diff --git a/API_SQLuedo/API/API.csproj b/API_SQLuedo/API/API.csproj index cd3cbe6..7bd07a9 100644 --- a/API_SQLuedo/API/API.csproj +++ b/API_SQLuedo/API/API.csproj @@ -24,6 +24,7 @@ + diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index a711180..450c64a 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -24,9 +24,9 @@ namespace API.Controllers } [HttpGet("users/{page}/{number}")] - public IActionResult GetUsers(int page, int number) + public async Task GetUsers(int page, int number) { - var nbUser = _userDataService.GetUsers(page, number).Count(); + var nbUser = (await _userDataService.GetUsers(page, number)).ToList().Count(); if(nbUser == 0) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé."); @@ -37,7 +37,7 @@ namespace API.Controllers } [HttpGet("user/id/{id}")] - public IActionResult GetUserById(int id) + public async Task GetUserById(int id) { try { @@ -51,7 +51,7 @@ namespace API.Controllers } [HttpGet("user/username/{username}")] - public IActionResult GetUserByUsername(string username) + public async Task GetUserByUsername(string username) { try { @@ -66,9 +66,9 @@ namespace API.Controllers } [HttpDelete] - public IActionResult DeleteUser(int id) + public async Task DeleteUser(int id) { - var success = _userDataService.DeleteUser(id); + var success = await _userDataService.DeleteUser(id); if(success) { _logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id); @@ -82,7 +82,7 @@ namespace API.Controllers } [HttpPost] - public IActionResult CreateUser([FromBody]UserDTO dto) + public async Task CreateUser([FromBody]UserDTO dto) { if (dto.Username == null || dto.Password == null || dto.Email == null) { @@ -94,7 +94,7 @@ namespace API.Controllers } [HttpPut] - public IActionResult UpdateUser(int id, [FromBody] UserDTO userDTO) + public async Task UpdateUser(int id, [FromBody] UserDTO userDTO) { if(id != userDTO.Id) { diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index d4dbe01..eb3ee3c 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -3,7 +3,9 @@ using DbContextLib; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; +using ModelToEntity; using Services; +using System.Data.Common; var builder = WebApplication.CreateBuilder(args); @@ -14,6 +16,7 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); builder.Services.AddIdentityApiEndpoints().AddEntityFrameworkStores(); diff --git a/API_SQLuedo/API_SQLuedo.sln b/API_SQLuedo/API_SQLuedo.sln index 888826d..f9b50ec 100644 --- a/API_SQLuedo/API_SQLuedo.sln +++ b/API_SQLuedo/API_SQLuedo.sln @@ -3,19 +3,21 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34408.163 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{65F4AE69-E1CA-4B87-BDF0-946A37351BD1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "API\API.csproj", "{65F4AE69-E1CA-4B87-BDF0-946A37351BD1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "EntityFramework\Entities.csproj", "{6D079CDA-C000-4833-98A0-D07D153EA264}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "EntityFramework\Entities.csproj", "{6D079CDA-C000-4833-98A0-D07D153EA264}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{ADCC427D-A3CD-431C-A90B-F9369C4584E8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{ADCC427D-A3CD-431C-A90B-F9369C4584E8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAPI", "TestAPI\TestAPI.csproj", "{17025B90-8B2A-49E9-97D3-1A84A208DF50}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAPI", "TestAPI\TestAPI.csproj", "{17025B90-8B2A-49E9-97D3-1A84A208DF50}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEF", "TestEF\TestEF.csproj", "{54FAD8AF-7601-4C54-8406-D81476A8D7D7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestEF", "TestEF\TestEF.csproj", "{54FAD8AF-7601-4C54-8406-D81476A8D7D7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Services", "Services\Services.csproj", "{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services", "Services\Services.csproj", "{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelToEntity", "ModelToEntity\ModelToEntity.csproj", "{8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -51,6 +53,10 @@ Global {9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Debug|Any CPU.Build.0 = Debug|Any CPU {9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Release|Any CPU.ActiveCfg = Release|Any CPU {9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Release|Any CPU.Build.0 = Release|Any CPU + {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/API_SQLuedo/DbContextLib/UserDbContext.cs b/API_SQLuedo/DbContextLib/UserDbContext.cs index 9e5d5ba..4bf3c4e 100644 --- a/API_SQLuedo/DbContextLib/UserDbContext.cs +++ b/API_SQLuedo/DbContextLib/UserDbContext.cs @@ -20,6 +20,7 @@ namespace DbContextLib public DbSet Success { get; set; } public DbSet Notepad { get; set; } public UserDbContext(DbContextOptions options) : base(options) { } + public UserDbContext() : base() { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) @@ -67,6 +68,7 @@ namespace DbContextLib modelBuilder.Entity().HasKey(c => c.LessonPartId); modelBuilder.Entity().HasKey(s => s.UserId); modelBuilder.Entity().HasKey(s => s.InquiryId); + modelBuilder.Entity().HasKey(s => s.Id); base.OnModelCreating(modelBuilder); } } diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs new file mode 100644 index 0000000..7ae486b --- /dev/null +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -0,0 +1,162 @@ +using Entities.SQLudeoDB; +using Microsoft.EntityFrameworkCore; +using Model.DTO; +using Services; +using DbContextLib; +using Model.Business; +using Model; +using Model.Mappers; + +namespace ModelToEntity +{ + public class DbDataManager : IDataServiceEF + { + public async Task GetUserById(int id) + { + using(var context = new UserDbContext()) + { + var userEntity = context.Users.FirstOrDefault(u => u.Id == id); + if(userEntity == null) + { + throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id)); + } + return await Task.FromResult(userEntity.FromEntityToModel()); + } + + } + + public async Task GetUserByUsername(string username) + { + using (var context = new UserDbContext()) + { + var userEntity = context.Users.FirstOrDefault(u => u.Username == username); + if (userEntity == null) + { + throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(username)); + } + + return await Task.FromResult(userEntity.FromEntityToModel()); + } + } + + public async Task> GetUsers(int page, int number) + { + using (var context = new UserDbContext()) + { + return await Task.FromResult(context.Users.Skip((page - 1) * number).Take(number).ToList().Select(u => u.FromEntityToModel())); + } + } + + public Task DeleteUser(int id) + { + using (var context = new UserDbContext()) { + var userEntity = context.Users.FirstOrDefault(u => u.Id == id); + if (userEntity == null) + { + return Task.FromResult(false); + } + context.Users.Remove(userEntity); + context.SaveChangesAsync(); + } + return Task.FromResult(true); + } + + public async Task UpdateUser(int id, UserDTO user) + { + using (var context = new UserDbContext()) + { + var updatingUser = context.Users.FirstOrDefault(u => u.Id == id); + if (updatingUser == null) + { + throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id)); + } + updatingUser.Username = user.Username; + updatingUser.Password = user.Password; + updatingUser.Email = user.Email; + updatingUser.IsAdmin = user.IsAdmin; + // Permet d'indiquer en Db que l'entité a été modifiée. + context.Entry(updatingUser).State = EntityState.Modified; + context.SaveChangesAsync(); + return await Task.FromResult(updatingUser.FromEntityToModel()); + } + } + + public async Task CreateUser(string username, string password, string email, bool isAdmin) + { + using (var context = new UserDbContext()) + { + var newUserEntity = new UserDTO + { + Username = username, + Password = password, + Email = email, + IsAdmin = isAdmin + }; + context.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity()); + context.SaveChangesAsync(); + + return await Task.FromResult(newUserEntity.FromDTOToModel()); + } + } + + public IEnumerable GetInquiries(int page, int number) + { + throw new NotImplementedException(); + } + + public InquiryDTO GetInquiryById(int id) + { + throw new NotImplementedException(); + } + + public InquiryDTO GetInquiryByTitle(string title) + { + throw new NotImplementedException(); + } + public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable) + { + throw new NotImplementedException(); + } + + public bool DeleteInquiry(int id) + { + throw new NotImplementedException(); + } + + public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry) + { + throw new NotImplementedException(); + } + + public IEnumerable GetLessons(int page, int number) + { + throw new NotImplementedException(); + } + + public InquiryDTO GetLessonById(int id) + { + throw new NotImplementedException(); + } + + public InquiryDTO GetLessonByTitle(string title) + { + throw new NotImplementedException(); + } + + public bool DeleteLesson(int id) + { + throw new NotImplementedException(); + } + + public InquiryDTO UpdateLesson(int id, LessonDTO lesson) + { + throw new NotImplementedException(); + } + + public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit) + { + throw new NotImplementedException(); + } + } + +} diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs new file mode 100644 index 0000000..b039fe4 --- /dev/null +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Identity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Entities; +using Entities.SQLudeoDB; +using Model.Business; + +namespace ModelToEntity +{ + static class Extension + { + } +} diff --git a/API_SQLuedo/ModelToEntity/IDataServiceEF.cs b/API_SQLuedo/ModelToEntity/IDataServiceEF.cs new file mode 100644 index 0000000..f09c129 --- /dev/null +++ b/API_SQLuedo/ModelToEntity/IDataServiceEF.cs @@ -0,0 +1,33 @@ +using Entities.SQLudeoDB; +using Model.Business; +using Model.DTO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public interface IDataServiceEF + { + public Task> GetUsers(int page, int number); + public Task GetUserById(int id); + public Task GetUserByUsername(string username); + public Task DeleteUser(int id); + public Task UpdateUser(int id, UserDTO user); + public Task CreateUser(string username, string password, string email, bool isAdmin); + public IEnumerable GetInquiries(int page, int number); + public InquiryDTO GetInquiryById(int id); + public InquiryDTO GetInquiryByTitle(string title); + public bool DeleteInquiry(int id); + public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry); + public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable); + public IEnumerable GetLessons(int page, int number); + public InquiryDTO GetLessonById(int id); + public InquiryDTO GetLessonByTitle(string title); + public bool DeleteLesson(int id); + public InquiryDTO UpdateLesson(int id, LessonDTO lesson); + public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit); + } +} diff --git a/API_SQLuedo/ModelToEntity/ModelToEntity.csproj b/API_SQLuedo/ModelToEntity/ModelToEntity.csproj new file mode 100644 index 0000000..4cd7b1b --- /dev/null +++ b/API_SQLuedo/ModelToEntity/ModelToEntity.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/API_SQLuedo/Services/IDataService.cs b/API_SQLuedo/Services/IDataService.cs index c9f84dc..c296774 100644 --- a/API_SQLuedo/Services/IDataService.cs +++ b/API_SQLuedo/Services/IDataService.cs @@ -6,12 +6,12 @@ namespace Services { public interface IDataService { - public IEnumerable GetUsers(int page, int number); - public UserDTO GetUserById(int id); - public UserDTO GetUserByUsername(string username); - public bool DeleteUser(int id); - public UserDTO UpdateUser(int id, UserDTO user); - public UserDTO CreateUser(string username, string password, string email, bool isAdmin); + public Task> GetUsers(int page, int number); + public Task GetUserById(int id); + public Task GetUserByUsername(string username); + public Task DeleteUser(int id); + public Task UpdateUser(int id, UserDTO user); + public Task CreateUser(string username, string password, string email, bool isAdmin); public IEnumerable GetInquiries(int page, int number); public InquiryDTO GetInquiryById(int id); public InquiryDTO GetInquiryByTitle(string title); diff --git a/API_SQLuedo/Services/Services.csproj b/API_SQLuedo/Services/Services.csproj index 4cd7b1b..dd690cb 100644 --- a/API_SQLuedo/Services/Services.csproj +++ b/API_SQLuedo/Services/Services.csproj @@ -22,6 +22,7 @@ + diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 1533da7..4bffa90 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -9,85 +9,52 @@ using Model.Mappers; using Model.Business; using Microsoft.EntityFrameworkCore; using Entities.SQLudeoDB; +using ModelToEntity; namespace Services { public class UserDataService : IDataService { private UserDbContext DbContext { get; set; } - public UserDataService(UserDbContext context) + private readonly IDataServiceEF dataServiceEF; + public UserDataService(IDataServiceEF dataServiceEF) { - DbContext = context; - context.Database.EnsureCreated(); + this.dataServiceEF = dataServiceEF; } - public UserDTO GetUserById(int id) + public async Task GetUserById(int id) { - var userEntity = DbContext.Users.FirstOrDefault(u => u.Id == id); - if (userEntity == null) - { - throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id)); - } - return userEntity.FromEntityToModel().FromModelToDTO(); - + var user = await dataServiceEF.GetUserById(id); + return user.FromModelToDTO(); } - public UserDTO GetUserByUsername(string username) + public async Task GetUserByUsername(string username) { - var userEntity = DbContext.Users.FirstOrDefault(u => u.Username == username); - if (userEntity == null) - { - throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(username)); - } - return userEntity.FromEntityToModel().FromModelToDTO(); + var user = await dataServiceEF.GetUserByUsername(username); + return user.FromModelToDTO(); } - public IEnumerable GetUsers(int page, int number) + public async Task> GetUsers(int page, int number) { - return DbContext.Users.Skip((page - 1) * number).Take(number).ToList().Select(u => u.FromEntityToModel().FromModelToDTO()); + var users = await dataServiceEF.GetUsers(page, number); + return users.Select(u => u.FromModelToDTO()); } - public bool DeleteUser(int id) + public async Task DeleteUser(int id) { - var userEntity = DbContext.Users.FirstOrDefault(u => u.Id == id); - if (userEntity == null) - { - return false; - } - DbContext.Users.Remove(userEntity); - DbContext.SaveChangesAsync(); - return true; + var respons = await dataServiceEF.DeleteUser(id); + return respons; } - public UserDTO UpdateUser(int id, UserDTO user) + public async Task UpdateUser(int id, UserDTO user) { - var updatingUser = DbContext.Users.FirstOrDefault(u => u.Id == id); - if (updatingUser == null) - { - throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id)); - } - updatingUser.Username = user.Username; - updatingUser.Password = user.Password; - updatingUser.Email = user.Email; - updatingUser.IsAdmin = user.IsAdmin; - // Permet d'indiquer en Db que l'entité a été modifiée. - DbContext.Entry(updatingUser).State = EntityState.Modified; - DbContext.SaveChangesAsync(); - return updatingUser.FromEntityToModel().FromModelToDTO(); + var updatingUser = await dataServiceEF.UpdateUser(id, user); + return updatingUser.FromModelToDTO(); } - - public UserDTO CreateUser(string username, string password, string email, bool isAdmin) + public async Task CreateUser(string username, string password, string email, bool isAdmin) { - var newUserEntity = new UserDTO - { - Username = username, - Password = password, - Email = email, - IsAdmin = isAdmin - }; - DbContext.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity()); - DbContext.SaveChangesAsync(); - return newUserEntity; + var newUserEntity = await dataServiceEF.CreateUser(username, password, email, isAdmin); + return newUserEntity.FromModelToDTO(); } public IEnumerable GetInquiries(int page, int number)