commit 7dde7eb598c6e12096a8cb7eac6c39031bf560bd Author: Daniil Date: Thu Jul 4 02:41:34 2024 +0300 API diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a74e8ef --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d254d86 --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + + +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-test + test + + + + \ No newline at end of file diff --git a/src/main/java/org/example/TaskAPI.java b/src/main/java/org/example/TaskAPI.java new file mode 100644 index 0000000..eb01224 --- /dev/null +++ b/src/main/java/org/example/TaskAPI.java @@ -0,0 +1,79 @@ +package org.example; + +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; + +@SpringBootApplication +@RestController +public class TaskAPI { + + public static class Task { + private int id; + private String title; + private String description; + + + public Task(int id, String title, String description) { + this.id = id; + this.title = title; + this.description = description; + + } + + + public void setId(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public String getTitle(){return title;} + + 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")); + + } + + // Получение задания по id + @GetMapping("/tasks/{taskId}") + public Task getTask(@PathVariable int taskId) { + return tasks.stream() + .filter(task -> task.getId() == taskId) + .findFirst() + .orElseThrow(() -> new RuntimeException("Задание не найдено")); + } + + // Получение всех заданий + @GetMapping("/tasks") + public List getTasks() { + return tasks; + } + + //добавление задания + @PostMapping("/tasks") + public Task createTask(@RequestBody Task newTask) { + newTask.setId(tasks.size() + 1); + tasks.add(newTask); + return newTask; + } + + public static void main(String[] args) { + SpringApplication.run(TaskAPI.class, args); + } +} +