Création DbUsersManager pour débuter relation User de API à EF
continuous-integration/drone/push Build is failing Details

Fait en binome avec Léni
pull/6/head
kekentin 4 weeks ago
parent 0b1c5b0208
commit 960a966f06

@ -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)

@ -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<Users>
{
private WTFContext _context;
private GenericRepository<Users> _repo;
public DbUsersManager( WTFContext context )
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
_repo = new GenericRepository<Users>(_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<int> CountUser()
{
return _repo.Count();
}
public async Task<bool> ExistEmail(string email)
{
throw new NotImplementedException();
}
public async Task<bool> ExistUsername(string username)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Users>> GetAllUser()
{
List<Users> users = _repo.GetItems(0,_repo.Count(),[nameof(Users.Images)]).ToList();
return new PaginationResult<Users>(users.Count,0,users.Count,users);
}
public async Task<string> GetHashPassword(string username)
{
throw new NotImplementedException();
}
public async Task<int> GetLastUserId()
{
PaginationResult<Users> 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<PaginationResult<Users>> GetSomeUser(int index, int pageSize)
{
List<Users> users = _repo.GetItems(index, pageSize, [nameof(Users.Images)]).ToList();
return new PaginationResult<Users>(users.Count, index, pageSize, users);
}
public async Task<Users> 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<Users> 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<Users> 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<Users> 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;
}
}
}

@ -24,6 +24,7 @@ namespace Contextlib
//public DbSet<RecordQuiz> records { get; set; }
public DbSet<Source> sources { get; set; }
public DbSet<Users> users { get; set; }
public DbSet<Admin> admins { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -74,6 +75,9 @@ namespace Contextlib
l => l.HasOne<Question>().WithMany().HasForeignKey(q => q.IdQuestion),
r => r.HasOne<Quiz>().WithMany().HasForeignKey(u => u.IdQuiz)
);
modelBuilder.Entity<Admin>()
.HasOne(a => a.User)
.WithOne(u => u.admin);
}
public WTFContext()

@ -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;
}

@ -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<DailyQuote> DailyQuotes { get; set; } = new List<DailyQuote>();
public ICollection<Commentary> Commentarys { get; set; } = new List<Commentary>();

@ -38,5 +38,10 @@ namespace Entity
public ICollection<Quote> Quotes { get; set; } = new List<Quote>();
public ICollection<Quote> Favorite { get; set; } = new List<Quote>();
[ForeignKey(nameof(Admin))]
public int Idadmin { get; set; }
public Admin admin { get; set; }
}
}

Loading…
Cancel
Save