🚧 Modification du service User
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
4eee15fa4d
commit
1cff605b0d
@ -1,121 +0,0 @@
|
|||||||
using Dto;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Entities;
|
|
||||||
using Shared.Mapper;
|
|
||||||
using DbContextLib;
|
|
||||||
using Model.OrderCriteria;
|
|
||||||
|
|
||||||
namespace Shared
|
|
||||||
{
|
|
||||||
public class UserDataService : IUserDataService
|
|
||||||
{
|
|
||||||
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.FromEntityToDTO();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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.FromEntityToDTO();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<UserDTO> GetUsers(int page, int number, UserOrderCriteria orderCriteria)
|
|
||||||
{
|
|
||||||
IQueryable<UserEntity> query = DbContext.Users.Skip((page - 1) * number).Take(number);
|
|
||||||
switch (orderCriteria)
|
|
||||||
{
|
|
||||||
case UserOrderCriteria.None:
|
|
||||||
break;
|
|
||||||
case UserOrderCriteria.ById:
|
|
||||||
query = query.OrderBy(s => s.Id);
|
|
||||||
break;
|
|
||||||
case UserOrderCriteria.ByUsername:
|
|
||||||
query = query.OrderBy(s => s.Username);
|
|
||||||
break;
|
|
||||||
case UserOrderCriteria.ByEmail:
|
|
||||||
query = query.OrderBy(s => s.Email);
|
|
||||||
break;
|
|
||||||
case UserOrderCriteria.ByIsAdmin:
|
|
||||||
query = query.OrderBy(s => s.IsAdmin);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
var users = query.ToList();
|
|
||||||
return users.Select(s => s.FromEntityToDTO());
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
DbContext.SaveChangesAsync();
|
|
||||||
return updatingUser.FromEntityToDTO();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserDTO 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.FromDTOToEntity());
|
|
||||||
DbContext.SaveChangesAsync();
|
|
||||||
return newUserEntity;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue