From 1fc4affc8911bdaecd5551b2901bdcc6ae73de38 Mon Sep 17 00:00:00 2001 From: Nestisse Date: Tue, 5 Mar 2024 18:51:51 +0100 Subject: [PATCH] Modification du nom d'un projet --- .../DbDataManager.csproj} | 0 .../Service/InquiryDataService.cs | 23 +++++ .../DbDataManager/Service/UserDataService.cs | 90 ++++++++++++++++++ .../ModelToEntities/Mapper/BlackListMapper.cs | 39 -------- .../Mapper/ContentLessonMapper.cs | 34 ------- .../ModelToEntities/Mapper/InquiryMapper.cs | 39 -------- .../Mapper/InquiryTableMapper.cs | 39 -------- .../ModelToEntities/Mapper/LessonMapper.cs | 41 --------- .../ModelToEntities/Mapper/NotepadMapper.cs | 39 -------- .../ModelToEntities/Mapper/ParagraphMapper.cs | 38 -------- .../ModelToEntities/Mapper/SolutionMapper.cs | 38 -------- .../ModelToEntities/Mapper/SuccessMapper.cs | 39 -------- .../ModelToEntities/Mapper/UserMapper.cs | 38 -------- .../Service/InquiryDataService.cs | 23 ----- .../Service/UserDataService.cs | 92 ------------------- 15 files changed, 113 insertions(+), 499 deletions(-) rename API_SQLuedo/{ModelToEntities/ModelToEntities.csproj => DbDataManager/DbDataManager.csproj} (100%) create mode 100644 API_SQLuedo/DbDataManager/Service/InquiryDataService.cs create mode 100644 API_SQLuedo/DbDataManager/Service/UserDataService.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/BlackListMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/ContentLessonMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/InquiryMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/InquiryTableMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/LessonMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/NotepadMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/ParagraphMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/SolutionMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/SuccessMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Mapper/UserMapper.cs delete mode 100644 API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs delete mode 100644 API_SQLuedo/ModelToEntities/Service/UserDataService.cs diff --git a/API_SQLuedo/ModelToEntities/ModelToEntities.csproj b/API_SQLuedo/DbDataManager/DbDataManager.csproj similarity index 100% rename from API_SQLuedo/ModelToEntities/ModelToEntities.csproj rename to API_SQLuedo/DbDataManager/DbDataManager.csproj diff --git a/API_SQLuedo/DbDataManager/Service/InquiryDataService.cs b/API_SQLuedo/DbDataManager/Service/InquiryDataService.cs new file mode 100644 index 0000000..e57ca27 --- /dev/null +++ b/API_SQLuedo/DbDataManager/Service/InquiryDataService.cs @@ -0,0 +1,23 @@ +using Dto; +using Entities; +using Shared; + +namespace DbDataManager.Service; + +public class InquiryDataService : IInquiryService +{ + public IEnumerable GetInquiries(int page, int number) + { + throw new NotImplementedException(); + } + + public InquiryEntity GetInquiryById(int id) + { + throw new NotImplementedException(); + } + + public InquiryEntity GetInquiryByTitle(string title) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/API_SQLuedo/DbDataManager/Service/UserDataService.cs b/API_SQLuedo/DbDataManager/Service/UserDataService.cs new file mode 100644 index 0000000..e9d32ce --- /dev/null +++ b/API_SQLuedo/DbDataManager/Service/UserDataService.cs @@ -0,0 +1,90 @@ +using DbContextLib; +using Entities; +using Microsoft.EntityFrameworkCore; +using Shared; + +namespace DbDataManager.Service; + +public class UserDataService : IUserService +{ + private UserDbContext DbContext { get; set; } + + public UserDataService(UserDbContext context) + { + DbContext = context; + context.Database.EnsureCreated(); + } + + public UserEntity 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; + } + + public UserEntity 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; + } + + public IEnumerable GetUsers(int page, int number) + { + return DbContext.Users.Skip((page - 1) * number).Take(number).ToList() + .Select(u => u); + } + + public bool 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; + } + + public UserEntity UpdateUser(int id, UserEntity 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; + } + + public UserEntity CreateUser(string username, string password, string email, bool isAdmin) + { + var newUserEntity = new UserEntity() + { + Username = username, + Password = password, + Email = email, + IsAdmin = isAdmin + }; + DbContext.Users.Add(newUserEntity); + DbContext.SaveChangesAsync(); + return newUserEntity; + } +} \ No newline at end of file diff --git a/API_SQLuedo/ModelToEntities/Mapper/BlackListMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/BlackListMapper.cs deleted file mode 100644 index 0eb2392..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/BlackListMapper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class BlackListMapper - { - public static BlackListDTO FromModelToDTO(BlackList model) - { - return new BlackListDTO(model.Email, model.ExpirationDate); - } - - public static BlackListDTO FromEntityToDTO(BlackListEntity ent) - { - return new BlackListDTO(ent.Email, ent.ExpirationDate); - } - - public static BlackList FromDTOToModel(BlackListDTO dto) - { - return new BlackList(dto.Email, dto.ExpirationDate); - } - - public static BlackList FromEntityToModel(BlackListEntity ent) - { - return new BlackList(ent.Email, ent.ExpirationDate); - } - - public static BlackListEntity FromDTOToEntity(BlackListDTO dto) - { - return new BlackListEntity(dto.Email, dto.ExpirationDate); - } - - public static BlackListEntity FromModelToEntity(BlackList model) - { - return new BlackListEntity(model.Email, model.ExpirationDate); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/ContentLessonMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/ContentLessonMapper.cs deleted file mode 100644 index 1cff6d4..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/ContentLessonMapper.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class ContentLessonMapper - { - public static ContentLesson FromDTOToModel(this ContentLessonDTO dto) - { - return new ContentLesson(dto.LessonId, dto.LessonPartId); - } - - public static ContentLesson FromEntityToModel(this ContentLessonEntity ent) - { - return new ContentLesson(ent.LessonId, ent.LessonPartId); - } - - public static ContentLessonDTO FromModelToDTO(this ContentLesson les) - { - return new ContentLessonDTO(les.LessonId, les.LessonPartId); - } - - public static ContentLessonDTO FromEntityToDTO(this ContentLessonEntity ent) - { - return new ContentLessonDTO(ent.LessonId, ent.LessonPartId); - } - - public static ContentLessonEntity FromModelToEntity(this ContentLesson les) - { - return new ContentLessonEntity(les.LessonId,new LessonEntity(les.LessonId),les.LessonPartId,new ParagraphEntity(les.LessonPartId)); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/InquiryMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/InquiryMapper.cs deleted file mode 100644 index 5fc0fe4..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/InquiryMapper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class InquiryMapper - { - public static Inquiry FromDTOToModel(this InquiryDTO InqDto) - { - return new Inquiry(InqDto.Id,InqDto.Title,InqDto.Description,InqDto.IsUser,InqDto.Database,InqDto.InquiryTable); - } - - public static Inquiry FromEntityToModel(this InquiryEntity InqEntity) - { - return new Inquiry(InqEntity.Id, InqEntity.Title, InqEntity.Description, InqEntity.IsUser, InqEntity.Database.OwnerId, InqEntity.InquiryTable.OwnerId); - } - - - public static InquiryEntity FromModelToEntity(this Inquiry Inq) - { - return new InquiryEntity(Inq.Id, Inq.Title, Inq.Description, Inq.IsUser,new InquiryTableEntity(Inq.Database),new SolutionEntity(Inq.InquiryTable)); - } - public static InquiryEntity FromDTOToEntity(this InquiryDTO InqDto) - { - return new InquiryEntity(InqDto.Id, InqDto.Title, InqDto.Description, InqDto.IsUser, new InquiryTableEntity(InqDto.Database), new SolutionEntity(InqDto.InquiryTable)); - } - public static InquiryDTO FromModelToDTO(this Inquiry Inq) - { - return new InquiryDTO(Inq.Id, Inq.Title, Inq.Description, Inq.IsUser, Inq.Database, Inq.InquiryTable); - } - - public static InquiryDTO FromEntityToDTO(this InquiryEntity InqEntity) - { - return new InquiryDTO(InqEntity.Id, InqEntity.Title, InqEntity.Description, InqEntity.IsUser, InqEntity.Database.OwnerId, InqEntity.InquiryTable.OwnerId); - - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/InquiryTableMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/InquiryTableMapper.cs deleted file mode 100644 index 5f01925..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/InquiryTableMapper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class InquiryTableMapper - { - public static InquiryTable FromDTOToModel(this InquiryTableDTO InqTDto) - { - return new InquiryTable(InqTDto.OwnerId, InqTDto.ConnectionInfo, InqTDto.DatabaseName); - } - - public static InquiryTable FromEntityToModel(this InquiryTableEntity InqTEntity) - { - return new InquiryTable(InqTEntity.OwnerId, InqTEntity.ConnectionInfo, InqTEntity.DatabaseName); - } - - public static InquiryTableDTO FromModelToDTO(this InquiryTable InqT) - { - return new InquiryTableDTO(InqT.OwnerId, InqT.DatabaseName, InqT.ConnectionInfo); - } - - public static InquiryTableDTO FromEntityToDTO(this InquiryTableEntity InqTEntity) - { - return new InquiryTableDTO(InqTEntity.OwnerId, InqTEntity.DatabaseName, InqTEntity.ConnectionInfo); - } - - public static InquiryTableEntity FromModelToEntity(this InquiryTable InqT) - { - return new InquiryTableEntity(InqT.OwnerId, InqT.DatabaseName, InqT.ConnectionInfo); - } - - public static InquiryTableEntity FromDTOToEntity(this InquiryTableDTO dto) - { - return new InquiryTableEntity(dto.OwnerId, dto.DatabaseName, dto.ConnectionInfo); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/LessonMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/LessonMapper.cs deleted file mode 100644 index 9b22858..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/LessonMapper.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class LessonMapper - { - - public static LessonDTO FromModelToDTO(Lesson model) - { - return new LessonDTO(model.Id, model.Title, model.LastPublisher, model.LastEdit); - } - - public static LessonDTO FromEntityToDTO(LessonEntity model) - { - return new LessonDTO(model.Id, model.Title, model.LastPublisher, model.LastEdit); - } - - public static LessonEntity FromModelToEntity(Lesson model) - { - return new LessonEntity(model.Id, model.Title, model.LastPublisher, model.LastEdit); - } - - public static LessonEntity FromDTOToEntity(LessonDTO dto) - { - return new LessonEntity(dto.Id, dto.Title, dto.LastPublisher, dto.LastEdit); - } - - public static Lesson FromDTOToModel(LessonDTO dto) - { - return new Lesson(dto.Id, dto.Title, dto.LastPublisher, dto.LastEdit); - } - - public static Lesson FromEntityToModel(LessonEntity entity) - { - return new Lesson(entity.Id, entity.Title, entity.LastPublisher, entity.LastEdit); - } - } -} - \ No newline at end of file diff --git a/API_SQLuedo/ModelToEntities/Mapper/NotepadMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/NotepadMapper.cs deleted file mode 100644 index ecb7013..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/NotepadMapper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class NotepadMapper - { - public static Notepad FromDTOToModel(this NotepadDTO dto) - { - return new Notepad(dto.Id, dto.UserId, dto.InquiryId, dto.Notes); - } - - public static Notepad FromEntityToModel(this NotepadEntity ent) - { - return new Notepad(ent.Id, ent.UserId, ent.InquiryId, ent.Notes); - } - - public static NotepadDTO FromModelToDTO(this Notepad not) - { - return new NotepadDTO(not.Id, not.UserId, not.InquiryId, not.Notes); - } - - public static NotepadDTO FromEntityToDTO(this NotepadEntity ent) - { - return new NotepadDTO(ent.Id, ent.UserId, ent.InquiryId, ent.Notes); - } - - public static NotepadEntity FromDTOToEntity(this NotepadDTO dto) - { - return new NotepadEntity(dto.Id,new UserEntity(dto.UserId), dto.UserId, new InquiryEntity(dto.InquiryId), dto.Notes); - } - - public static NotepadEntity FromModelToEntity(this Notepad not) - { - return new NotepadEntity(not.Id, new UserEntity(not.UserId), not.UserId, new InquiryEntity(not.InquiryId), not.Notes); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/ParagraphMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/ParagraphMapper.cs deleted file mode 100644 index 72ddb9c..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/ParagraphMapper.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class ParagraphMapper - { - public static Paragraph FromDTOToModel(ParagraphDTO dto) - { - return new Paragraph(dto.Id,dto.Title,dto.Content,dto.Info,dto.Query,dto.Comment); - } - - public static Paragraph FromEntityToModel(ParagraphEntity model) - { - return new Paragraph(model.Id, model.Title, model.Content, model.Info, model.Query, model.Comment); - } - public static ParagraphDTO FromEntityToDTO(ParagraphEntity model) - { - return new ParagraphDTO(model.Id, model.Title, model.Content, model.Info, model.Query, model.Comment); - } - - public static ParagraphDTO FromModelToDTO(Paragraph model) - { - return new ParagraphDTO(model.Id, model.Title, model.Content, model.Info, model.Query, model.Comment); - } - - public static ParagraphEntity FromDTOToEntity(ParagraphDTO dto) - { - return new ParagraphEntity(dto.Id, dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment); - } - - public static Paragraph FromModelToEntity(Paragraph model) - { - return new Paragraph(model.Id, model.Title, model.Content, model.Info, model.Query, model.Comment); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/SolutionMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/SolutionMapper.cs deleted file mode 100644 index cad2024..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/SolutionMapper.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class SolutionMapper - { - public static Solution FromDTOToModel(this SolutionDTO dto) - { - return new Solution(dto.OwnerId,dto.MurdererFirstName,dto.MurdererLastName,dto.MurderPlace,dto.MurderWeapon,dto.Explanation); - } - - public static Solution FromEntityToModel(this SolutionEntity entity) - { - return new Solution(entity.OwnerId, entity.MurdererFirstName, entity.MurdererLastName, entity.MurderPlace, entity.MurderWeapon, entity.Explanation); - } - public static SolutionDTO FromModelToDTO(this Solution model) - { - return new SolutionDTO(model.OwnerId, model.MurdererFirstName, model.MurdererLastName, model.MurderPlace, model.MurderWeapon, model.Explanation); - } - - public static SolutionDTO FromEntityToDTO(this SolutionEntity entity) - { - return new SolutionDTO(entity.OwnerId, entity.MurdererFirstName, entity.MurdererLastName, entity.MurderPlace, entity.MurderWeapon, entity.Explanation); - } - - public static SolutionEntity FromModelToEntity(this Solution model) - { - return new SolutionEntity(model.OwnerId, new InquiryEntity(model.OwnerId), model.MurdererFirstName, model.MurdererLastName, model.MurderPlace, model.MurderWeapon, model.Explanation); - } - - public static SolutionEntity FromDTOToEntity(this SolutionDTO dto) - { - return new SolutionEntity(dto.OwnerId, new InquiryEntity(dto.OwnerId), dto.MurdererFirstName, dto.MurdererLastName, dto.MurderPlace, dto.MurderWeapon, dto.Explanation); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/SuccessMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/SuccessMapper.cs deleted file mode 100644 index 935689a..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/SuccessMapper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities -{ - public static class SuccessMapper - { - public static Success FromDTOToModel(this SuccessDTO dto) - { - return new Success(dto.UserId, dto.InquiryId, dto.IsFinished); - } - - public static Success FromEntityToModel(this SuccessEntity ent) - { - return new Success(ent.UserId, ent.InquiryId, ent.IsFinished); - } - - public static SuccessDTO FromModelToDTO(this Success suc) - { - return new SuccessDTO(suc.UserId, suc.InquiryId, suc.IsFinished); - } - - public static SuccessDTO FromEntityToDTO(this SuccessEntity ent) - { - return new SuccessDTO(ent.UserId, ent.InquiryId, ent.IsFinished); - } - - public static SuccessEntity FromDTOToEntity(this SuccessDTO dto) - { - return new SuccessEntity(dto.UserId,new UserEntity(dto.UserId),dto.InquiryId,new InquiryEntity(dto.InquiryId),dto.IsFinished); - } - - public static SuccessEntity FromModelToEntity(this Success suc) - { - return new SuccessEntity(suc.UserId, new UserEntity(suc.UserId), suc.InquiryId, new InquiryEntity(suc.InquiryId), suc.IsFinished); - } - } -} diff --git a/API_SQLuedo/ModelToEntities/Mapper/UserMapper.cs b/API_SQLuedo/ModelToEntities/Mapper/UserMapper.cs deleted file mode 100644 index c570909..0000000 --- a/API_SQLuedo/ModelToEntities/Mapper/UserMapper.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Dto; -using Entities; -using ModelToEntities.Business; - -namespace ModelToEntities.Service -{ - public static class UserMapper - { - public static User FromDTOToModel(this UserDTO dto) - { - return new User(dto.Id, dto.Username, dto.Password, dto.Email, dto.IsAdmin); - } - public static UserEntity FromDTOToEntity(this UserDTO dto) - { - return new UserEntity(dto.Id, dto.Username, dto.Password, dto.Email, dto.IsAdmin); - } - - public static User FromEntityToModel(this UserEntity entity) - { - return new User(entity.Id, entity.Username, entity.Password, entity.Email, entity.IsAdmin); - } - public static UserDTO FromEntityToDTO(this UserEntity entity) - { - return new UserDTO(entity.Id, entity.Username, entity.Password, entity.Email, entity.IsAdmin); - } - - public static UserDTO FromModelToDTO(this User user) - { - return new UserDTO(user.Id, user.Username, user.Password, user.Email, user.IsAdmin); - } - - public static UserEntity FromModelToEntity(this User user) - { - return new UserEntity(user.Id, user.Username, user.Password, user.Email, user.IsAdmin); - } - - } -} diff --git a/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs b/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs deleted file mode 100644 index 4b76513..0000000 --- a/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Dto; -using ModelToEntities.Business; -using Shared; - -namespace ModelToEntities.Service; - -public class InquiryDataService : IInquiryService -{ - 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(); - } -} \ No newline at end of file diff --git a/API_SQLuedo/ModelToEntities/Service/UserDataService.cs b/API_SQLuedo/ModelToEntities/Service/UserDataService.cs deleted file mode 100644 index 1ad41d2..0000000 --- a/API_SQLuedo/ModelToEntities/Service/UserDataService.cs +++ /dev/null @@ -1,92 +0,0 @@ -using DbContextLib; -using Dto; -using Entities; -using Microsoft.EntityFrameworkCore; -using Shared; - -namespace ModelToEntities.Service -{ - public class UserDataService : IUserService - { - private UserDbContext DbContext { get; set; } - - public UserDataService(UserDbContext context) - { - DbContext = context; - context.Database.EnsureCreated(); - } - - public UserDTO 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(); - } - - public UserDTO 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(); - } - - public IEnumerable GetUsers(int page, int number) - { - return DbContext.Users.Skip((page - 1) * number).Take(number).ToList() - .Select(u => u.FromEntityToModel().FromModelToDTO()); - } - - public bool 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; - } - - public UserDTO 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(); - } - - public UserDTO CreateUser(string username, string password, string email, bool isAdmin) - { - var newUserEntity = new UserEntity() - { - Username = username, - Password = password, - Email = email, - IsAdmin = isAdmin - }; - DbContext.Users.Add(newUserEntity); - DbContext.SaveChangesAsync(); - return newUserEntity.FromEntityToDTO(); - } - } -} \ No newline at end of file