From 960a966f06fc50a923c1d3c7f73a40af2be6c83b Mon Sep 17 00:00:00 2001 From: kekentin Date: Thu, 27 Mar 2025 11:59:54 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20DbUsersManager=20pour=20d=C3=A9?= =?UTF-8?q?buter=20relation=20User=20de=20API=20=C3=A0=20EF=20Fait=20en=20?= =?UTF-8?q?binome=20avec=20L=C3=A9ni?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WF_EF_Api/Contextlib/DbCharacterManager.cs | 2 +- WF_EF_Api/Contextlib/DbUsersManager.cs | 154 +++++++++++++++++++++ WF_EF_Api/Contextlib/WTFContext.cs | 4 + WF_EF_Api/Dto2Entities/Extention.cs | 4 +- WF_EF_Api/Entity/Quote.cs | 6 - WF_EF_Api/Entity/Users.cs | 5 + 6 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 WF_EF_Api/Contextlib/DbUsersManager.cs diff --git a/WF_EF_Api/Contextlib/DbCharacterManager.cs b/WF_EF_Api/Contextlib/DbCharacterManager.cs index 9b36686..e81dc1d 100644 --- a/WF_EF_Api/Contextlib/DbCharacterManager.cs +++ b/WF_EF_Api/Contextlib/DbCharacterManager.cs @@ -121,7 +121,7 @@ namespace Contextlib public async Task UpdateCharacter(int id, Character character) { Character? charac = _repo.GetById(id); - if (charac != null && charac != null) + if (charac != null) { bool change = false; if (character.IdImage != 0) diff --git a/WF_EF_Api/Contextlib/DbUsersManager.cs b/WF_EF_Api/Contextlib/DbUsersManager.cs new file mode 100644 index 0000000..d816444 --- /dev/null +++ b/WF_EF_Api/Contextlib/DbUsersManager.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Entity; +using Shared; + +namespace Contextlib +{ + public class DbUsersManager : IUserService + { + private WTFContext _context; + private GenericRepository _repo; + + public DbUsersManager( WTFContext context ) + { + _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); + _repo = new GenericRepository(_context); + } + + public async Task AddUser(Users user) + { + if (user == null) { + throw new ArgumentNullException(nameof(user), "user cannot be null."); + } + _repo.Insert(user); + await _context.SaveChangesAsync(); + } + + public async Task CountUser() + { + return _repo.Count(); + } + + public async Task ExistEmail(string email) + { + throw new NotImplementedException(); + } + + public async Task ExistUsername(string username) + { + throw new NotImplementedException(); + } + + public async Task> GetAllUser() + { + List users = _repo.GetItems(0,_repo.Count(),[nameof(Users.Images)]).ToList(); + return new PaginationResult(users.Count,0,users.Count,users); + } + + public async Task GetHashPassword(string username) + { + throw new NotImplementedException(); + } + + public async Task GetLastUserId() + { + PaginationResult users = await GetAllUser(); + int lastUserId = 0; + foreach (Users user in users.items) + { + if (user.Id >= lastUserId) + { + lastUserId = user.Id + 1; + } + } + return lastUserId; + } + + public async Task> GetSomeUser(int index, int pageSize) + { + List users = _repo.GetItems(index, pageSize, [nameof(Users.Images)]).ToList(); + return new PaginationResult(users.Count, index, pageSize, users); + } + + public async Task GetUserByEmail(string email) + { + var user = _repo.GetItems(item => item.Email == email, 0, 1, [nameof(Character.Images)]).FirstOrDefault(); + + if (user == null) + { + throw new KeyNotFoundException($"Error : No user found with the email: {email}."); + } + + return user; + } + + public async Task GetUserById(int id) + { + Users? user =_repo.GetById(id, item => item.Id == id, nameof(Users.Images)); + if (user == null) + { + throw new KeyNotFoundException($"Error : No users found with the ID: {id}."); + } + return user; + } + + public async Task GetUserByUsername(string username) + { + var user = _repo.GetItems(item => item.UserName == username, 0, 1, [nameof(Character.Images)]).FirstOrDefault(); + + if (user == null) + { + throw new KeyNotFoundException($"Error : No user found with the name: {username}."); + } + + return user; + } + + public async Task RemoveUser(Users user) + { + _repo.Delete(user); + await _context.SaveChangesAsync(); + } + + public async Task SetAdminRole(bool isAdmin) + { + throw new NotImplementedException(); + } + + public async Task UpdateUser(int userId, Users user) + { + Users? u = _repo.GetById(userId); + if (u != null) + { + bool change = false; + if (user.IdImage != 0) + { + u.IdImage = user.IdImage; + change = true; + } + if (user.UserName != null) + { + u.UserName = user.UserName; + change = true; + } + if (user.Email != null) + { + u.Email = user.Email; + change = true; + } + if (user.Password != null) + { + u.Password = user.Password; + change = true; + } + _repo.Update(u); + if (change)_context.SaveChanges(); + } + return u; + } + } +} diff --git a/WF_EF_Api/Contextlib/WTFContext.cs b/WF_EF_Api/Contextlib/WTFContext.cs index 4b95f3b..2706b88 100644 --- a/WF_EF_Api/Contextlib/WTFContext.cs +++ b/WF_EF_Api/Contextlib/WTFContext.cs @@ -24,6 +24,7 @@ namespace Contextlib //public DbSet records { get; set; } public DbSet sources { get; set; } public DbSet users { get; set; } + public DbSet admins { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -74,6 +75,9 @@ namespace Contextlib l => l.HasOne().WithMany().HasForeignKey(q => q.IdQuestion), r => r.HasOne().WithMany().HasForeignKey(u => u.IdQuiz) ); + modelBuilder.Entity() + .HasOne(a => a.User) + .WithOne(u => u.admin); } public WTFContext() diff --git a/WF_EF_Api/Dto2Entities/Extention.cs b/WF_EF_Api/Dto2Entities/Extention.cs index 62ed743..fb300ef 100644 --- a/WF_EF_Api/Dto2Entities/Extention.cs +++ b/WF_EF_Api/Dto2Entities/Extention.cs @@ -28,8 +28,8 @@ namespace Dto2Entities commentary.Id = item.Id; commentary.Date = item.DateCommentary; commentary.Comment = item.Comment; - commentary.User = item.Users.UserName; - commentary.ImagePath = item.Users.Images.ImgPath; + commentary.User = item.User.UserName; + commentary.ImagePath = item.User.Images.ImgPath; return commentary; } diff --git a/WF_EF_Api/Entity/Quote.cs b/WF_EF_Api/Entity/Quote.cs index 68b12c1..37d5f00 100644 --- a/WF_EF_Api/Entity/Quote.cs +++ b/WF_EF_Api/Entity/Quote.cs @@ -46,12 +46,6 @@ namespace Entity public Character Character { get; set; } = null!; - public Users? User { get; set; } = null!; - - public Source Source { get; set; } = null!; - - public Character Character { get; set; } = null!; - public ICollection DailyQuotes { get; set; } = new List(); public ICollection Commentarys { get; set; } = new List(); diff --git a/WF_EF_Api/Entity/Users.cs b/WF_EF_Api/Entity/Users.cs index e7a8101..6b834eb 100644 --- a/WF_EF_Api/Entity/Users.cs +++ b/WF_EF_Api/Entity/Users.cs @@ -38,5 +38,10 @@ namespace Entity public ICollection Quotes { get; set; } = new List(); public ICollection Favorite { get; set; } = new List(); + + + [ForeignKey(nameof(Admin))] + public int Idadmin { get; set; } + public Admin admin { get; set; } } }