From 14a8ed655722a27d8342ec062ca4429ec562bea5 Mon Sep 17 00:00:00 2001 From: devMonkey87 Date: Sat, 4 Jun 2022 17:38:41 +0200 Subject: [PATCH] :+1: --- pom.xml | 134 ++++++++++-------- .../postgresdemo/config/SwaggerConfig.java | 21 +++ .../controller/AnswerController.java | 87 ++++++------ .../controller/QuestionController.java | 20 +-- .../example/postgresdemo/model/Answer.java | 30 +--- .../example/postgresdemo/model/Question.java | 32 ++--- .../repository/AnswerRepository.java | 5 +- .../repository/QuestionRepository.java | 2 +- .../postgresdemo/service/AnswerService.java | 16 +++ .../service/impl/AnswerServiceImpl.java | 40 ++++++ src/main/resources/application.properties | 14 +- src/main/resources/banner.txt | 6 + .../service/impl/AnswerServiceImplTest.java | 23 +++ 13 files changed, 264 insertions(+), 166 deletions(-) create mode 100644 src/main/java/com/example/postgresdemo/config/SwaggerConfig.java create mode 100644 src/main/java/com/example/postgresdemo/service/AnswerService.java create mode 100644 src/main/java/com/example/postgresdemo/service/impl/AnswerServiceImpl.java create mode 100644 src/main/resources/banner.txt create mode 100644 src/test/java/com/example/postgresdemo/service/impl/AnswerServiceImplTest.java diff --git a/pom.xml b/pom.xml index 2c23e76..512dc99 100644 --- a/pom.xml +++ b/pom.xml @@ -1,62 +1,82 @@ - 4.0.0 - - com.example - postgres-demo - 0.0.1-SNAPSHOT - jar - - postgres-demo - Demo project for Spring Boot - - - org.springframework.boot - spring-boot-starter-parent - 2.5.5 - - - - - UTF-8 - UTF-8 - 11 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-validation - - - org.postgresql - postgresql - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + com.example + postgres-demo + 0.0.1-SNAPSHOT + jar + + postgres-demo + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.5.5 + + + + + UTF-8 + UTF-8 + 11 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-validation + + + org.postgresql + postgresql + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + + + io.springfox + springfox-swagger2 + 3.0.0 + + + io.springfox + springfox-boot-starter + 3.0.0 + + + io.springfox + springfox-swagger-ui + 2.9.2 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/src/main/java/com/example/postgresdemo/config/SwaggerConfig.java b/src/main/java/com/example/postgresdemo/config/SwaggerConfig.java new file mode 100644 index 0000000..ce75ad7 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/config/SwaggerConfig.java @@ -0,0 +1,21 @@ +package com.example.postgresdemo.config; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +@Configuration +public class SwaggerConfig { + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } +} \ 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 index 7cfaa47..31cc81d 100644 --- a/src/main/java/com/example/postgresdemo/controller/AnswerController.java +++ b/src/main/java/com/example/postgresdemo/controller/AnswerController.java @@ -2,65 +2,68 @@ 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 com.example.postgresdemo.service.AnswerService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + import javax.validation.Valid; -import java.util.List; @RestController public class AnswerController { @Autowired - private AnswerRepository answerRepository; + private AnswerService answerService; @Autowired - private QuestionRepository questionRepository; + private QuestionRepository questionService; + /* @GetMapping("/questions/{questionId}/answers") - public List getAnswersByQuestionId(@PathVariable Long questionId) { - return answerRepository.findByQuestionId(questionId); - } + public List getAnswersByQuestionId(@PathVariable int questionId) { + return answerService.findById(questionId); + }*/ @PostMapping("/questions/{questionId}/answers") - public Answer addAnswer(@PathVariable Long questionId, + public Answer addAnswer(@PathVariable int questionId, @Valid @RequestBody Answer answer) { - return questionRepository.findById(questionId) + return questionService.findById(questionId) .map(question -> { answer.setQuestion(question); - return answerRepository.save(answer); + return answerService.saveOrUpdate(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)); - - } +// +// @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 index c231819..f218a9b 100644 --- a/src/main/java/com/example/postgresdemo/controller/QuestionController.java +++ b/src/main/java/com/example/postgresdemo/controller/QuestionController.java @@ -27,16 +27,16 @@ 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)); - } +// @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}") diff --git a/src/main/java/com/example/postgresdemo/model/Answer.java b/src/main/java/com/example/postgresdemo/model/Answer.java index b5e48d0..7fc20ff 100644 --- a/src/main/java/com/example/postgresdemo/model/Answer.java +++ b/src/main/java/com/example/postgresdemo/model/Answer.java @@ -1,22 +1,26 @@ package com.example.postgresdemo.model; import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import javax.persistence.*; @Entity +@Data @Table(name = "answers") public class Answer extends AuditModel { + private static final long serialVersionUID = 1L; + @Id @GeneratedValue(generator = "answer_generator") @SequenceGenerator( name = "answer_generator", sequenceName = "answer_sequence", - initialValue = 1000 + initialValue = 1 ) - private Long id; + private int id; @Column(columnDefinition = "text") private String text; @@ -27,27 +31,5 @@ public class Answer extends AuditModel { @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 index d16a459..e4cd43e 100644 --- a/src/main/java/com/example/postgresdemo/model/Question.java +++ b/src/main/java/com/example/postgresdemo/model/Question.java @@ -1,20 +1,26 @@ package com.example.postgresdemo.model; +import lombok.Data; + import javax.persistence.*; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; @Entity +@Data @Table(name = "questions") public class Question extends AuditModel { + + private static final long serialVersionUID = -5502043778089640767L; + @Id @GeneratedValue(generator = "question_generator") @SequenceGenerator( name = "question_generator", sequenceName = "question_sequence", - initialValue = 1000 + initialValue = 1 ) - private Long id; + private int id; @NotBlank @Size(min = 3, max = 100) @@ -23,27 +29,5 @@ public class Question extends AuditModel { @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/repository/AnswerRepository.java b/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java index 761f91f..cf020ff 100644 --- a/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java +++ b/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java @@ -3,9 +3,10 @@ import com.example.postgresdemo.model.Answer; 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 AnswerRepository extends JpaRepository { + List findByQuestionId(int questionId); } diff --git a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java b/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java index 290373d..7667031 100644 --- a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java +++ b/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java @@ -5,5 +5,5 @@ import org.springframework.stereotype.Repository; @Repository -public interface QuestionRepository extends JpaRepository { +public interface QuestionRepository extends JpaRepository { } diff --git a/src/main/java/com/example/postgresdemo/service/AnswerService.java b/src/main/java/com/example/postgresdemo/service/AnswerService.java new file mode 100644 index 0000000..1f24a9d --- /dev/null +++ b/src/main/java/com/example/postgresdemo/service/AnswerService.java @@ -0,0 +1,16 @@ +package com.example.postgresdemo.service; + +import com.example.postgresdemo.model.Answer; + +import java.util.List; +import java.util.Optional; + +public interface AnswerService { + + Optional findById(int id); + + List findAll(); + + Answer saveOrUpdate(Answer answer); + +} diff --git a/src/main/java/com/example/postgresdemo/service/impl/AnswerServiceImpl.java b/src/main/java/com/example/postgresdemo/service/impl/AnswerServiceImpl.java new file mode 100644 index 0000000..252c01c --- /dev/null +++ b/src/main/java/com/example/postgresdemo/service/impl/AnswerServiceImpl.java @@ -0,0 +1,40 @@ +package com.example.postgresdemo.service.impl; + +import com.example.postgresdemo.model.Answer; +import com.example.postgresdemo.repository.AnswerRepository; +import com.example.postgresdemo.service.AnswerService; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Date; +import java.util.List; +import java.util.Optional; + +public class AnswerServiceImpl implements AnswerService { + + @Autowired + AnswerRepository answerRepository; + + @Override + public Optional findById(int id) { + return answerRepository.findById(id); + } + + @Override + public List findAll() { + return answerRepository.findAll(); + } + + @Override + public Answer saveOrUpdate(Answer answer) { + if (!answerRepository.existsById(answer.getId())) { + return answerRepository.save(answer); + } else { + Answer answer1 = new Answer(); + answer1.setQuestion(answer.getQuestion()); + answer1.setText(answer.getText()); + answer1.setUpdatedAt(new Date()); + answer1.setCreatedAt(answer.getCreatedAt()); + return answerRepository.save(answer1); + } + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 35b376a..f829187 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,12 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo -spring.datasource.username= postgres -spring.datasource.password= - +spring.datasource.username=postgres +spring.datasource.password=root # 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 # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update +spring.jpa.hibernate.ddl-auto=update +spring.jpa.generate-ddl=true +logging.level.org.hibernate.SQL=DEBUG +logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE +spring.banner.location=classpath:banner.txt diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt new file mode 100644 index 0000000..976df62 --- /dev/null +++ b/src/main/resources/banner.txt @@ -0,0 +1,6 @@ + _______ __ __ ______ __ ___ ____ ____ _______ ___ __ __ __ +| ____|| | | | / || |/ / \ \ / / | ____| / \ | | | | | | +| |__ | | | | | ,----'| ' / \ \/ / | |__ / ^ \ | |__| | | | +| __| | | | | | | | < \_ _/ | __| / /_\ \ | __ | | | +| | | `--' | | `----.| . \ | | | |____ / _____ \ | | | | |__| +|__| \______/ \______||__|\__\ |__| |_______/__/ \__\ |__| |__| (__) diff --git a/src/test/java/com/example/postgresdemo/service/impl/AnswerServiceImplTest.java b/src/test/java/com/example/postgresdemo/service/impl/AnswerServiceImplTest.java new file mode 100644 index 0000000..3a34328 --- /dev/null +++ b/src/test/java/com/example/postgresdemo/service/impl/AnswerServiceImplTest.java @@ -0,0 +1,23 @@ +package com.example.postgresdemo.service.impl; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class AnswerServiceImplTest { + + @BeforeEach + void setUp() { + } + + @Test + void findById() { + } + + @Test + void findAll() { + } + + @Test + void saveOrUpdate() { + } +} \ No newline at end of file