This repository has been archived on 2026-03-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Progetto-m152-Java/src/main/java/ch/progetto152/controller/UserController.java
2023-04-27 16:12:49 +02:00

89 lines
2.9 KiB
Java

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<List<UserEntity>> getAllUsers() {
List<UserEntity> users = userService.getAllUsers();
return new ResponseEntity<>(users, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<UserEntity> 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<UserEntity> 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<UserEntity> 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<UserEntity> 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<UserEntity> 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<Void> 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);
}
}
}