Skip to content

Commit d339fe6

Browse files
committed
Delete operation successfully done
1 parent 41308ef commit d339fe6

File tree

7 files changed

+76
-62
lines changed

7 files changed

+76
-62
lines changed

InternSathi/src/main/java/internsathi/javaAssignment/controller/AdminRegistrationController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import org.springframework.http.HttpStatus;
77
import org.springframework.stereotype.Controller;
88
import org.springframework.ui.Model;
9-
import org.springframework.web.bind.annotation.ModelAttribute;
10-
import org.springframework.web.bind.annotation.PostMapping;
11-
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.*;
1210

1311
@Controller
1412
@RequestMapping("/internsathi/admin/")

InternSathi/src/main/java/internsathi/javaAssignment/controller/HomeDashboardController.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import internsathi.javaAssignment.entity.User;
55
import internsathi.javaAssignment.security.token.JwtTokenService;
66
import internsathi.javaAssignment.service.UserService;
7+
import jakarta.servlet.http.HttpServletRequest;
78
import jakarta.servlet.http.HttpServletResponse;
89
import lombok.extern.slf4j.Slf4j;
910
import org.springframework.security.core.Authentication;
1011
import org.springframework.security.core.GrantedAuthority;
1112
import org.springframework.stereotype.Controller;
1213
import org.springframework.ui.Model;
13-
import org.springframework.web.bind.annotation.GetMapping;
14-
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.*;
1515

1616
import java.security.Principal;
1717
import java.util.ArrayList;
@@ -31,30 +31,35 @@ public HomeDashboardController(JwtTokenService jwtTokenService, UserService user
3131
}
3232

3333
@GetMapping("/user/home")
34-
public String userHomePage(Model model, Authentication authentication, Principal principal, HttpServletResponse response) {
34+
public String userHomePage(Model model, Authentication authentication, Principal principal,
35+
HttpServletRequest request, HttpServletResponse response) {
3536
if (authentication.isAuthenticated()) {
36-
3737
List<String> authorities = authentication.getAuthorities()
3838
.stream()
3939
.map(GrantedAuthority::getAuthority)
4040
.toList();
41-
42-
String loggedInUser = (String) authentication.getPrincipal();
43-
log.info("logged In: .......{}", loggedInUser);
4441
String token;
45-
try {
42+
String loggedInUser = (String) authentication.getPrincipal();
43+
if (request.getHeader("Authorization") == null) {
4644

47-
token = jwtTokenService.generateToken(loggedInUser);
48-
response.addHeader("Authorization", "Bearer " + token);
49-
model.addAttribute("token", token);
50-
model.addAttribute("principal", principal.getName());
45+
log.info("logged In: .......{}", loggedInUser);
46+
try {
5147

52-
log.info("token {}", token);
53-
} catch (Exception e) {
54-
token = "1234";
55-
throw new RuntimeException(e);
56-
}
48+
token = jwtTokenService.generateToken(loggedInUser);
49+
model.addAttribute("token", token);
50+
model.addAttribute("principal", principal.getName());
51+
response.addHeader("Authorization", token);
52+
log.info("token {}", token);
5753

54+
} catch (Exception e) {
55+
token = "1234";
56+
throw new RuntimeException(e);
57+
}
58+
}
59+
if (request.getHeader("Authorization") != null) {
60+
token = request.getHeader("Authorization");
61+
response.addHeader("Authorization", token);
62+
}
5863
log.info("is Admin?...." + authorities.contains(Role.ADMIN.name()));
5964
if (authorities.contains(Role.ADMIN.name())) {
6065
List<User> userList = getAllUser();
@@ -65,6 +70,13 @@ public String userHomePage(Model model, Authentication authentication, Principal
6570
return "userHome";
6671
}
6772

73+
@PostMapping("/admin/deleteUserById")
74+
public String deleteUserById(Model model, @RequestParam(value = "userId", defaultValue = "0") Long userId) {
75+
userService.deleteUserById(userId);
76+
return "redirect:/internsathi/user/home";
77+
78+
}
79+
6880
private List<User> getAllUser() {
6981
return userService.getAllUser();
7082
}

InternSathi/src/main/java/internsathi/javaAssignment/mapper/UserMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ default User conversionFromRegistrationDtoToUser(UserRegistrationDto registratio
4646
.email(registrationDto.getEmail())
4747
.phoneNumber(registrationDto.getPhoneNumber())
4848
.password(registrationDto.getPassword())
49-
.role(registrationDto.getRole())
49+
.role(Role.USER.name())
5050
.build();
5151
}
5252

@@ -59,7 +59,7 @@ default User conversionFromRegistrationDtoToAdminUser(UserRegistrationDto regist
5959
.email(registrationDto.getEmail())
6060
.phoneNumber(registrationDto.getPhoneNumber())
6161
.password(registrationDto.getPassword())
62-
.role(Role.USER.name())
62+
.role(Role.ADMIN.name())
6363
.build();
6464

6565
}

InternSathi/src/main/java/internsathi/javaAssignment/service/UserService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ public interface UserService {
1515
void updatePassword(String username, String password);
1616

1717
List<User> getAllUser();
18+
19+
void deleteUserById(Long userId);
20+
1821
}

InternSathi/src/main/java/internsathi/javaAssignment/serviceImplementation/UserServiceImplementation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ public void updatePassword(String username, String password) {
7777
public List<User> getAllUser() {
7878
return userRepo.findAll();
7979
}
80+
81+
@Override
82+
public void deleteUserById(Long userId) {
83+
userRepo.deleteById(userId);
84+
}
8085
}

InternSathi/src/main/resources/templates/login.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ <h1>InternSathi</h1>
1212
</div>
1313
<form method="POST" th:object="${login}" class="login-form" th:action="@{/internsathi/user/login}">
1414
<h1>Login</h1>
15-
<div th:if="${param.error}">
16-
Invalid username and password.</div>
17-
<div th:if="${isOtpVerified}">
15+
<div class="error-message" th:if="${param.error}">
16+
Invalid username or password.</div>
17+
<div th:if="${isOtpVerified}" class="success-message">
1818
Your password is reset. Please login with your new password</div>
19-
<div th:if="${param.logout}">
19+
<div th:if="${param.logout}" class="success-message">
2020
You have been logged out.</div>
2121
<div class="form-group">
2222
<label for="username">Username</label>

InternSathi/src/main/resources/templates/userHome.html

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,70 +26,66 @@
2626
</header>
2727

2828
<main>
29-
<p>Welcome User, <h2 th:text="${principal}"></h2></p><br>
30-
<h4>Bearer Token: <h3 th:text="${token}"></h3></h4>
29+
<p>Welcome User, <h2 th:text="${principal}"></h2>
3130

32-
<div th:if="${admin}">
3331
<table>
3432
<thead>
3533
<h1>Perform Action: </h1>
3634
<tr>
3735
<th>User ID</th>
36+
<th>Name</th>
3837
<th>Username</th>
3938
<th>Email</th>
4039
<th>Actions</th>
4140
</tr>
4241
</thead>
43-
<tbody>
44-
<tr th:each="user, iterator : ${userList}">
45-
<td th:text="${user.id}"></td>
46-
<td th:text="${user.name}"></td>
47-
<td th:text="${user.email}"></td>
48-
<td>
49-
<button class="update-button" th:onclick="'updateUser(' + ${user.id} + ')'">Update</button>
50-
<button class="delete-button" th:onclick="'deleteUser(' + ${user.id} + ')'">Delete</button>
51-
</td>
42+
<div th:if="${admin}">
43+
<tbody>
44+
<tr th:each="user, iterator : ${userList}">
45+
<div th:unless="${principal == user.username}">
46+
<td th:text="${user.id}"></td>
47+
<td th:text="${user.name}"></td>
48+
<td th:text="${user.username}"></td>
49+
<td th:text="${user.email}"></td>
50+
<td>
51+
<form th:action="@{/internsathi/admin/deleteUserById}" th:method="post">
52+
<input type="hidden" name="userId" th:value="${user.id}" />
53+
<button class="delete-button" type="submit">Delete</button>
54+
</form>
55+
</td>
56+
</div>
57+
</tr>
58+
</tbody>
59+
</div>
60+
<div th:unless="${admin}">
61+
<tr>
62+
<td colspan="5">You do not have permission to perform action</td>
5263
</tr>
53-
</tbody>
64+
</div>
5465
</table>
55-
</div>
66+
5667
</main>
5768

5869
<footer class="footer">
5970
<div class="footer-content">
60-
Footer Content Here
71+
<h5>
72+
Please visit <a style="text-decoration: none" href="https://internsathi.com/">InternSathi.</a>
73+
</h5>
6174
</div>
6275
</footer>
6376
</body>
6477

6578
<script>
79+
80+
function storeTokenInLocalStorage(token) {
81+
localStorage.setItem("token", token);
82+
}
6683
function logout() {
6784
window.location.href = "/logout"
6885
}
6986

7087
function deleteUser(userId) {
7188
console.log("Deleting user with ID: " + userId);
72-
fetch(`/internsathi/admin/deleteUser?${userId}`, {
73-
method: 'DELETE',
74-
headers: {
75-
'Content-Type': 'application/json',
76-
},
77-
})
78-
.then(response => {
79-
if (!response.ok) {
80-
throw new Error('Network response was not ok');
81-
}
82-
// Handle the response from the server (optional)
83-
return response.json();
84-
})
85-
.then(data => {
86-
console.log('Delete request successful', data);
87-
// Refresh the page or update the table (optional)
88-
// window.location.reload();
89-
})
90-
.catch(error => {
91-
console.error('Error:', error);
92-
});
9389
}
9490

9591
function updateUser(userId) {

0 commit comments

Comments
 (0)