diff --git a/src/main/java/com/isaacandrade/blog/controller/ProjectController.java b/src/main/java/com/isaacandrade/blog/controller/ProjectController.java new file mode 100644 index 0000000..b5a5652 --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/controller/ProjectController.java @@ -0,0 +1,154 @@ +package com.isaacandrade.blog.controller; + +import com.isaacandrade.blog.domain.projects.CreateProjectDto; +import com.isaacandrade.blog.domain.projects.ProjectDto; +import com.isaacandrade.blog.domain.user.UserDTO; +import com.isaacandrade.blog.service.ProjectService; +import com.isaacandrade.blog.utils.MediaType; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/projects") +@Tag(name = "Projetos" , description = "Endpoints para Gerenciar os Projetos") +public class ProjectController { + + @Autowired + ProjectService projectService; + + + @GetMapping( + produces = {MediaType.APPLICATION_JSON, + MediaType.APPLICATION_XML, + MediaType.APPLICATION_YML + } + ) + @Operation( + summary = "Encontre todos os Projetos", + description = "Encontre todos os Projetos", + tags = {"Projetos"}, + responses = { + @ApiResponse( + description = "Success", + responseCode = "200", + content = @Content( + mediaType = "application/json", + array = @ArraySchema(schema = @Schema(implementation = ProjectDto.class)) + ) + ), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unautorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity> getAllProjects(){ + List projects = projectService.getAllProjects(); + return ResponseEntity.ok(projects); + } + + + @GetMapping( + value = "/{id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} + ) + @Operation(summary = "Encontre o Projeto pelo Id", description = "Encontre o Usuário pelo Id", + tags = {"Projetos"}, + responses = { + @ApiResponse(description = "Success", responseCode = "200", + content = @Content(schema = @Schema(implementation = ProjectDto.class)) + ), + @ApiResponse(description = "No Content", responseCode = "204", content = @Content), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unautorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity getProjectById(@PathVariable Long id) { + ProjectDto projectById = projectService.getProjectById(id); + + return ResponseEntity.ok(projectById); + } + + + @PostMapping( + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} + ) + @Transactional + @Operation( + summary = "Cria um novo projeto", + description = "Cria um novo projeto passando uma representação em JSON, XML ou YML ", + tags = {"Projetos"}, + responses = { + @ApiResponse(description = "Success", responseCode = "200", + content = @Content(schema = @Schema(implementation = ProjectDto.class)) + ), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unauthorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity createProject(@RequestBody CreateProjectDto data) { + ProjectDto creatingProject = projectService.createProject(data); + return new ResponseEntity<>(creatingProject, HttpStatus.CREATED); + } + + @PutMapping( + value = "/{id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} + ) + @Transactional + @Operation( + summary = "Atualiza os dados de um projeto", + description = "Atualiza os dados de um projeto passando uma representação em JSON, XML ou YML ", + tags = {"Projetos"}, + responses = { + @ApiResponse(description = "Updated", responseCode = "200", + content = @Content(schema = @Schema(implementation = ProjectDto.class)) + ), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unauthorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity> updateProject(@PathVariable Long id, @RequestBody CreateProjectDto data) { + Optional updatingProject = projectService.updateProject(id, data); + + return ResponseEntity.ok(updatingProject); + } + + @DeleteMapping("/{id}") + @Operation( + summary = "Deleta um projeto", + description = "Deleta um projeto passando uma representação em JSON, XML ou YML ", + tags = {"Projetos"}, + responses = { + @ApiResponse(description = "No Content", responseCode = "204", content = @Content), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unauthorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity deleteProject(@PathVariable Long id) { + projectService.deleteProject(id); + + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/com/isaacandrade/blog/controller/TechnologyController.java b/src/main/java/com/isaacandrade/blog/controller/TechnologyController.java new file mode 100644 index 0000000..43221f1 --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/controller/TechnologyController.java @@ -0,0 +1,177 @@ +package com.isaacandrade.blog.controller; + +import com.isaacandrade.blog.domain.technologies.CreateTechnologyDto; +import com.isaacandrade.blog.domain.technologies.Technology; +import com.isaacandrade.blog.domain.technologies.TechnologyDto; +import com.isaacandrade.blog.domain.technologies.TechnologyType; +import com.isaacandrade.blog.service.TechnologyService; +import com.isaacandrade.blog.utils.MediaType; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/technologies") +@Tag(name = "Tecnologias" , description = "Endpoints para Gerenciar os Tecnologias") +public class TechnologyController { + + @Autowired + private TechnologyService technologyService; + + // Endpoint para criar uma nova tecnologia + @PostMapping( + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} + ) + @Transactional + @Operation( + summary = "Cria uma nova Tecnologia", + description = "Cria uma nova tecnologia passando uma representação em JSON, XML ou YML ", + tags = {"Tecnologias"}, + responses = { + @ApiResponse(description = "Success", responseCode = "200", + content = @Content(schema = @Schema(implementation = Technology.class)) + ), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unauthorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity createTechnology(@RequestBody CreateTechnologyDto data) { + TechnologyDto createdTechnology = technologyService.createTechnology(data); + + return ResponseEntity.status(HttpStatus.CREATED).body(createdTechnology); + } + + // Endpoint para obter uma tecnologia pelo ID + @GetMapping( + value = "/{id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} + ) + @Operation(summary = "Encontre a Tecnologia pelo Id", description = "Encontre a Tecnologia pelo Id", + tags = {"Tecnologias"}, + responses = { + @ApiResponse(description = "Success", responseCode = "200", + content = @Content(schema = @Schema(implementation = Technology.class)) + ), + @ApiResponse(description = "No Content", responseCode = "204", content = @Content), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unautorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity getTechnologyById(@PathVariable Long id) { + Optional technology = technologyService.getTechnologyById(id); + return technology.map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build()); + } + + // Endpoint para listar todas as tecnologias + @GetMapping( + produces = {MediaType.APPLICATION_JSON, + MediaType.APPLICATION_XML, + MediaType.APPLICATION_YML + }) + @Operation( + summary = "Encontre todas as Tecnologias", + description = "Encontre todas as Tecnologias", + tags = {"Tecnologias"}, + responses = { + @ApiResponse( + description = "Success", + responseCode = "200", + content = @Content( + mediaType = "application/json", + array = @ArraySchema(schema = @Schema(implementation = Technology.class)) + ) + ), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unautorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity> getAllTechnologies() { + List technologies = technologyService.getAllTechnologies(); + return ResponseEntity.ok(technologies); + } + + // Endpoint para obter tecnologias por tipo + @GetMapping("/type/{type}") + @Operation(summary = "Encontre a Tecnologia pelo tipo", description = "Encontre a Tecnologia pelo tipo", + tags = {"Tecnologias"}, + responses = { + @ApiResponse(description = "Success", responseCode = "200", + content = @Content(schema = @Schema(implementation = Technology.class)) + ), + @ApiResponse(description = "No Content", responseCode = "204", content = @Content), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unautorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity> getTechnologyByType(@PathVariable String type) { + TechnologyType technologyType = TechnologyType.valueOf(type.toUpperCase()); + List technologies = technologyService.getTechnologyByType(technologyType); + return ResponseEntity.ok(technologies); + } + + // Endpoint para atualizar uma tecnologia + @PutMapping( + value = "/{id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} + ) + @Transactional + @Operation( + summary = "Atualiza os dados de uma tecnologia", + description = "Atualiza os dados de uma tecnologia passando uma representação em JSON, XML ou YML ", + tags = {"Tecnologias"}, + responses = { + @ApiResponse(description = "Updated", responseCode = "200", + content = @Content(schema = @Schema(implementation = Technology.class)) + ), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unauthorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity updateTechnology(@PathVariable Long id, @RequestBody Technology updatedTechnology) { + Optional technology = technologyService.updateTechnology(id, updatedTechnology); + return technology.map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build()); + } + + // Endpoint para deletar uma tecnologia + @DeleteMapping("/{id}") + @Operation( + summary = "Deleta uma tecnologia", + description = "Deleta uma tecnologia passando uma representação em JSON, XML ou YML ", + tags = {"Tecnologias"}, + responses = { + @ApiResponse(description = "No Content", responseCode = "204", content = @Content), + @ApiResponse(description = "Bad Request", responseCode = "400", content = @Content), + @ApiResponse(description = "Unauthorized", responseCode = "401", content = @Content), + @ApiResponse(description = "Not Found", responseCode = "404", content = @Content), + @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), + } + ) + public ResponseEntity deleteTechnology(@PathVariable Long id) { + technologyService.deleteTechnology(id); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/isaacandrade/blog/domain/projects/CreateProjectDto.java b/src/main/java/com/isaacandrade/blog/domain/projects/CreateProjectDto.java new file mode 100644 index 0000000..3bfdb56 --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/domain/projects/CreateProjectDto.java @@ -0,0 +1,21 @@ +package com.isaacandrade.blog.domain.projects; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.isaacandrade.blog.domain.technologies.TechnologyDto; +@JsonPropertyOrder({"name", "description", "technologies", "url", "url_github"}) +public record CreateProjectDto( + @JsonProperty(value = "name") + String name, + @JsonProperty(value = "description") + String description, + + @JsonProperty(value = "technologies") + TechnologyDto technologies, + + @JsonProperty(value = "url") + String url, + @JsonProperty(value = "url_github") + String urlGitHub +) { +} diff --git a/src/main/java/com/isaacandrade/blog/domain/projects/ProjectDto.java b/src/main/java/com/isaacandrade/blog/domain/projects/ProjectDto.java new file mode 100644 index 0000000..a88b892 --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/domain/projects/ProjectDto.java @@ -0,0 +1,25 @@ +package com.isaacandrade.blog.domain.projects; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.isaacandrade.blog.domain.technologies.TechnologyDto; + +@JsonPropertyOrder({"id", "name", "description", "technologies", "url", "url_github"}) +public record ProjectDto( + @JsonProperty(value = "id") + Long id, + + @JsonProperty(value = "name") + String name, + @JsonProperty(value = "description") + String description, + + @JsonProperty(value = "technologies") + TechnologyDto technologies, + + @JsonProperty(value = "url") + String url, + @JsonProperty(value = "url_github") + String gitHub +) { +} diff --git a/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java b/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java new file mode 100644 index 0000000..68cbcf5 --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java @@ -0,0 +1,6 @@ +package com.isaacandrade.blog.domain.projects; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProjectRepository extends JpaRepository { +} diff --git a/src/main/java/com/isaacandrade/blog/domain/technologies/CreateTechnologyDto.java b/src/main/java/com/isaacandrade/blog/domain/technologies/CreateTechnologyDto.java new file mode 100644 index 0000000..b41de5d --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/domain/technologies/CreateTechnologyDto.java @@ -0,0 +1,9 @@ +package com.isaacandrade.blog.domain.technologies; + +public record CreateTechnologyDto( + String name, + TechnologyType type + +) { + +} diff --git a/src/main/java/com/isaacandrade/blog/domain/technologies/TechnologyDto.java b/src/main/java/com/isaacandrade/blog/domain/technologies/TechnologyDto.java new file mode 100644 index 0000000..8be682c --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/domain/technologies/TechnologyDto.java @@ -0,0 +1,10 @@ +package com.isaacandrade.blog.domain.technologies; + +import java.util.List; + +public record TechnologyDto( + List frontend, + List backend +) { + +} diff --git a/src/main/java/com/isaacandrade/blog/domain/technologies/TechnologyRepository.java b/src/main/java/com/isaacandrade/blog/domain/technologies/TechnologyRepository.java new file mode 100644 index 0000000..f806afb --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/domain/technologies/TechnologyRepository.java @@ -0,0 +1,11 @@ +package com.isaacandrade.blog.domain.technologies; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface TechnologyRepository extends JpaRepository { + Technology findByNameAndType(String name, TechnologyType type); + + List findByType(TechnologyType type); +} diff --git a/src/main/java/com/isaacandrade/blog/service/ProjectService.java b/src/main/java/com/isaacandrade/blog/service/ProjectService.java new file mode 100644 index 0000000..5d2e36b --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/service/ProjectService.java @@ -0,0 +1,186 @@ +package com.isaacandrade.blog.service; + +import com.isaacandrade.blog.domain.projects.CreateProjectDto; +import com.isaacandrade.blog.domain.projects.Project; +import com.isaacandrade.blog.domain.projects.ProjectDto; +import com.isaacandrade.blog.domain.projects.ProjectRepository; +import com.isaacandrade.blog.domain.technologies.Technology; +import com.isaacandrade.blog.domain.technologies.TechnologyDto; +import com.isaacandrade.blog.domain.technologies.TechnologyRepository; +import com.isaacandrade.blog.domain.technologies.TechnologyType; +import com.isaacandrade.blog.exception.ConstraintViolationException; +import com.isaacandrade.blog.exception.ProjectNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.*; +import java.util.stream.Collectors; + +@Service +public class ProjectService { + + @Autowired + ProjectRepository projectRepository; + + @Autowired + TechnologyRepository technologyRepository; + + // Metodo que para listar todos os projetos + public List getAllProjects() { + List projects = projectRepository.findAll(); + if (projects.isEmpty()){ + throw new ProjectNotFoundException(); + } + return projects.stream().map(this::mapToProjectDTO).collect(Collectors.toList()); + } + + // Metodo para mostrar um projeto pelo id + public ProjectDto getProjectById(Long id) { + Project project = projectRepository.findById(id).orElseThrow( + () -> new ProjectNotFoundException("Project With id " + id + " Was Not Found!") + ); + + return mapToProjectDTO(project); + } + + public ProjectDto createProject(CreateProjectDto data) { + // 1. Criar e salvar o projeto sem as tecnologias + Project project = new Project(); + project.setName(data.name()); + project.setDescription(data.description()); + project.setUrl(data.url()); + project.setUrlGitHub(data.urlGitHub()); + + if (data.name() == null || data.description() == null || data.technologies() == null || data.url() == null || data.urlGitHub() == null) { + throw new ConstraintViolationException("Name, Description, Technologies, Url and GitHubUrl Cannot Be Null!"); + } + + project = projectRepository.save(project); + + // 2. Associa as tecnologias ao projeto usando os nomes e tipos + Project finalProject = project; + Set frontendTechnologies = data.technologies().frontend().stream() + .map(techName -> getOrCreateTechnology(techName, TechnologyType.FRONTEND, finalProject)) + .collect(Collectors.toSet()); + + Project finalProject1 = project; + Set backendTechnologies = data.technologies().backend().stream() + .map(techName -> getOrCreateTechnology(techName, TechnologyType.BACKEND, finalProject1)) + .collect(Collectors.toSet()); + + // 3. Salvar todas as tecnologias (novas ou atualizadas) + Set allTechnologies = new HashSet<>(frontendTechnologies); + allTechnologies.addAll(backendTechnologies); + technologyRepository.saveAll(allTechnologies); + + // 4. Atualizar o projeto com as tecnologias + project.setTechnologies(new ArrayList<>(allTechnologies)); + + // 5. Retornar o DTO atualizado do projeto + return mapToProjectDTO(projectRepository.save(project)); + } + + public Optional updateProject(Long id, CreateProjectDto data) { + + projectRepository.findById(id).orElseThrow(() -> + new ProjectNotFoundException("Project With id " + id + " Was Not Found!")); + + return projectRepository.findById(id).map(existingProject -> { + // Atualiza as informações básicas do projeto + existingProject.setName(data.name()); + existingProject.setDescription(data.description()); + existingProject.setUrl(data.url()); + existingProject.setUrlGitHub(data.urlGitHub()); + + + // Remove as tecnologias antigas + List existingTechnologies = existingProject.getTechnologies(); + technologyRepository.deleteAll(existingTechnologies); + + // Cria novas tecnologias com base no DTO + List newTechnologies = new ArrayList<>(); + + // Adiciona tecnologias de frontend + for (String techName : data.technologies().frontend()) { + newTechnologies.add(convertTechnologyDTOToEntity(techName, TechnologyType.FRONTEND, existingProject)); + } + + // Adiciona tecnologias de backend + for (String techName : data.technologies().backend()) { + newTechnologies.add(convertTechnologyDTOToEntity(techName, TechnologyType.BACKEND, existingProject)); + } + + // Salva as novas tecnologias + technologyRepository.saveAll(newTechnologies); + + // Atualiza o projeto com as novas tecnologias + existingProject.setTechnologies(newTechnologies); + Project updatedProject = projectRepository.save(existingProject); + + // Retorna o DTO atualizado + return mapToProjectDTO(updatedProject); + }); + } + + public void deleteProject(Long id) { + + projectRepository.findById(id).orElseThrow(() -> new ProjectNotFoundException("Project With id " + id + " Was Not Found!")); + + projectRepository.findById(id).ifPresent(project -> { + technologyRepository.deleteAll(project.getTechnologies()); // Remove todas as tecnologias associadas + projectRepository.delete(project); // Remove o projeto + }); + } + + + + + + + private ProjectDto mapToProjectDTO(Project project) { + // Agrupa as tecnologias em frontend e backend + TechnologyDto technologyGroupDto = new TechnologyDto( + project.getTechnologies().stream() + .filter(tech -> tech.getType() == TechnologyType.FRONTEND) + .map(Technology::getName) + .collect(Collectors.toList()), + project.getTechnologies().stream() + .filter(tech -> tech.getType() == TechnologyType.BACKEND) + .map(Technology::getName) + .collect(Collectors.toList()) + ); + return new ProjectDto( + project.getId(), + project.getName(), + project.getDescription(), + technologyGroupDto, // Usa o DTO agrupado + project.getUrl(), + project.getUrlGitHub() + ); + } + + + + private Technology convertTechnologyDTOToEntity(String name, TechnologyType type, Project project) { + Technology technology = new Technology(); + technology.setName(name); + technology.setType(type); + technology.setProject(project); + return technology; + } + + + + private Technology getOrCreateTechnology(String name, TechnologyType type, Project project) { + Technology technology = technologyRepository.findByNameAndType(name, type); + if (technology == null) { + technology = new Technology(); + technology.setName(name); + technology.setType(type); + technology.setProject(project); + } else { + technology.setProject(project); + } + return technology; + } +} diff --git a/src/main/java/com/isaacandrade/blog/service/TechnologyService.java b/src/main/java/com/isaacandrade/blog/service/TechnologyService.java new file mode 100644 index 0000000..ab48cc7 --- /dev/null +++ b/src/main/java/com/isaacandrade/blog/service/TechnologyService.java @@ -0,0 +1,67 @@ +package com.isaacandrade.blog.service; + +import com.isaacandrade.blog.domain.technologies.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +public class TechnologyService { + + @Autowired + TechnologyRepository technologyRepository; + + + public TechnologyDto createTechnology(CreateTechnologyDto data) { + CreateTechnologyDto newTechnology = new CreateTechnologyDto(data.name(), data.type()); + + Technology technology = new Technology(newTechnology); + Technology savedTechnology = technologyRepository.save(technology); + + return mapToTechnologyDTO((List) savedTechnology); + } + + public Optional getTechnologyById(Long id) { + return technologyRepository.findById(id); + } + + public List getAllTechnologies() { + return technologyRepository.findAll(); + } + + public List getTechnologyByType(TechnologyType type) { + return technologyRepository.findByType(type); + } + + public Optional updateTechnology(Long id, Technology updatedTechnology) { + return technologyRepository.findById(id).map(existingTechnology -> { + existingTechnology.setName(updatedTechnology.getName()); + existingTechnology.setType(updatedTechnology.getType()); + return technologyRepository.save(existingTechnology); + + }); + } + + public void deleteTechnology(Long id) { + technologyRepository.deleteById(id); + } + + private TechnologyDto mapToTechnologyDTO(List technologies) { + // Separando tecnologias por tipo + List frontend = technologies.stream() + .filter(tech -> tech.getType() == TechnologyType.FRONTEND) + .map(Technology::getName) + .collect(Collectors.toList()); + + List backend = technologies.stream() + .filter(tech -> tech.getType() == TechnologyType.BACKEND) + .map(Technology::getName) + .collect(Collectors.toList()); + + // Retornando um DTO agrupado + return new TechnologyDto(frontend, backend); + } +}