public ActionResult Delete([FromBody] UserControllerRootDelete payload) { // Check if payload is present if (payload == null) { return(BadRequest("UserController (POST) - Missing payload.")); } try { _userService.DeleteUser(payload); return(Ok()); } catch (InvalidCastException e) { // Return 422 if we can't parse the user identifier as GUID return(UnprocessableEntity(e.Message)); } catch (InvalidDataException e) { // Return 400 if the user identifier is ambiguous return(BadRequest(e.Message)); } catch (InvalidOperationException e) { // Return 409 if more than one deletion was performed return(Conflict(e.Message)); } catch (NoNullAllowedException e) { // Return 400 if no deletion was performed return(BadRequest(e.Message)); } catch (KeyNotFoundException e) { // Return 404 if we couldn't find the user we wanted to delete return(NotFound(e.Message)); } catch (Exception e) { // Return 500 if any other exception occurred return(Problem(e.Message, e.Source, 500, "UserController (DELETE)", e.GetType().ToString())); } }
public void DeleteUser(UserControllerRootDelete payload) { // Validate Payload DataValidator.ValidateField(nameof(payload.UserIdentifier), payload.UserIdentifier); DataValidator.ValidateGuid(payload.UserIdentifier); // Delete User var deletionResult = _storageService.DeleteUser(payload.UserIdentifier); // Check Deletion Result if (deletionResult.DeletedCount == 0) { throw new NoNullAllowedException("UserService (DeleteUser) - No deletions were performed."); } if (deletionResult.DeletedCount > 1) { throw new InvalidOperationException("UserService (DeleteUser) - More than one user was deleted."); } }
public async Task ShouldDeleteUser() { // Arrange var payload = new UserControllerRootDelete { UserIdentifier = "197231f1-166d-4a3e-8a83-99dce27ff68c" }; // Prepare DELETE Request var deleteUserRequest = new HttpRequestMessage(HttpMethod.Delete, "api/User") { Content = Shared.FormatContent <UserControllerRootDelete>(payload) }; // Act // Create User first await Shared.CreateUser(Client, payload); // Delete User var deleteUserResponse = await Client.SendAsync(deleteUserRequest); // Assert Assert.Equal(HttpStatusCode.OK, deleteUserResponse.StatusCode); }