|
|
@ -1,92 +0,0 @@
|
|
|
|
using DbContextLib;
|
|
|
|
|
|
|
|
using Dto;
|
|
|
|
|
|
|
|
using Entities;
|
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
using Shared;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ModelToEntities.Service
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public class UserDataService : IUserService<UserDTO>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
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<UserDTO> 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|