package ch.progetto152.controller; import ch.progetto152.entity.UserEntity; import ch.progetto152.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/progetto152/users") public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @GetMapping("") public ResponseEntity> getAllUsers() { List users = userService.getAllUsers(); return new ResponseEntity<>(users, HttpStatus.OK); } @GetMapping("/{id}") public ResponseEntity getUserById(@PathVariable("id") Long id) { UserEntity user = userService.getUserById(id); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @GetMapping("?name={name}") public ResponseEntity getUserByName(@PathVariable("name") String name) { UserEntity user = userService.getUserByName(name); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @GetMapping("?username={username}") public ResponseEntity getUserByUsername(@PathVariable("username") String username) { UserEntity user = userService.getUserByUsername(username); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @PostMapping("") public ResponseEntity createUser(@RequestBody UserEntity user) { UserEntity createdUser = userService.createUser(user); if(createdUser == null) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity<>(createdUser, HttpStatus.CREATED); } @PutMapping("/{id}") public ResponseEntity updateUser(@PathVariable("id") Long id, @RequestBody UserEntity user) { UserEntity updatedUser = userService.updateUser(id, user); if (updatedUser != null) { return new ResponseEntity<>(updatedUser, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @DeleteMapping("/{id}") public ResponseEntity deleteUser(@PathVariable("id") Long id) { boolean deleted = userService.deleteUser(id); if (deleted) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } }