diff --git a/Screenshot Test/DELETE Todo.png b/Screenshot Test/DELETE Todo.png new file mode 100644 index 0000000..a7c218b Binary files /dev/null and b/Screenshot Test/DELETE Todo.png differ diff --git a/Screenshot Test/DELETE User.png b/Screenshot Test/DELETE User.png new file mode 100644 index 0000000..453a089 Binary files /dev/null and b/Screenshot Test/DELETE User.png differ diff --git a/Screenshot Test/GET Todo.png b/Screenshot Test/GET Todo.png new file mode 100644 index 0000000..b6c9c6f Binary files /dev/null and b/Screenshot Test/GET Todo.png differ diff --git a/Screenshot Test/GET User.png b/Screenshot Test/GET User.png new file mode 100644 index 0000000..28e060e Binary files /dev/null and b/Screenshot Test/GET User.png differ diff --git a/Screenshot Test/POST Todo.png b/Screenshot Test/POST Todo.png new file mode 100644 index 0000000..7842feb Binary files /dev/null and b/Screenshot Test/POST Todo.png differ diff --git a/Screenshot Test/POST User.png b/Screenshot Test/POST User.png new file mode 100644 index 0000000..721c7aa Binary files /dev/null and b/Screenshot Test/POST User.png differ diff --git a/Screenshot Test/PUT Todo.png b/Screenshot Test/PUT Todo.png new file mode 100644 index 0000000..6b49606 Binary files /dev/null and b/Screenshot Test/PUT Todo.png differ diff --git a/Screenshot Test/PUT User.png b/Screenshot Test/PUT User.png new file mode 100644 index 0000000..fecd5c1 Binary files /dev/null and b/Screenshot Test/PUT User.png differ diff --git a/src/main/java/com/example/postgresdemo/PostgresDemoApplication.java b/src/main/java/com/example/postgresdemo/PostgresDemoApplication.java index 29cf2e1..5727225 100644 --- a/src/main/java/com/example/postgresdemo/PostgresDemoApplication.java +++ b/src/main/java/com/example/postgresdemo/PostgresDemoApplication.java @@ -10,4 +10,4 @@ public class PostgresDemoApplication { public static void main(String[] args) { SpringApplication.run(PostgresDemoApplication.class, args); } -} +} \ No newline at end of file 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..ba1d7a8 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/controller/TodoController.java @@ -0,0 +1,67 @@ +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 getTodoByUserId(@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 todosRequest + ) { + if(!userRepository.existsById(userId)) { + throw new ResourceNotFoundException("User not found with id" + userId); + } + + return todoRepository.findById(userId) + .map(todo -> { + todo.setTodos(todosRequest.getTodos()); + return todoRepository.save(todo); + }).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId)); + } + + @DeleteMapping("/users/{userId}/todo/{todoId}") + public ResponseEntity deleteTodo(@PathVariable Long userId, + @PathVariable Long todoId) { + if (!todoRepository.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..848fd3f --- /dev/null +++ b/src/main/java/com/example/postgresdemo/controller/UserController.java @@ -0,0 +1,53 @@ +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")//pakai http post, jadi berbeda dengan get walaupun sama-sema menggunakan /question + public User createUser(@Valid @RequestBody User user) { + + return userRepository.save(user); + } + + @PutMapping("/users/{userId}") //pull update pakai http put + public User updateUser(@PathVariable Long userId, + @Valid @RequestBody User userRequest) { + return userRepository.findById(userId) + .map(user -> { + user.setUsername(userRequest.getUsername()); + 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/Answer.java b/src/main/java/com/example/postgresdemo/model/Answer.java deleted file mode 100644 index b5e48d0..0000000 --- a/src/main/java/com/example/postgresdemo/model/Answer.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.example.postgresdemo.model; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import org.hibernate.annotations.OnDelete; -import org.hibernate.annotations.OnDeleteAction; - -import javax.persistence.*; - -@Entity -@Table(name = "answers") -public class Answer extends AuditModel { - @Id - @GeneratedValue(generator = "answer_generator") - @SequenceGenerator( - name = "answer_generator", - sequenceName = "answer_sequence", - initialValue = 1000 - ) - private Long id; - - @Column(columnDefinition = "text") - private String text; - - @ManyToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "question_id", nullable = false) - @OnDelete(action = OnDeleteAction.CASCADE) - @JsonIgnore - private Question question; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public Question getQuestion() { - return question; - } - - public void setQuestion(Question question) { - this.question = question; - } -} 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/Todo.java b/src/main/java/com/example/postgresdemo/model/Todo.java new file mode 100644 index 0000000..cd5b1fe --- /dev/null +++ b/src/main/java/com/example/postgresdemo/model/Todo.java @@ -0,0 +1,55 @@ +package com.example.postgresdemo.model; + + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import javax.persistence.*; + +@Entity +@Table (name = "todo") +public class Todo extends AuditModel { + @Id + @GeneratedValue (generator = "todo_generator") + @SequenceGenerator( + name = "todo_generator", + sequenceName = "todo_sequence", + initialValue = 2000 + ) + private Long id; + + @Column(columnDefinition = "text") + private String todos; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "user_id", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) + @JsonIgnore + + private User user; + + public Long getId() { + return id; + } + + public void setId(Long id){ + this.id = id; + } + + public String getTodos() { + return todos; + } + + public void setTodos(String todos) { + this.todos = todos; + } + + public User getUser() { + return user; + } + + 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..3696fc5 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/model/User.java @@ -0,0 +1,54 @@ +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 = 2000 + ) + private Long id; + + @NotBlank + @Size(min = 4, max = 2000) + private String username; + + @Column(columnDefinition = "text") + private String name; + + public Long getId() + { + return id; + } + + public void setId(Long id) + { + this.id = id; + } + + public String getUsername() + { + return username; + } + + public void setUsername(String username) + { + this.username = username; + + } + 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..f680e76 100644 --- a/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java +++ b/src/main/java/com/example/postgresdemo/repository/TodoRepository.java @@ -1,11 +1,12 @@ 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); } 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..bfbc39e 100644 --- a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java +++ b/src/main/java/com/example/postgresdemo/repository/UserRepository.java @@ -1,9 +1,11 @@ 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..f9a8529 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,13 @@ ## 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://stampy.db.elephantsql.com:5432/adouixpt +spring.datasource.username=adouixpt +spring.datasource.password=fiHg5jFdSwpo3RRKD2fyqMDeyXNfgVcF +spring.datasource.hikari.maximum-pool-size=3 # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect # Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto = update + +server.port=10000 \ No newline at end of file