Skip to content
Closed
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
134 changes: 77 additions & 57 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,62 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>postgres-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>postgres-demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>postgres-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>postgres-demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
21 changes: 21 additions & 0 deletions src/main/java/com/example/postgresdemo/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Answer> getAnswersByQuestionId(@PathVariable Long questionId) {
return answerRepository.findByQuestionId(questionId);
}
public List<Answer> 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));
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
30 changes: 6 additions & 24 deletions src/main/java/com/example/postgresdemo/model/Answer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
Loading