Skip to content

Commit 0f081d9

Browse files
committed
Remove user feature on app
1 parent cec58d9 commit 0f081d9

File tree

6 files changed

+61
-15
lines changed

6 files changed

+61
-15
lines changed

src/main/java/com/github/throyer/common/springboot/controllers/app/UserController.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
import com.github.throyer.common.springboot.domain.models.pagination.Page;
44
import com.github.throyer.common.springboot.domain.models.pagination.Pagination;
5+
import com.github.throyer.common.springboot.domain.models.shared.Type;
56
import com.github.throyer.common.springboot.domain.repositories.UserRepository;
6-
import com.github.throyer.common.springboot.domain.services.user.FindUserService;
7+
import com.github.throyer.common.springboot.domain.services.user.RemoveUserService;
78
import com.github.throyer.common.springboot.domain.services.user.dto.SearchUser;
9+
import com.github.throyer.common.springboot.utils.Toasts;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.data.domain.Sort;
1012
import org.springframework.security.access.prepost.PreAuthorize;
1113
import org.springframework.stereotype.Controller;
1214
import org.springframework.ui.Model;
1315
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.PathVariable;
17+
import org.springframework.web.bind.annotation.PostMapping;
1418
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1520

1621
@Controller
1722
@PreAuthorize("hasAnyAuthority('ADM')")
@@ -21,13 +26,25 @@ public class UserController {
2126
@Autowired
2227
private UserRepository repository;
2328

29+
@Autowired
30+
private RemoveUserService removeService;
31+
2432
@GetMapping
25-
public String idnex(Model model, Pagination pagination, Sort sort, SearchUser search) {
33+
public String index(Model model, Pagination pagination, Sort sort, SearchUser search) {
2634

2735
var page = Page.of(repository.findSimplifiedUsers(pagination.build()));
2836

2937
model.addAttribute("page", page);
3038

3139
return "app/users/index";
3240
}
41+
42+
@PostMapping("/delete/{id}")
43+
public String delete(@PathVariable Long id, RedirectAttributes redirect) {
44+
removeService.remove(id);
45+
46+
Toasts.add(redirect, "Usuário deletado com sucesso.", Type.SUCCESS);
47+
48+
return "redirect:/app/users";
49+
}
3350
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.modal-dialog {
2+
height: 90%;
3+
max-width: 385px;
4+
}
5+
6+
.modal-content {
7+
top: 40%;
8+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
console.log("gg")
1+
document.querySelector("#delete-confirm")
2+
.addEventListener("show.bs.modal", ({ target: modal, relatedTarget: button }) => {
3+
const { id, name } = button.dataset;
4+
5+
modal.querySelector("h5 > span").innerHTML = name;
6+
modal.querySelector("form").action = `/app/users/delete/${id}`;
7+
});
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
<div th:fragment="delete-confirm" class="modal fade" tabindex="-1" role="dialog" id="delete-confirm">
2-
<div class="modal-dialog" role="document">
2+
<div class="modal-sm modal-dialog" role="document">
33
<div class="modal-content rounded-4 shadow">
44
<div class="modal-body p-4 text-center">
5-
<h5 class="mb-0">Enable this setting?</h5>
6-
<p class="mb-0">You can always change your mind in your account settings.</p>
7-
</div>
8-
<div class="modal-footer flex-nowrap p-0">
9-
<button type="button"
10-
class="btn btn-lg btn-link fs-6 text-decoration-none col-6 m-0 rounded-0 border-right"><strong>Yes,
11-
enable</strong></button>
12-
<button type="button" class="btn btn-lg btn-link fs-6 text-decoration-none col-6 m-0 rounded-0"
13-
data-bs-dismiss="modal">No thanks</button>
5+
<h5 class="mb-0">Deseja remover <span>username</span>?</h5>
6+
<p class="mb-0">Caso o usuário seja deletado, esta ação não pode ser desfeita.</p>
147
</div>
8+
<form action="" method="post" class="modal-footer mb-0 flex-nowrap p-0">
9+
<button
10+
type="submit"
11+
class="btn btn-lg btn-link fs-6 text-decoration-none col-6 m-0 rounded-0 border-end">
12+
<strong>Sim, remover</strong>
13+
</button>
14+
<button
15+
type="button"
16+
data-bs-dismiss="modal"
17+
class="btn btn-lg btn-link fs-6 text-decoration-none col-6 m-0 rounded-0">
18+
Cancelar
19+
</button>
20+
</form>
1521
</div>
1622
</div>
1723
</div>

src/main/resources/templates/app/users/fragments/table.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
<td th:text="*{email}" class="align-middle">email</td>
1414
<td width="130" class="text-center">
1515
<div class="btn-group">
16-
<button type="button" class="btn-sm btn btn-outline-dark">
16+
<button
17+
type="button"
18+
class="btn-sm btn btn-outline-dark"
19+
data-bs-toggle="modal"
20+
data-bs-target="#delete-confirm"
21+
th:data-id="*{id}"
22+
th:data-name="*{name}">
1723
<i class="far fa-trash-alt"></i>
1824
</button>
1925
</div>

src/main/resources/templates/app/users/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
<layout th:replace="~{app/fragments/layout :: layout(~{::title}, ~{}, ~{::section}, ~{::scripts}, ~{::modal})}">
1+
<layout th:replace="~{app/fragments/layout :: layout(~{::title}, ~{::styles}, ~{::section}, ~{::scripts}, ~{::modal})}">
22
<title>Users</title>
3+
<styles>
4+
<link rel="stylesheet" type="text/css" th:href="@{/css/users.css}">
5+
</styles>
36
<section class="container">
47
<table
58
th:replace="~{__${execInfo.templateName}__/../fragments/table :: table(${page})}">

0 commit comments

Comments
 (0)