commit
d76ed39220
19
pom.xml
19
pom.xml
@ -1,9 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>org.example</groupId>
|
<groupId>org.example</groupId>
|
||||||
<artifactId>TaskAPI</artifactId>
|
<artifactId>TaskAPI</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
@ -26,11 +26,28 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-jpa</artifactId>
|
||||||
|
<version>3.2.5</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,79 +1,90 @@
|
|||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
public class TaskAPI {
|
public class TaskAPI {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TaskRepository taskRepository;
|
||||||
|
|
||||||
|
@Entity
|
||||||
public static class Task {
|
public static class Task {
|
||||||
private int id;
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
private String title;
|
private String title;
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
public Task(String title, String description) {
|
||||||
public Task(int id, String title, String description) {
|
|
||||||
this.id = id;
|
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task() {
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle(){return title;}
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
public String getDescription(){return description;}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
private List<Task> tasks = new ArrayList<>();
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
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 setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Получение задания по id
|
// Получение задания по id
|
||||||
@GetMapping("/tasks/{taskId}")
|
@GetMapping("/tasks/{taskId}")
|
||||||
public Task getTask(@PathVariable int taskId) {
|
public Task getTask(@PathVariable Long taskId) {
|
||||||
return tasks.stream()
|
return taskRepository.findById(taskId).orElse(null);
|
||||||
.filter(task -> task.getId() == taskId)
|
|
||||||
.findFirst()
|
|
||||||
.orElseThrow(() -> new RuntimeException("Задание не найдено"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Получение всех заданий
|
// Получение всех заданий
|
||||||
@GetMapping("/tasks")
|
@GetMapping("/tasks")
|
||||||
public List<Task> getTasks() {
|
public List<Task> getTasks() {
|
||||||
return tasks;
|
return taskRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
//добавление задания
|
// Добавление нового задания
|
||||||
@PostMapping("/tasks")
|
@PostMapping("/tasks")
|
||||||
public Task createTask(@RequestBody Task newTask) {
|
public Task createTask(@RequestBody Task newTask) {
|
||||||
newTask.setId(tasks.size() + 1);
|
return taskRepository.save(newTask);
|
||||||
tasks.add(newTask);
|
}
|
||||||
return newTask;
|
|
||||||
|
// Удаление задания по id
|
||||||
|
@DeleteMapping("/tasks/{taskId}")
|
||||||
|
public String deleteTaskById(@PathVariable Long taskId) {
|
||||||
|
Optional<Task> 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) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(TaskAPI.class, args);
|
SpringApplication.run(TaskAPI.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
src/main/java/org/example/TaskRepository.java
Normal file
6
src/main/java/org/example/TaskRepository.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface TaskRepository extends JpaRepository<TaskAPI.Task, Long> {
|
||||||
|
}
|
5
src/main/resources/application.properties
Normal file
5
src/main/resources/application.properties
Normal file
@ -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
|
5
target/classes/application.properties
Normal file
5
target/classes/application.properties
Normal file
@ -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
|
BIN
target/classes/org/example/TaskAPI$Task.class
Normal file
BIN
target/classes/org/example/TaskAPI$Task.class
Normal file
Binary file not shown.
BIN
target/classes/org/example/TaskAPI.class
Normal file
BIN
target/classes/org/example/TaskAPI.class
Normal file
Binary file not shown.
BIN
target/classes/org/example/TaskRepository.class
Normal file
BIN
target/classes/org/example/TaskRepository.class
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user