|
| 1 | +package com.companyname.springbootcrudrest.controller; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import javax.validation.Valid; |
| 9 | + |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.web.bind.annotation.PathVariable; |
| 15 | +import org.springframework.web.bind.annotation.PostMapping; |
| 16 | +import org.springframework.web.bind.annotation.PutMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestBody; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | +import org.springframework.web.bind.annotation.RestController; |
| 20 | + |
| 21 | +import com.companyname.springbootcrudrest.exception.ResourceNotFoundException; |
| 22 | +import com.companyname.springbootcrudrest.model.User; |
| 23 | +import com.companyname.springbootcrudrest.repository.UserRepository; |
| 24 | + |
| 25 | +@RestController |
| 26 | +@RequestMapping("/api/v1") |
| 27 | +public class UserController { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private UserRepository userRepository; |
| 31 | + |
| 32 | + |
| 33 | + @GetMapping("/users") |
| 34 | + public List<User> getAllUsers() { |
| 35 | + return userRepository.findAll(); |
| 36 | + } |
| 37 | + |
| 38 | + @GetMapping("/users/{id}") |
| 39 | + public ResponseEntity<User> getUserById( |
| 40 | + @PathVariable(value = "id") Long userId) throws ResourceNotFoundException { |
| 41 | + User user = userRepository.findById(userId) |
| 42 | + .orElseThrow(() -> new ResourceNotFoundException("User not found :: " + userId)); |
| 43 | + return ResponseEntity.ok().body(user); |
| 44 | + } |
| 45 | + |
| 46 | + @PostMapping("/users") |
| 47 | + public User createUser(@Valid @RequestBody User user) { |
| 48 | + return userRepository.save(user); |
| 49 | + } |
| 50 | + |
| 51 | + @PutMapping("/users/{id}") |
| 52 | + public ResponseEntity<User> updateUser( |
| 53 | + @PathVariable(value = "id") Long userId, |
| 54 | + @Valid @RequestBody User userDetails) throws ResourceNotFoundException { |
| 55 | + User user = userRepository.findById(userId) |
| 56 | + .orElseThrow(() -> new ResourceNotFoundException("User not found :: " + userId)); |
| 57 | + |
| 58 | + user.setEmailId(userDetails.getEmailId()); |
| 59 | + user.setLastName(userDetails.getLastName()); |
| 60 | + user.setFirstName(userDetails.getFirstName()); |
| 61 | + user.setUpdatedAt(new Date()); |
| 62 | + final User updatedUser = userRepository.save(user); |
| 63 | + return ResponseEntity.ok(updatedUser); |
| 64 | + } |
| 65 | + |
| 66 | + @DeleteMapping("/users/{id}") |
| 67 | + public Map<String, Boolean> deleteUser( |
| 68 | + @PathVariable(value = "id") Long userId) throws ResourceNotFoundException { |
| 69 | + User user = userRepository.findById(userId) |
| 70 | + .orElseThrow(() -> new ResourceNotFoundException("User not found :: " + userId)); |
| 71 | + |
| 72 | + userRepository.delete(user); |
| 73 | + Map<String, Boolean> response = new HashMap<>(); |
| 74 | + response.put("deleted", Boolean.TRUE); |
| 75 | + return response; |
| 76 | + } |
| 77 | +} |
0 commit comments