feat: CRUD des tâches, amélioration de la structure de l'API
This commit is contained in:
@@ -1,26 +1,44 @@
|
||||
package myapi.api.Controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import myapi.api.Model.Task;
|
||||
import myapi.api.Repository.TaskRepository;
|
||||
import myapi.api.Service.TaskService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import myapi.api.Data.Repository.TaskRepository;
|
||||
import myapi.api.Model.Task;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/task")
|
||||
public class TaskController {
|
||||
private final TaskRepository taskRepository;
|
||||
|
||||
public TaskController(TaskRepository repository){
|
||||
this.taskRepository = repository;
|
||||
}
|
||||
@Autowired
|
||||
private TaskService taskService;
|
||||
|
||||
@GetMapping
|
||||
public List<Task> getTasks(){
|
||||
return this.taskRepository.findAll();
|
||||
public List<Task> getAllTasks() {
|
||||
return taskService.getAllTasks();
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public Task addTask(@RequestBody Task task) {
|
||||
return taskService.addTask(task);
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
public Task updateTask(@RequestBody Task task) {
|
||||
return taskService.updateTask(task);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public void deleteTask(@PathVariable("id") int idTask) {
|
||||
taskService.deleteTask(idTask);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package myapi.api.Data.Repository;
|
||||
package myapi.api.Repository;
|
||||
|
||||
import myapi.api.Model.Task;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
@@ -0,0 +1,35 @@
|
||||
package myapi.api.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import myapi.api.Model.Task;
|
||||
|
||||
public interface TaskService {
|
||||
/**
|
||||
* Récupère toutes les tâches de la database
|
||||
* @return Une liste de tâches
|
||||
*/
|
||||
List<Task> getAllTasks();
|
||||
|
||||
/**
|
||||
* Ajoute une nouvelle tâche
|
||||
* @param task La nouvelle tâche
|
||||
* @return La tâche nouvellement créée
|
||||
*/
|
||||
Task addTask(Task task);
|
||||
|
||||
/**
|
||||
* Modifie une tâche déjà existante
|
||||
* @param task La tâche à modifier. Il faut que son id existe déjà dans la database
|
||||
* @return La tâche modifiée
|
||||
*/
|
||||
Task updateTask(Task task);
|
||||
|
||||
/**
|
||||
* Supprime une tâche existante à partir de son identifiant
|
||||
* @param taskId L'id de la tâche à supprimer
|
||||
*/
|
||||
void deleteTask(int taskId);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package myapi.api.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import myapi.api.Model.Task;
|
||||
import myapi.api.Repository.TaskRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TaskServiceImpl implements TaskService {
|
||||
|
||||
@Autowired
|
||||
private TaskRepository taskRepository;
|
||||
|
||||
@Override
|
||||
public List<Task> getAllTasks() {
|
||||
return taskRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task addTask(Task task) {
|
||||
return taskRepository.save(task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task updateTask(Task task) {
|
||||
Task dbTask = taskRepository.findById(task.getId_task()).get();
|
||||
dbTask.setName_task(task.getName_task());
|
||||
dbTask.setStatus_task(task.getStatus_task());
|
||||
dbTask.setDate_task(task.getDate_task());
|
||||
dbTask.setPriority_task(task.getPriority_task());
|
||||
dbTask.setId_group(task.getId_group());
|
||||
|
||||
return taskRepository.save(dbTask);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTask(int taskId) {
|
||||
taskRepository.deleteById(taskId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user