Last active
December 31, 2017 11:54
-
-
Save chathurangat/bd01430190bb52ae95b02566f0638d42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.springbootdev.examples.entity.User; | |
import com.springbootdev.examples.repository.UserRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.*; | |
import java.util.List; | |
@RestController | |
@RequestMapping("/api") | |
public class UserController { | |
@Autowired | |
private UserRepository userRepository; | |
@PostMapping("/users") | |
public User create(@RequestBody User user) | |
{ | |
return userRepository.save(user); | |
} | |
@GetMapping("/users") | |
public List<User> findAll() | |
{ | |
return userRepository.findAll(); | |
} | |
@PutMapping("/users/{user_id}") | |
public User update(@PathVariable("user_id") Long userId, @RequestBody User userObject) | |
{ | |
User user = userRepository.findOne(userId); | |
user.setName(userObject.getName()); | |
user.setCountry(userObject.getCountry()); | |
return userRepository.save(user); | |
} | |
@DeleteMapping("/users/{user_id}") | |
public List<User> delete(@PathVariable("user_id") Long userId) | |
{ | |
userRepository.delete(userId); | |
return userRepository.findAll(); | |
} | |
@GetMapping("/users/{user_id}") | |
public User findByUserId(@PathVariable("user_id") Long userId) | |
{ | |
return userRepository.findOne(userId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment