Débuggage du CRUD pour pouvoir faire des tests dessus

pull/23/head
Johnny RATTON 1 year ago
parent 00cc2e3a2d
commit d47098ab37

@ -82,11 +82,15 @@ namespace API.Controllers
} }
[HttpPost] [HttpPost]
public IActionResult CreateUser(string username, string password, string email, bool isAdmin) public IActionResult CreateUser([FromBody]UserDTO dto)
{ {
if (dto.Username == null || dto.Password == null || dto.Email == null)
{
return BadRequest();
}
// return Ok(_userDataService.CreateUser(username, password, email, isAdmin)); // return Ok(_userDataService.CreateUser(username, password, email, isAdmin));
_logger.LogInformation("[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", username, password, email, isAdmin); _logger.LogInformation("[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", dto.Username, dto.Password, dto.Email, dto.IsAdmin);
return CreatedAtAction(nameof(GetUsers), _userDataService.CreateUser(username, password, email, isAdmin)); return Created(nameof(GetUsers), _userDataService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin));
} }
[HttpPut] [HttpPut]

@ -54,13 +54,13 @@ namespace Services
return false; return false;
} }
DbContext.Users.Remove(userEntity); DbContext.Users.Remove(userEntity);
DbContext.SaveChanges(); DbContext.SaveChangesAsync();
return true; return true;
} }
public UserDTO UpdateUser(int id, UserDTO user) public UserDTO UpdateUser(int id, UserDTO user)
{ {
var updatingUser = GetUserById(id); var updatingUser = DbContext.Users.FirstOrDefault(u => u.Id == id);
if (updatingUser == null) if (updatingUser == null)
{ {
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id)); throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id));
@ -69,11 +69,10 @@ namespace Services
updatingUser.Password = user.Password; updatingUser.Password = user.Password;
updatingUser.Email = user.Email; updatingUser.Email = user.Email;
updatingUser.IsAdmin = user.IsAdmin; updatingUser.IsAdmin = user.IsAdmin;
var updatedUser = updatingUser.FromDTOToModel().FromModelToEntity();
// Permet d'indiquer en Db que l'entité a été modifiée. // Permet d'indiquer en Db que l'entité a été modifiée.
DbContext.Entry(updatedUser).State = EntityState.Modified; DbContext.Entry(updatingUser).State = EntityState.Modified;
return updatedUser.FromEntityToModel().FromModelToDTO(); DbContext.SaveChangesAsync();
return updatingUser.FromEntityToModel().FromModelToDTO();
} }
public UserDTO CreateUser(string username, string password, string email, bool isAdmin) public UserDTO CreateUser(string username, string password, string email, bool isAdmin)
@ -86,7 +85,7 @@ namespace Services
IsAdmin = isAdmin IsAdmin = isAdmin
}; };
DbContext.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity()); DbContext.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity());
DbContext.SaveChanges(); DbContext.SaveChangesAsync();
return newUserEntity; return newUserEntity;
} }

Loading…
Cancel
Save