Skip to content

Commit

Permalink
Basic delete user functionality implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
surajcm committed Feb 27, 2024
1 parent 6315d9f commit 119044d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,18 @@ public String editUser(@PathVariable(name = "id") final Long id, final Model mod
}
return "redirect:/userManagement";
}

@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable(name = "id") final Long id,
final RedirectAttributes redirectAttributes) {
logger.info("Into the deleteUser method, id is {}", id);
try {
userService.delete(id);
redirectAttributes.addFlashAttribute("message", "The user ID " + id + " has been deleted successfully");
} catch (UserNotFoundException ex) {
redirectAttributes.addFlashAttribute("message", ex.getMessage());
}
return "redirect:/userManagement";
}
}

2 changes: 2 additions & 0 deletions src/main/java/com/quiz/darkhold/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public interface UserService {
Boolean isEmailUnique(Long id, String email);

User get(Long id) throws UserNotFoundException;

void delete(final Long id) throws UserNotFoundException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ public User get(final Long id) throws UserNotFoundException {
return userRepository.findById(id)
.orElseThrow(() -> new UserNotFoundException("Could not find any user with the id " + id));
}

@Override
public void delete(final Long id) throws UserNotFoundException {
var countById = userRepository.countById(id);
if (countById == null || countById == 0) {
throw new UserNotFoundException("Could not find any user with the id " + id);
}
userRepository.deleteById(id);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/user/usermanagement.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h2>Manage Users</h2>
<td>
<a class="fas fa-edit fa-2x icon-green" th:href="@{'/user/edit/' + ${user.id}}" title="Edit this user"></a>
&nbsp;
<a class="fas fa-trash fa-2x icon-dark" href=""></a>
<a class="fas fa-trash fa-2x icon-dark" th:href="@{'/user/delete/' + ${user.id}}" title="Delete this user"></a>
</td>
</tr>
</tbody>
Expand Down

0 comments on commit 119044d

Please sign in to comment.