Skip to content

Commit 9cb2948

Browse files
committed
Todo Samuel
1 parent 68ce15f commit 9cb2948

File tree

14 files changed

+179
-187
lines changed

14 files changed

+179
-187
lines changed

SS/CreateTodo.JPG

114 KB
Loading

SS/CreateUser.JPG

109 KB
Loading

SS/Get all todos of a User.JPG

113 KB
Loading

SS/GetpaginatedQuestions.JPG

123 KB
Loading

src/main/java/com/example/postgresdemo/controller/AnswerController.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/main/java/com/example/postgresdemo/controller/QuestionController.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.example.postgresdemo.controller;
2+
3+
import com.example.postgresdemo.exception.ResourceNotFoundException;
4+
import com.example.postgresdemo.model.Todo;
5+
import com.example.postgresdemo.repository.TodoRepository;
6+
import com.example.postgresdemo.repository.UserRepository;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.*;
10+
import javax.validation.Valid;
11+
import java.util.List;
12+
13+
@RestController
14+
public class TodoController {
15+
16+
@Autowired
17+
private TodoRepository todoRepository;
18+
19+
@Autowired
20+
private UserRepository userRepository;
21+
22+
@GetMapping("/users/{userId}/todos")
23+
public List<Todo> getTodosByUserId(@PathVariable Long userId) {
24+
return todoRepository.findByUserId(userId);
25+
}
26+
27+
@PostMapping("/users/{userId}/todos")
28+
public Todo addTodo(@PathVariable Long userId,
29+
@Valid @RequestBody Todo todo) {
30+
return userRepository.findById(userId)
31+
.map(user -> {
32+
todo.setUser(user);
33+
return todoRepository.save(todo);
34+
}).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId));
35+
}
36+
37+
@PutMapping("/users/{userId}/todos/{todoId}")
38+
public Todo updateTodo(@PathVariable Long userId,
39+
@PathVariable Long todoId,
40+
@Valid @RequestBody Todo todoRequest) {
41+
if(!userRepository.existsById(userId)) {
42+
throw new ResourceNotFoundException("User not found with id " + userId);
43+
}
44+
45+
return todoRepository.findById(todoId)
46+
.map(todo -> {
47+
todo.setText(todoRequest.getText());
48+
return todoRepository.save(todo);
49+
}).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId));
50+
}
51+
52+
@DeleteMapping("/users/{userId}/todos/{todoId}")
53+
public ResponseEntity<?> deleteTodo(@PathVariable Long userId,
54+
@PathVariable Long todoId) {
55+
if(!userRepository.existsById(userId)) {
56+
throw new ResourceNotFoundException("User not found with id " + userId);
57+
}
58+
59+
return todoRepository.findById(todoId)
60+
.map(todo -> {
61+
todoRepository.delete(todo);
62+
return ResponseEntity.ok().build();
63+
}).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId));
64+
65+
}
66+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.postgresdemo.controller;
2+
3+
import com.example.postgresdemo.exception.ResourceNotFoundException;
4+
import com.example.postgresdemo.model.User;
5+
import com.example.postgresdemo.repository.UserRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.*;
11+
import javax.validation.Valid;
12+
13+
@RestController
14+
public class UserController {
15+
16+
@Autowired
17+
private UserRepository userRepository;
18+
19+
@GetMapping("/users")
20+
public Page<User> getUsers(Pageable pageable) {
21+
return userRepository.findAll(pageable);
22+
}
23+
24+
25+
@PostMapping("/users")
26+
public User createUser(@Valid @RequestBody User user) {
27+
return userRepository.save(user);
28+
}
29+
30+
@PutMapping("/Users/{UserId}")
31+
public User updateUser(@PathVariable Long userId,
32+
@Valid @RequestBody User userRequest) {
33+
return userRepository.findById(userId)
34+
.map(user -> {
35+
user.setName(userRequest.getName());
36+
return userRepository.save(user);
37+
}).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId));
38+
}
39+
40+
41+
@DeleteMapping("/users/{userId}")
42+
public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
43+
return userRepository.findById(userId)
44+
.map(user -> {
45+
userRepository.delete(user);
46+
return ResponseEntity.ok().build();
47+
}).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId));
48+
}
49+
}

src/main/java/com/example/postgresdemo/model/Question.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/com/example/postgresdemo/model/Answer.java renamed to src/main/java/com/example/postgresdemo/model/Todo.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import javax.persistence.*;
88

99
@Entity
10-
@Table(name = "answers")
11-
public class Answer extends AuditModel {
10+
@Table(name = "todos")
11+
public class Todo extends AuditModel {
1212
@Id
13-
@GeneratedValue(generator = "answer_generator")
13+
@GeneratedValue(generator = "todo_generator")
1414
@SequenceGenerator(
15-
name = "answer_generator",
16-
sequenceName = "answer_sequence",
15+
name = "todo_generator",
16+
sequenceName = "todo_sequence",
1717
initialValue = 1000
1818
)
1919
private Long id;
@@ -22,10 +22,10 @@ public class Answer extends AuditModel {
2222
private String text;
2323

2424
@ManyToOne(fetch = FetchType.LAZY, optional = false)
25-
@JoinColumn(name = "question_id", nullable = false)
25+
@JoinColumn(name = "user_id", nullable = false)
2626
@OnDelete(action = OnDeleteAction.CASCADE)
2727
@JsonIgnore
28-
private Question question;
28+
private User user;
2929

3030
public Long getId() {
3131
return id;
@@ -43,11 +43,11 @@ public void setText(String text) {
4343
this.text = text;
4444
}
4545

46-
public Question getQuestion() {
47-
return question;
46+
public User getUser() {
47+
return user;
4848
}
4949

50-
public void setQuestion(Question question) {
51-
this.question = question;
50+
public void setUser(User user) {
51+
this.user = user;
5252
}
5353
}

0 commit comments

Comments
 (0)