Skip to content

Commit

Permalink
Base de Datos y DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
Pezcue committed Apr 12, 2024
1 parent b30c696 commit 376cab6
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 41 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/example/rest/DTO/ProjectDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.rest.DTO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProjectDTO {
private String name;
private String description;gi

public ProjectDTO() {
}

public ProjectDTO(String name, String description) {
this.name = name;
this.description = description;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/example/rest/DTO/TaskDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.rest.DTO;

import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
public class TaskDTO {

private String name;
private String description;
private String type;
private LocalDate startDate;
private LocalDate dueDate;

public TaskDTO(String name, String description, String type, LocalDate startDate, LocalDate dueDate) {
this.name = name;
this.description = description;
this.type = type;
this.startDate = startDate;
this.dueDate = dueDate;
}

public TaskDTO() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public LocalDate getStartDate() {
return startDate;
}

public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}

public LocalDate getDueDate() {
return dueDate;
}

public void setDueDate(LocalDate dueDate) {
this.dueDate = dueDate;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/rest/Services/ProjectService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.example.rest.Services;

import com.example.rest.DTO.ProjectDTO;
import com.example.rest.entities.Project;

public interface ProjectService {

// para crear projectos
Project createProject(Project project);
Project createProject(ProjectDTO projectDTO);

// para editar projectos
Project editProject(Long id, Project project);
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/com/example/rest/Services/ProjectServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.rest.Services;

import com.example.rest.DTO.ProjectDTO;
import com.example.rest.entities.Project;
import com.example.rest.entities.projectStatus;
import com.example.rest.repositories.ProjectRepository;
Expand All @@ -20,22 +21,20 @@ public ProjectServiceImpl(ProjectRepository projectRepository) {
}

@Override
public Project createProject(Project project) {

// Chequear que el projecto no sea nulo y tenga un nombre
if (project == null || project.getName() == null || project.getName().isEmpty()) {
throw new IllegalArgumentException("Invalid project details");
}

// Aregarle los atributos por defecto
project.setStatus(projectStatus.ACTIVE);
project.setCreateDate(LocalDateTime.now());

// Guardar (?) Preguntar
return projectRepository.save(project);
public Project createProject(ProjectDTO projectDTO) {

Project newProject = new Project();
newProject.setName(projectDTO.getName());
newProject.setDescription(projectDTO.getDescription());
newProject.setStatus(projectStatus.ACTIVE.toString());
newProject.setCreateDate(LocalDateTime.now());
newProject.setLastUpdatedDate(LocalDateTime.now());

// Guardar
Project savedProject = projectRepository.save(newProject);
return savedProject;
}


@Override
public Project editProject(Long id, Project project) {

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/example/rest/Services/TaskService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.rest.Services;

import com.example.rest.DTO.TaskDTO;
import com.example.rest.entities.Task;

public interface TaskService {


// para crea tareas

Task createTask(long project_id, TaskDTO taskDTO);

// para eliminar tareas

void deleteTask(Long task_id);

// para obtener tareas

Task getTaskById(Long task_id);

}
67 changes: 67 additions & 0 deletions src/main/java/com/example/rest/Services/TaskServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.example.rest.Services;

import com.example.rest.DTO.TaskDTO;
import com.example.rest.entities.Project;
import com.example.rest.entities.Task;
import com.example.rest.entities.TaskStatus;
import com.example.rest.repositories.ProjectRepository;
import com.example.rest.repositories.TaskRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class TaskServiceImpl implements TaskService {

private final TaskRepository taskRepository;
private final ProjectRepository projectRepository;

public TaskServiceImpl(TaskRepository taskRepository, ProjectRepository projectRepository) {
this.taskRepository = taskRepository;
this.projectRepository = projectRepository;
}

@Override
public Task createTask(long project_id, TaskDTO taskDTO) {

Project proyectoBaseDeDatos = projectRepository.findProjectById(project_id);
if (proyectoBaseDeDatos != null) {
throw new RuntimeException("Ya existe en la base de datos");
}

Task newTask = new Task();
newTask.setName(taskDTO.getName());
newTask.setDescription(taskDTO.getDescription());
newTask.setTaskStatus(TaskStatus.TODO.toString());
newTask.setType(taskDTO.getType());
newTask.setStartDate(taskDTO.getStartDate());
newTask.setDueDate(taskDTO.getDueDate());
newTask.setCreateDate(LocalDate.now());
newTask.setLastUpdatedDate(LocalDateTime.now());

Task savedTask = taskRepository.save(newTask);
return newTask;
}

@Override
public void deleteTask(Long task_id) {
Optional <Task> taskOptional = taskRepository.findById(task_id);
if(taskOptional.isPresent()){
taskRepository.deleteById(task_id);
} else {
throw new RuntimeException("La tarea no existe"); // cambiar
}
}

@Override
public Task getTaskById(Long task_id) {
Optional <Task> taskOptional = taskRepository.findById(task_id);
if(taskOptional.isPresent()){
return taskOptional.get();
} else {
throw new RuntimeException("La tarea no existe"); // cambiar
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/example/rest/SpringBootRestApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.rest;

import com.example.rest.entities.Project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand All @@ -8,6 +9,8 @@ public class SpringBootRestApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootRestApplication.class, args);


}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.rest.controllers;

import com.example.rest.DTO.ProjectDTO;
import com.example.rest.Services.ProjectServiceImpl;
import com.example.rest.entities.Project;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,9 +24,9 @@ public ProjectController(ProjectServiceImpl projectService) {


//POST -> /v1/projects crear un Project
@PostMapping
public ResponseEntity<Project> createProject(@RequestBody Project project) {
Project projectoCreado = projectService.createProject(project);
@PostMapping("/")
public ResponseEntity<Project> createProject(@RequestBody ProjectDTO projectDTO) {
Project projectoCreado = projectService.createProject(projectDTO);
return ResponseEntity.created(URI.create("/v1/projects" + projectoCreado.getId()))
.body(projectoCreado);
}
Expand All @@ -48,11 +49,9 @@ public ResponseEntity<Void> eliminarProjecto(@PathVariable("id") Long id) {

// GET -> /v1/projects/{id} obtener un Project por id
@GetMapping("/{id}")
public ResponseEntity<Project> obtenerProjecto(@PathVariable("id") Long id) {
public ResponseEntity<Project> getProject(@PathVariable("id") Long id) {
Project project = projectService.getProjectById(id);
return ResponseEntity.ok(project);
}



}

This file was deleted.

43 changes: 43 additions & 0 deletions src/main/java/com/example/rest/controllers/TaskController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.rest.controllers;

import com.example.rest.DTO.TaskDTO;
import com.example.rest.Services.TaskServiceImpl;
import com.example.rest.entities.Task;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

@RestController
@RequestMapping("/v1/task/")
public class TaskController {

//Injeccion del servicio para Task

private TaskServiceImpl taskService;

// POST // Crear tarea
@PostMapping("/project/{id}")
public ResponseEntity<Task> createTask(@PathVariable("id") Long project_id, @RequestBody TaskDTO taskDTO) {
Task tareaCreada = taskService.createTask(project_id, taskDTO);
return ResponseEntity.created(URI.create("/v1/task" + tareaCreada.getId()))
.body(tareaCreada);
}

// DELETE // Eliminar un Task

public ResponseEntity<Void> deleteTask(@PathVariable("id") Long task_id) {
taskService.deleteTask(task_id);
return ResponseEntity.noContent()
.build();
}

// GET // Obtener un Task

@GetMapping("/{id}")
public ResponseEntity<Task> getTask(@PathVariable("id") Long task_id){
Task task = taskService.getTaskById(task_id);
return ResponseEntity.ok(task);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/example/rest/entities/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Project {
private String description;

@Column(name = "status")
private projectStatus status;
private String status;

@Column(name = "fecha_creacion")
private LocalDateTime createDate;
Expand Down
Loading

0 comments on commit 376cab6

Please sign in to comment.