Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.alwinsimon.UserManagementJavaSpringBoot.Controller;

import com.alwinsimon.UserManagementJavaSpringBoot.Model.User;
import com.alwinsimon.UserManagementJavaSpringBoot.Service.AdminService;
import com.alwinsimon.UserManagementJavaSpringBoot.Service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/admin")
@RequiredArgsConstructor
@CrossOrigin("*")
public class AdminController {

private final AdminService adminService;
@GetMapping("/get-users")
public ResponseEntity<List<User>> getAllUsers() {

// API Endpoint to get the LoggedIn User Details using Token received in the Request Header.
List<User> users = adminService.getAllUsers();
return ResponseEntity.ok(users);

}

@DeleteMapping("/delete-user/{email}")
public ResponseEntity<String> deleteUser(@PathVariable("email")String email){
try {
adminService.deleteUserByEmail(email);
return ResponseEntity.ok("User deleted.");
}catch (Exception e){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User deletion Failed.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alwinsimon.UserManagementJavaSpringBoot.Controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,18 +18,19 @@
public class GeneralController {

@GetMapping("/")
public Map<String, String> getServerStatus() {

// Get the current date and time in UTC
public ResponseEntity<Map<String, Object>> getServerStatus() {
ZonedDateTime currentDateTimeUtc = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy, hh:mm:ss a");
String formattedDateTime = currentDateTimeUtc.format(formatter);

// Create a Map for the response
Map<String, String> response = new HashMap<>();
response.put("status", "SERVER and Systems are Up & Running.");
response.put("dateTime", formattedDateTime);
Map<String, Object> data = new HashMap<>();
data.put("status", "SERVER and Systems are Up & Running.");
data.put("dateTime", formattedDateTime);

Map<String, Object> response = new HashMap<>();
response.put("data", data);

return response;
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.alwinsimon.UserManagementJavaSpringBoot.Model.User;
import com.alwinsimon.UserManagementJavaSpringBoot.Service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,10 +19,13 @@ public class UserController {
private final UserService userService;

@GetMapping("/current-user")
public User getCurrentUser() {

// API Endpoint to get the LoggedIn User Details using Token received in the Request Header.
return userService.currentUserDetails();

public ResponseEntity<User> getCurrentUser() throws Exception {
try {
// API Endpoint to get the LoggedIn User Details using Token received in the Request Header.
User user = userService.currentUserDetails();
return ResponseEntity.ok(user);
} catch (Exception e) {
throw new UsernameNotFoundException("User not found.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
public interface UserRepository extends JpaRepository <User, Long> {
Optional<User> findByEmail(String email);

@Override
void deleteById(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alwinsimon.UserManagementJavaSpringBoot.Service;

import com.alwinsimon.UserManagementJavaSpringBoot.Model.User;
import com.alwinsimon.UserManagementJavaSpringBoot.Repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminService {

private final UserRepository userRepository;


public List<User> getAllUsers() {
return userRepository.findAll();
}

public void deleteUserByEmail(String email) throws Exception {

User userToDelete = userRepository.findByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("User not found."));

try {

userRepository.deleteById(userToDelete.getId());

} catch (EmptyResultDataAccessException e) {

throw new UsernameNotFoundException("User Deletion FAILED.", e);

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public String generateJwtToken(Map<String, Object> additionalClaims, UserDetails
.setClaims(additionalClaims)
.setSubject(userDetails.getUsername())
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 24))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24)) // 24 hours
.signWith(getJwtSigningKey(), SignatureAlgorithm.HS256)
.compact();

Expand Down