Compare commits

...

4 Commits

Author SHA1 Message Date
d76ed39220 Merge pull request 'Final' (#1) from Final into master
Reviewed-on: #1
2024-07-05 00:47:42 +00:00
Wanster
38d208ea5a
Add files via upload 2024-07-05 03:37:54 +03:00
Wanster
11ca734d07
Delete TaskAPI directory 2024-07-05 03:36:38 +03:00
Wanster
87f3e6abda
Add files via upload 2024-07-05 03:36:09 +03:00
8 changed files with 104 additions and 60 deletions

69
pom.xml
View File

@ -1,36 +1,53 @@
<?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>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
</parent>
<groupId>org.example</groupId>
<artifactId>TaskAPI</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>

View File

@ -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<Task> 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<Task> 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<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);
}
}

View File

@ -0,0 +1,6 @@
package org.example;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TaskRepository extends JpaRepository<TaskAPI.Task, Long> {
}

View 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

View 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

Binary file not shown.

Binary file not shown.

Binary file not shown.