Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GET, PUT and DELETE user by id #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.idea/
*.iml
*.iws
out/

# Mac
.DS_Store
Expand All @@ -17,4 +18,4 @@ target/

#gradle
.gradle
build/
build/
27 changes: 27 additions & 0 deletions src/main/java/spaceshuttle/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,31 @@ User addUser(@RequestBody User newUser) {
// @RequestParam means it is a parameter from the GET or POST request
return userRepository.save(newUser);
}

// Homework 1: GET user by id
@GetMapping(value = "/{id}")
public @ResponseBody
User getUserById(@PathVariable Long id) {
return userRepository.findOne(id);
}

// Homework 2: PUT user by id
@PutMapping(value = "/{id}")
public @ResponseBody
void updateUserById(@PathVariable Long id,
@RequestParam String username,
@RequestParam String password) {
User user = userRepository.findOne(id);
user.setUsername(username);
user.setPassword(password);
userRepository.save(user);
}

// Homework 3: DELETE user by id
@DeleteMapping(value = "/{id}")
public @ResponseBody
void deleteUserById(@PathVariable Long id) {
userRepository.delete(id);
}

}
9 changes: 9 additions & 0 deletions src/main/java/spaceshuttle/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}
Expand Down