diff --git a/TaskAPI/pom.xml b/TaskAPI/pom.xml new file mode 100644 index 0000000..529934e --- /dev/null +++ b/TaskAPI/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.example + TaskAPI + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + + 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/TaskAPI/src/main/java/org/example/TaskAPI.java b/TaskAPI/src/main/java/org/example/TaskAPI.java new file mode 100644 index 0000000..3a5b75d --- /dev/null +++ b/TaskAPI/src/main/java/org/example/TaskAPI.java @@ -0,0 +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.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 { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + private String description; + + public Task(String title, String description) { + this.title = title; + this.description = description; + } + + public Task() { + } + + public Long getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + } + + // Получение задания по id + @GetMapping("/tasks/{taskId}") + public Task getTask(@PathVariable Long taskId) { + return taskRepository.findById(taskId).orElse(null); + } + + // Получение всех заданий + @GetMapping("/tasks") + public List getTasks() { + return taskRepository.findAll(); + } + + // Добавление нового задания + @PostMapping("/tasks") + public Task createTask(@RequestBody Task 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/TaskAPI/src/main/java/org/example/TaskRepository.java b/TaskAPI/src/main/java/org/example/TaskRepository.java new file mode 100644 index 0000000..61b6716 --- /dev/null +++ b/TaskAPI/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/TaskAPI/src/main/resources/application.properties b/TaskAPI/src/main/resources/application.properties new file mode 100644 index 0000000..c743646 --- /dev/null +++ b/TaskAPI/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/TaskAPI/target/classes/application.properties b/TaskAPI/target/classes/application.properties new file mode 100644 index 0000000..c743646 --- /dev/null +++ b/TaskAPI/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/TaskAPI/target/classes/org/example/TaskAPI$Task.class b/TaskAPI/target/classes/org/example/TaskAPI$Task.class new file mode 100644 index 0000000..4892f99 Binary files /dev/null and b/TaskAPI/target/classes/org/example/TaskAPI$Task.class differ diff --git a/TaskAPI/target/classes/org/example/TaskAPI.class b/TaskAPI/target/classes/org/example/TaskAPI.class new file mode 100644 index 0000000..4ad0b41 Binary files /dev/null and b/TaskAPI/target/classes/org/example/TaskAPI.class differ diff --git a/TaskAPI/target/classes/org/example/TaskRepository.class b/TaskAPI/target/classes/org/example/TaskRepository.class new file mode 100644 index 0000000..a092d10 Binary files /dev/null and b/TaskAPI/target/classes/org/example/TaskRepository.class differ