diff --git a/pom.xml b/pom.xml index d254d86..529934e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,36 +1,53 @@ - -4.0.0 -org.example -TaskAPI -1.0-SNAPSHOT + 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.5 - + org.example + TaskAPI + 1.0-SNAPSHOT - - 20 - 20 - UTF-8 - - - - + org.springframework.boot - spring-boot-starter-web - + spring-boot-starter-parent + 3.2.5 + - - org.springframework.boot - spring-boot-starter-test - test - - + + 20 + 20 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.postgresql + postgresql + runtime + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.data + spring-data-jpa + 3.2.5 + + \ No newline at end of file diff --git a/src/main/java/org/example/TaskAPI.java b/src/main/java/org/example/TaskAPI.java index eb01224..3a5b75d 100644 --- a/src/main/java/org/example/TaskAPI.java +++ b/src/main/java/org/example/TaskAPI.java @@ -1,79 +1,90 @@ package org.example; +import jakarta.persistence.*; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.*; - -import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.Optional; @SpringBootApplication @RestController public class TaskAPI { + @Autowired + private TaskRepository taskRepository; + + @Entity public static class Task { - private int id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; private String title; private String description; - - public Task(int id, String title, String description) { - this.id = id; + public Task(String title, String description) { this.title = title; this.description = description; - } - - public void setId(int id) { - this.id = id; + public Task() { } - public int getId() { + public Long getId() { return id; } - public String getTitle(){return title;} + public String getTitle() { + return title; + } - public String getDescription(){return description;} + public String getDescription() { + return description; + } - } - - - private List tasks = new ArrayList<>(); - - public TaskAPI() { - tasks.add(new Task(1, "Задание №1", "Описание задания №1")); - tasks.add(new Task(2, "Задание №2", "Описание задания №2")); - tasks.add(new Task(3, "Задание №3", "Описание задания №3")); + public void setTitle(String title) { + this.title = title; + } + public void setDescription(String description) { + this.description = description; + } } // Получение задания по id @GetMapping("/tasks/{taskId}") - public Task getTask(@PathVariable int taskId) { - return tasks.stream() - .filter(task -> task.getId() == taskId) - .findFirst() - .orElseThrow(() -> new RuntimeException("Задание не найдено")); + public Task getTask(@PathVariable Long taskId) { + return taskRepository.findById(taskId).orElse(null); } // Получение всех заданий @GetMapping("/tasks") public List getTasks() { - return tasks; + return taskRepository.findAll(); } - //добавление задания + // Добавление нового задания @PostMapping("/tasks") public Task createTask(@RequestBody Task newTask) { - newTask.setId(tasks.size() + 1); - tasks.add(newTask); - return newTask; + return taskRepository.save(newTask); + } + + // Удаление задания по id + @DeleteMapping("/tasks/{taskId}") + public String deleteTaskById(@PathVariable Long taskId) { + Optional taskToDelete = taskRepository.findById(taskId); + + if (taskToDelete.isPresent()) { + Task task = taskToDelete.get(); + taskRepository.deleteById(taskId); + return "Удалена задача с id: " + task.getId() + " и названием: " + task.getTitle(); + } else { + return "Задача с id " + taskId + " не найдена."; + } } public static void main(String[] args) { SpringApplication.run(TaskAPI.class, args); } -} - +} \ No newline at end of file diff --git a/src/main/java/org/example/TaskRepository.java b/src/main/java/org/example/TaskRepository.java new file mode 100644 index 0000000..61b6716 --- /dev/null +++ b/src/main/java/org/example/TaskRepository.java @@ -0,0 +1,6 @@ +package org.example; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TaskRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..c743646 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.datasource.url=jdbc:postgresql://localhost:5432/Tasks +spring.datasource.username=Wanster +spring.datasource.password=6254 +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file diff --git a/target/classes/application.properties b/target/classes/application.properties new file mode 100644 index 0000000..c743646 --- /dev/null +++ b/target/classes/application.properties @@ -0,0 +1,5 @@ +spring.datasource.url=jdbc:postgresql://localhost:5432/Tasks +spring.datasource.username=Wanster +spring.datasource.password=6254 +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file diff --git a/target/classes/org/example/TaskAPI$Task.class b/target/classes/org/example/TaskAPI$Task.class new file mode 100644 index 0000000..4892f99 Binary files /dev/null and b/target/classes/org/example/TaskAPI$Task.class differ diff --git a/target/classes/org/example/TaskAPI.class b/target/classes/org/example/TaskAPI.class new file mode 100644 index 0000000..4ad0b41 Binary files /dev/null and b/target/classes/org/example/TaskAPI.class differ diff --git a/target/classes/org/example/TaskRepository.class b/target/classes/org/example/TaskRepository.class new file mode 100644 index 0000000..a092d10 Binary files /dev/null and b/target/classes/org/example/TaskRepository.class differ