diff --git a/SS/CreateTodo.JPG b/SS/CreateTodo.JPG new file mode 100644 index 0000000..1e27ec4 Binary files /dev/null and b/SS/CreateTodo.JPG differ diff --git a/SS/CreateUser.JPG b/SS/CreateUser.JPG new file mode 100644 index 0000000..a25cc11 Binary files /dev/null and b/SS/CreateUser.JPG differ diff --git a/SS/Get all todos of a User.JPG b/SS/Get all todos of a User.JPG new file mode 100644 index 0000000..3d6896e Binary files /dev/null and b/SS/Get all todos of a User.JPG differ diff --git a/SS/GetpaginatedQuestions.JPG b/SS/GetpaginatedQuestions.JPG new file mode 100644 index 0000000..3fb0b16 Binary files /dev/null and b/SS/GetpaginatedQuestions.JPG differ diff --git a/src/main/java/com/example/postgresdemo/controller/AnswerController.java b/src/main/java/com/example/postgresdemo/controller/AnswerController.java deleted file mode 100644 index 7cfaa47..0000000 --- a/src/main/java/com/example/postgresdemo/controller/AnswerController.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.example.postgresdemo.controller; - -import com.example.postgresdemo.exception.ResourceNotFoundException; -import com.example.postgresdemo.model.Answer; -import com.example.postgresdemo.repository.AnswerRepository; -import com.example.postgresdemo.repository.QuestionRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; -import java.util.List; - -@RestController -public class AnswerController { - - @Autowired - private AnswerRepository answerRepository; - - @Autowired - private QuestionRepository questionRepository; - - @GetMapping("/questions/{questionId}/answers") - public List getAnswersByQuestionId(@PathVariable Long questionId) { - return answerRepository.findByQuestionId(questionId); - } - - @PostMapping("/questions/{questionId}/answers") - public Answer addAnswer(@PathVariable Long questionId, - @Valid @RequestBody Answer answer) { - return questionRepository.findById(questionId) - .map(question -> { - answer.setQuestion(question); - return answerRepository.save(answer); - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); - } - - @PutMapping("/questions/{questionId}/answers/{answerId}") - public Answer updateAnswer(@PathVariable Long questionId, - @PathVariable Long answerId, - @Valid @RequestBody Answer answerRequest) { - if(!questionRepository.existsById(questionId)) { - throw new ResourceNotFoundException("Question not found with id " + questionId); - } - - return answerRepository.findById(answerId) - .map(answer -> { - answer.setText(answerRequest.getText()); - return answerRepository.save(answer); - }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); - } - - @DeleteMapping("/questions/{questionId}/answers/{answerId}") - public ResponseEntity deleteAnswer(@PathVariable Long questionId, - @PathVariable Long answerId) { - if(!questionRepository.existsById(questionId)) { - throw new ResourceNotFoundException("Question not found with id " + questionId); - } - - return answerRepository.findById(answerId) - .map(answer -> { - answerRepository.delete(answer); - return ResponseEntity.ok().build(); - }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); - - } -} diff --git a/src/main/java/com/example/postgresdemo/controller/QuestionController.java b/src/main/java/com/example/postgresdemo/controller/QuestionController.java deleted file mode 100644 index c231819..0000000 --- a/src/main/java/com/example/postgresdemo/controller/QuestionController.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.example.postgresdemo.controller; - -import com.example.postgresdemo.exception.ResourceNotFoundException; -import com.example.postgresdemo.model.Question; -import com.example.postgresdemo.repository.QuestionRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; - -@RestController -public class QuestionController { - - @Autowired - private QuestionRepository questionRepository; - - @GetMapping("/questions") - public Page getQuestions(Pageable pageable) { - return questionRepository.findAll(pageable); - } - - - @PostMapping("/questions") - public Question createQuestion(@Valid @RequestBody Question question) { - return questionRepository.save(question); - } - - @PutMapping("/questions/{questionId}") - public Question updateQuestion(@PathVariable Long questionId, - @Valid @RequestBody Question questionRequest) { - return questionRepository.findById(questionId) - .map(question -> { - question.setTitle(questionRequest.getTitle()); - question.setDescription(questionRequest.getDescription()); - return questionRepository.save(question); - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); - } - - - @DeleteMapping("/questions/{questionId}") - public ResponseEntity deleteQuestion(@PathVariable Long questionId) { - return questionRepository.findById(questionId) - .map(question -> { - questionRepository.delete(question); - return ResponseEntity.ok().build(); - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); - } -} diff --git a/src/main/java/com/example/postgresdemo/controller/TodoController.java b/src/main/java/com/example/postgresdemo/controller/TodoController.java new file mode 100644 index 0000000..85e4d4c --- /dev/null +++ b/src/main/java/com/example/postgresdemo/controller/TodoController.java @@ -0,0 +1,66 @@ +package com.example.postgresdemo.controller; + +import com.example.postgresdemo.exception.ResourceNotFoundException; +import com.example.postgresdemo.model.Todo; +import com.example.postgresdemo.repository.TodoRepository; +import com.example.postgresdemo.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; +import java.util.List; + +@RestController +public class TodoController { + + @Autowired + private TodoRepository todoRepository; + + @Autowired + private UserRepository userRepository; + + @GetMapping("/users/{userId}/todos") + public List getTodosByUserId(@PathVariable Long userId) { + return todoRepository.findByUserId(userId); + } + + @PostMapping("/users/{userId}/todos") + public Todo addTodo(@PathVariable Long userId, + @Valid @RequestBody Todo todo) { + return userRepository.findById(userId) + .map(user -> { + todo.setUser(user); + return todoRepository.save(todo); + }).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId)); + } + + @PutMapping("/users/{userId}/todos/{todoId}") + public Todo updateTodo(@PathVariable Long userId, + @PathVariable Long todoId, + @Valid @RequestBody Todo todoRequest) { + if(!userRepository.existsById(userId)) { + throw new ResourceNotFoundException("User not found with id " + userId); + } + + return todoRepository.findById(todoId) + .map(todo -> { + todo.setText(todoRequest.getText()); + return todoRepository.save(todo); + }).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId)); + } + + @DeleteMapping("/users/{userId}/todos/{todoId}") + public ResponseEntity deleteTodo(@PathVariable Long userId, + @PathVariable Long todoId) { + if(!userRepository.existsById(userId)) { + throw new ResourceNotFoundException("User not found with id " + userId); + } + + return todoRepository.findById(todoId) + .map(todo -> { + todoRepository.delete(todo); + return ResponseEntity.ok().build(); + }).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId)); + + } +} diff --git a/src/main/java/com/example/postgresdemo/controller/UserController.java b/src/main/java/com/example/postgresdemo/controller/UserController.java new file mode 100644 index 0000000..a0a864e --- /dev/null +++ b/src/main/java/com/example/postgresdemo/controller/UserController.java @@ -0,0 +1,49 @@ +package com.example.postgresdemo.controller; + +import com.example.postgresdemo.exception.ResourceNotFoundException; +import com.example.postgresdemo.model.User; +import com.example.postgresdemo.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; + +@RestController +public class UserController { + + @Autowired + private UserRepository userRepository; + + @GetMapping("/users") + public Page getUsers(Pageable pageable) { + return userRepository.findAll(pageable); + } + + + @PostMapping("/users") + public User createUser(@Valid @RequestBody User user) { + return userRepository.save(user); + } + + @PutMapping("/Users/{UserId}") + public User updateUser(@PathVariable Long userId, + @Valid @RequestBody User userRequest) { + return userRepository.findById(userId) + .map(user -> { + user.setName(userRequest.getName()); + return userRepository.save(user); + }).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId)); + } + + + @DeleteMapping("/users/{userId}") + public ResponseEntity deleteUser(@PathVariable Long userId) { + return userRepository.findById(userId) + .map(user -> { + userRepository.delete(user); + return ResponseEntity.ok().build(); + }).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId)); + } +} diff --git a/src/main/java/com/example/postgresdemo/model/Question.java b/src/main/java/com/example/postgresdemo/model/Question.java deleted file mode 100644 index d16a459..0000000 --- a/src/main/java/com/example/postgresdemo/model/Question.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.example.postgresdemo.model; - -import javax.persistence.*; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Size; - -@Entity -@Table(name = "questions") -public class Question extends AuditModel { - @Id - @GeneratedValue(generator = "question_generator") - @SequenceGenerator( - name = "question_generator", - sequenceName = "question_sequence", - initialValue = 1000 - ) - private Long id; - - @NotBlank - @Size(min = 3, max = 100) - private String title; - - @Column(columnDefinition = "text") - private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/src/main/java/com/example/postgresdemo/model/Answer.java b/src/main/java/com/example/postgresdemo/model/Todo.java similarity index 63% rename from src/main/java/com/example/postgresdemo/model/Answer.java rename to src/main/java/com/example/postgresdemo/model/Todo.java index b5e48d0..c1d035e 100644 --- a/src/main/java/com/example/postgresdemo/model/Answer.java +++ b/src/main/java/com/example/postgresdemo/model/Todo.java @@ -7,13 +7,13 @@ import javax.persistence.*; @Entity -@Table(name = "answers") -public class Answer extends AuditModel { +@Table(name = "todos") +public class Todo extends AuditModel { @Id - @GeneratedValue(generator = "answer_generator") + @GeneratedValue(generator = "todo_generator") @SequenceGenerator( - name = "answer_generator", - sequenceName = "answer_sequence", + name = "todo_generator", + sequenceName = "todo_sequence", initialValue = 1000 ) private Long id; @@ -22,10 +22,10 @@ public class Answer extends AuditModel { private String text; @ManyToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "question_id", nullable = false) + @JoinColumn(name = "user_id", nullable = false) @OnDelete(action = OnDeleteAction.CASCADE) @JsonIgnore - private Question question; + private User user; public Long getId() { return id; @@ -43,11 +43,11 @@ public void setText(String text) { this.text = text; } - public Question getQuestion() { - return question; + public User getUser() { + return user; } - public void setQuestion(Question question) { - this.question = question; + public void setUser(User user) { + this.user = user; } } diff --git a/src/main/java/com/example/postgresdemo/model/User.java b/src/main/java/com/example/postgresdemo/model/User.java new file mode 100644 index 0000000..2b9e796 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/model/User.java @@ -0,0 +1,38 @@ +package com.example.postgresdemo.model; + +import javax.persistence.*; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; + +@Entity +@Table(name = "Users") +public class User extends AuditModel { + @Id + @GeneratedValue(generator = "user_generator") + @SequenceGenerator( + name = "user_generator", + sequenceName = "user_sequence", + initialValue = 1000 + ) + private Long id; + + @NotBlank + @Size(min = 3, max = 100) + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java b/src/main/java/com/example/postgresdemo/repository/TodoRepository.java similarity index 53% rename from src/main/java/com/example/postgresdemo/repository/AnswerRepository.java rename to src/main/java/com/example/postgresdemo/repository/TodoRepository.java index 761f91f..71a1f89 100644 --- a/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java +++ b/src/main/java/com/example/postgresdemo/repository/TodoRepository.java @@ -1,11 +1,11 @@ package com.example.postgresdemo.repository; -import com.example.postgresdemo.model.Answer; +import com.example.postgresdemo.model.Todo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository -public interface AnswerRepository extends JpaRepository { - List findByQuestionId(Long questionId); -} +public interface TodoRepository extends JpaRepository { + List findByUserId(Long userId); +} \ No newline at end of file diff --git a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java b/src/main/java/com/example/postgresdemo/repository/UserRepository.java similarity index 58% rename from src/main/java/com/example/postgresdemo/repository/QuestionRepository.java rename to src/main/java/com/example/postgresdemo/repository/UserRepository.java index 290373d..77ef225 100644 --- a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java +++ b/src/main/java/com/example/postgresdemo/repository/UserRepository.java @@ -1,9 +1,9 @@ package com.example.postgresdemo.repository; -import com.example.postgresdemo.model.Question; +import com.example.postgresdemo.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface QuestionRepository extends JpaRepository { +public interface UserRepository extends JpaRepository { } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ff398ee..13b3efb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,14 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo -spring.datasource.username= rajeevkumarsingh -spring.datasource.password= +spring.datasource.url=jdbc:postgresql://baasu.db.elephantsql.com:5432/pfqcexbz +spring.datasource.username= pfqcexbz +spring.datasource.password= pcKJ_dNqAr2E6I1M4HnNBwfOg5RtxiNm # The SQL dialect makes Hibernate generate better SQL for the chosen database -spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect +# spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect +# spring.datasource.driver-class-name=org.postgresql.Driver +spring.datasource.hikari.maximum-pool-size=3 +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.jpa.properties.hibernate.temp.use_jdbc_metadata_default=false # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update +spring.jpa.hibernate.ddl-auto = update \ No newline at end of file