update controllers and services

This commit is contained in:
mirrerror
2023-10-02 08:58:17 +03:00
parent 51b5537558
commit 16d74c0ab6
6 changed files with 86 additions and 84 deletions

View File

@@ -4,10 +4,10 @@ import com.faf223.expensetrackerfaf.model.User;
import com.faf223.expensetrackerfaf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
@@ -20,14 +20,40 @@ public class UserController {
this.userService = userService;
}
@GetMapping("/{userUuid}")
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
User user = userService.getUserById(userUuid);
if (user != null) {
@GetMapping()
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getUsers();
if (!users.isEmpty()) return ResponseEntity.ok(users);
else return ResponseEntity.notFound().build();
}
@PostMapping()
public ResponseEntity<User> createNewUser(@ModelAttribute("user") User user,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
userService.createOrUpdateUser(user);
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
}
@PatchMapping()
public ResponseEntity<User> updateUser(@ModelAttribute("user") User user,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
userService.createOrUpdateUser(user);
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
}
@GetMapping("/{userUuid}")
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
User user = userService.getUserById(userUuid);
if (user != null) return ResponseEntity.ok(user);
else return ResponseEntity.notFound().build();
}
}