post and get on user table
This commit is contained in:
@@ -3,9 +3,8 @@ package com.innocuoussymmetry.recipin.controllers;
|
||||
import com.innocuoussymmetry.recipin.models.User;
|
||||
import com.innocuoussymmetry.recipin.repositories.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@@ -24,7 +23,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public User getUserById(Integer id) {
|
||||
public User getUserById(@PathVariable Integer id) {
|
||||
Optional<User> maybe = userRepository.findById(id);
|
||||
if (maybe.isEmpty()) {
|
||||
return null;
|
||||
@@ -32,4 +31,22 @@ public class UserController {
|
||||
|
||||
return maybe.get();
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public User updateUserById(@PathVariable Integer id, @RequestBody Map<String, String> updates) {
|
||||
Optional<User> maybe = userRepository.findById(id);
|
||||
|
||||
if (maybe.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
User updatedUser = maybe.get();
|
||||
return new User(updates);
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public User addNewUser(@RequestBody Map<String, String> user) {
|
||||
User newUser = new User(user);
|
||||
return userRepository.save(newUser);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user