2. Model
3. Service
4. Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/create")
public ResponseEntity<?> createUser(@RequestBody User user) {
try {
User createdUser = userService.createUser(user);
return ResponseEntity.ok(createdUser);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@PutMapping("/{id}")
public ResponseEntity<?> updateUser(@PathVariable("id") Long id, @RequestBody User user) {
try {
User updateUser = userService.updateUser(id, user);
return ResponseEntity.ok(updateUser);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteUser(@PathVariable("id") Long id) {
try {
userService.deleteUser(id);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
}