Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9680865
Add .env file for private data
Jun 12, 2024
9c1c5ce
Add docker-compose.yml
Jun 12, 2024
d5ef5db
Merge pull request #1 from ZaharKalosha/feature/postgres-automation-a…
ZaharKalosha Jun 12, 2024
39f1388
Add new .env veriable DB_NAME
Jun 12, 2024
050cb9a
Merge pull request #2 from ZaharKalosha/feature/postgres-automation-a…
ZaharKalosha Jun 12, 2024
3725e60
Refactor pom.sql and add h2 dependency
Jun 12, 2024
d7a806e
Bug: QuestionControllerTest have problem with Autowiring
Jun 13, 2024
95049a3
Fix pom.xml junit dependency
Jun 13, 2024
5b82e53
Add application.properties for test directory
Jun 13, 2024
37174b5
Add first correct test
Jun 13, 2024
bab3c32
Add QuestionController test
Jun 14, 2024
27d8a89
Fix antipatterns in tests
Jun 14, 2024
ae1f093
Fix tests methods
Jun 14, 2024
3cd1219
Clean tests class
Jun 14, 2024
095cf1c
Merge pull request #3 from ZaharKalosha/feature/create-tests
ZaharKalosha Jun 14, 2024
c02e4e9
Add questionDTOs and move all business logic from Controller to service
Jun 14, 2024
140bfd9
Add constructor instead of injection
Jun 17, 2024
9919849
Add String.join()
Jun 17, 2024
8c20fd6
Changes the signature of the delete function
Jun 17, 2024
7c35883
Add all data
ZaharKalosha Jun 18, 2024
218a21b
Add fixes to the tests
Kavazar-i Jun 20, 2024
62f5148
Add Captor to the tests but i am not sure that it's correct
Kavazar-i Jun 20, 2024
4ac170f
Rewrite testCreate with capturedQuestion
Kavazar-i Jun 20, 2024
4fda6bc
Add ParameterizedTest fpr createTest
Kavazar-i Jun 20, 2024
f173b88
Add MethodSource and checking of body
Kavazar-i Jun 20, 2024
863a8a8
Merge pull request #4 from ZaharKalosha/feature/question-controller-r…
ZaharKalosha Jun 20, 2024
cf1cb5c
Update entity of user
Kavazar-i Jun 21, 2024
048c7e6
Add user field to question and refactor service and controller
Kavazar-i Jun 21, 2024
f88ff4a
Add user's service and controller
Kavazar-i Jun 21, 2024
edb355f
Add user's in controllers.
Kavazar-i Jun 21, 2024
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
4 changes: 4 additions & 0 deletions dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DB_URL=localhost
DB_NAME=postgres_db
DB_USER=postgres
DB_PASSWORD=
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.5'

services:
postgres-automaton:
container_name: postgres_demo_application
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
PGDATA: /data/postgres
volumes:
- ./postgres-db:/data/postgres
57 changes: 32 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,38 @@
<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>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
<plugins>
Expand All @@ -58,5 +66,4 @@
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
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<Answer> 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));

}
}
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<Answer> 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));
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,39 @@
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<Question> 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));
}
}
package com.example.postgresdemo.controller;

import com.example.postgresdemo.model.QuestionRequestDTO;
import com.example.postgresdemo.model.QuestionResponseDTO;
import com.example.postgresdemo.service.QuestionService;
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 QuestionService questionService;

@GetMapping("/questions")
public Page<QuestionResponseDTO> getQuestions(Pageable pageable) {
return questionService.findAll(pageable);
}

@PostMapping("/questions")
public QuestionResponseDTO createQuestion(@Valid @RequestBody QuestionRequestDTO question) {
return questionService.create(question);
}

@PutMapping("/questions/{questionId}")
public QuestionResponseDTO updateQuestion(@PathVariable Long questionId,
@Valid @RequestBody QuestionRequestDTO questionRequest) {
return questionService.update(questionId, questionRequest);
}

@DeleteMapping("/questions/{questionId}")
public void deleteQuestion(@PathVariable Long questionId) {
questionService.delete(questionId);
}
}
Loading