Skip to content

Commit

Permalink
segunda actualizacion
Browse files Browse the repository at this point in the history
  • Loading branch information
Pezcue committed Apr 9, 2024
1 parent 6be0e38 commit b30c696
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 21 deletions.
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/main/.DS_Store
Binary file not shown.
Binary file modified src/main/java/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions src/main/java/com/example/rest/Services/ProjectService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.rest.Services;

import com.example.rest.entities.Project;

public interface ProjectService {

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

// para editar projectos
Project editProject(Long id, Project project);

// para eliminar projectos
void deleteProject(Long id);

// para obtener projecto por id
Project getProjectById(Long id);
}
75 changes: 75 additions & 0 deletions src/main/java/com/example/rest/Services/ProjectServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.rest.Services;

import com.example.rest.entities.Project;
import com.example.rest.entities.projectStatus;
import com.example.rest.repositories.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ProjectServiceImpl implements ProjectService {

private final ProjectRepository projectRepository;

@Autowired
public ProjectServiceImpl(ProjectRepository projectRepository) {
this.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);
}


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

Optional <Project> optionalProject = projectRepository.findById(id);

if (optionalProject.isPresent()) {

projectRepository.save(project);

return project;
} else {
throw new RuntimeException("El proyecto no existe"); // Cambiar por exp personalizada
}
}

@Override
public void deleteProject(Long id) {
Optional <Project> projectOptional = projectRepository.findById(id);
if(projectOptional.isPresent()){
projectRepository.deleteById(id);
} else {
throw new RuntimeException("Proyecto con el id: " + id + " no existe.");
}
}

@Override
public Project getProjectById(Long id) {
Optional <Project> projectOptional = projectRepository.findById(id);
if(projectOptional.isPresent()) {
return projectOptional.get();
} else {
throw new RuntimeException("Proyecto con el id: " + id + " no existe.");
}
}

}

9 changes: 0 additions & 9 deletions src/main/java/com/example/rest/Services/ProjectoService.java

This file was deleted.

28 changes: 17 additions & 11 deletions src/main/java/com/example/rest/controllers/ProjectController.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
package com.example.rest.controllers;

import com.example.rest.Services.ProjectoService;
import com.example.rest.Services.ProjectServiceImpl;
import com.example.rest.entities.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;


@RestController
@RequestMapping("/v1/projects")
public class ProjectController {

// Injeccion del servicio
private final ProjectoService projectoService;
private ProjectServiceImpl projectService ;

@Autowired
public ProjectController(ProjectoService projectoService) {
this.projectoService = projectoService;
public ProjectController(ProjectServiceImpl projectService) {
this.projectService = projectService;
}


//POST -> /v1/projects crear un Project
@PostMapping
public ResponseEntity<Project> crearProjecto(@RequestBody Project project) {
Project projectoCreado = projectoService.crearProjecto(project);
public ResponseEntity<Project> createProject(@RequestBody Project project) {
Project projectoCreado = projectService.createProject(project);
return ResponseEntity.created(URI.create("/v1/projects" + projectoCreado.getId()))
.body(projectoCreado);
}

// PUT -> /v1/projects/{id} editar un Project
@PutMapping("/{id}")
public ResponseEntity<Project> editarProjecto(@PathVariable("id") Long id,
public ResponseEntity<Project> editProject(@PathVariable("id") Long id,
@RequestBody Project project) {

Project proyectoEditado = projectService.editProject(id, project);
return ResponseEntity.created(URI.create("/v1/projects" + proyectoEditado.getId()))
.body(proyectoEditado);
}

// DELETE -> /v1/projects/{id} eliminar un Project
public ResponseEntity<Void> eliminarProjecto(@PathVariable("id") Long id) {

projectService.deleteProject(id);
return ResponseEntity.noContent()
.build();
}

// GET -> /v1/projects/{id} obtener un Project por i
// GET -> /v1/projects/{id} obtener un Project por id
@GetMapping("/{id}")
public ResponseEntity<Project> obtenerProjecto(@PathVariable("id") Long id) {

Project project = projectService.getProjectById(id);
return ResponseEntity.ok(project);
}


Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/example/rest/entities/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Setter
@Getter
@Builder
Expand All @@ -22,6 +24,76 @@ public class Project {
@Column(name = "nombre")
private String name;

@Column(name = "descripcion")
private String description;

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

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

@Column(name = "fecha_actualizacion")
private LocalDateTime lastUpdatedDate;

@OneToMany(mappedBy = "project", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private List<Task> userProyectList = new ArrayList<>();


public Project(Long id, String name, String description, LocalDateTime createDate, LocalDateTime lastUpdatedDate, List<Task> userProyectList) {
this.id = id;
this.name = name;
this.description = description;
this.createDate = createDate;
this.lastUpdatedDate = lastUpdatedDate;
this.userProyectList = userProyectList;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public LocalDateTime getCreateDate() {
return createDate;
}

public LocalDateTime getLastUpdatedDate() {
return lastUpdatedDate;
}

public List<Task> getUserProyectList() {
return userProyectList;
}

public void setId(Long id) {
this.id = id;
}

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

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

public void setCreateDate(LocalDateTime createDate) {
this.createDate = createDate;
}

public void setLastUpdatedDate(LocalDateTime lastUpdatedDate) {
this.lastUpdatedDate = lastUpdatedDate;
}

public void setUserProyectList(List<Task> userProyectList) {
this.userProyectList = userProyectList;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/example/rest/entities/projectStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.rest.entities;

public enum projectStatus {
ACTIVE,
INACTIVE,
PAUSED
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package com.example.rest.repositories;

public interface ProjectRepository {
import com.example.rest.entities.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {

Project findByName(String name);
Project findProjectById(Long id);
}

0 comments on commit b30c696

Please sign in to comment.