Final #1
@ -1,53 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>TaskAPI</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.5</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</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>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>3.2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,90 +0,0 @@
|
||||
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<Task> 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<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) {
|
||||
SpringApplication.run(TaskAPI.class, args);
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package org.example;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface TaskRepository extends JpaRepository<TaskAPI.Task, Long> {
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
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
|
@ -1,5 +0,0 @@
|
||||
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
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user