-
Notifications
You must be signed in to change notification settings - Fork 0
7_Работа с БД #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| services: | ||
| todo: | ||
| image: postgres:latest | ||
| environment: | ||
| - POSTGRES_USER=admin | ||
| - POSTGRES_PASSWORD=maozedong | ||
| - POSTGRES_DB=todo | ||
| ports: | ||
| - "5432:5432" | ||
| volumes: | ||
| - todo_data:/var/lib/postgresql/data | ||
|
|
||
| volumes: | ||
| todo_data: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.javaspringcourse.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.javaspringcourse.dto.TodoDto; | ||
| import org.javaspringcourse.service.TodoService; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/todo") | ||
| @RequiredArgsConstructor | ||
| public class TodoController { | ||
| private final TodoService todoService; | ||
|
|
||
| @GetMapping("/get") | ||
| public List<TodoDto> getAllTodo() { | ||
| return todoService.findAll(); | ||
| } | ||
|
|
||
| @PostMapping("/create") | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public void createTodo(@RequestBody TodoDto todoDto) { | ||
| todoService.save(todoDto); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package org.javaspringcourse.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record TodoDto(String name, List<String> events) {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package org.javaspringcourse.model; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @Entity | ||
| @Table(name = "todo") | ||
| public class TodoEntity { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У всех entity необходимо переопределять hashCode и equalse методы |
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private long id; | ||
|
|
||
| @Column(nullable = false) | ||
| private String name; | ||
|
|
||
| @ElementCollection | ||
| @CollectionTable(name = "events", joinColumns = @JoinColumn(name = "todo_id")) | ||
| private Set<String> events = new HashSet<String>(); | ||
|
|
||
| public TodoEntity(String name, Set<String> events) { | ||
| this.name = name; | ||
| this.events = events; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package org.javaspringcourse.repository; | ||
|
|
||
| import org.javaspringcourse.model.TodoEntity; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface TodoEntityRepository extends JpaRepository<TodoEntity, Long> {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package org.javaspringcourse.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.javaspringcourse.dto.TodoDto; | ||
| import org.javaspringcourse.model.TodoEntity; | ||
| import org.javaspringcourse.repository.TodoEntityRepository; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class TodoService { | ||
| private final TodoEntityRepository todoEntityRepository; | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<TodoDto> findAll() { | ||
| return todoEntityRepository.findAll().stream() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Получим проблему n+1, на каждый Task будет выполнен запрос на поиск events, хотя все это можно загрузить за один запрос |
||
| .map(this::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| private TodoDto toDto(TodoEntity todo) { | ||
| return new TodoDto(todo.getName(), todo.getEvents().stream().toList()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void save(TodoDto todoDto) { | ||
| var todoEntity = new TodoEntity(todoDto.name(), new HashSet<>(todoDto.events())); | ||
| todoEntityRepository.save(todoEntity); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| spring: | ||
| datasource: | ||
| url: jdbc:postgresql://${POSTGRES_HOST:localhost}:${POSTGRES_PORT:5432}/${POSTGRES_DB:todo} | ||
| username: ${POSTGRES_USER:admin} | ||
| password: ${POSTGRES_PASSWORD:maozedong} | ||
| jpa: | ||
| hibernate: | ||
| ddl-auto: create-drop |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TaskDto используется как для ответа, так и для запроса. В данном случае не принципиально, но лучше для ответа и запроса (даже если в разных методах используется) создавать свои ДТО