Skip to content

Commit

Permalink
feat: Services e Controllers das novas Entidades Implementados
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacAndra committed Sep 22, 2024
1 parent d9bf7ef commit ba86cd5
Show file tree
Hide file tree
Showing 10 changed files with 666 additions and 0 deletions.
154 changes: 154 additions & 0 deletions src/main/java/com/isaacandrade/blog/controller/ProjectController.java
Original file line number Diff line number Diff line change
@@ -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<List<ProjectDto>> getAllProjects(){
List<ProjectDto> 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<ProjectDto> 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<ProjectDto> 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<Optional<ProjectDto>> updateProject(@PathVariable Long id, @RequestBody CreateProjectDto data) {
Optional<ProjectDto> 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<ProjectDto> deleteProject(@PathVariable Long id) {
projectService.deleteProject(id);

return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -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<TechnologyDto> 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<Technology> getTechnologyById(@PathVariable Long id) {
Optional<Technology> 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<List<Technology>> getAllTechnologies() {
List<Technology> 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<List<Technology>> getTechnologyByType(@PathVariable String type) {
TechnologyType technologyType = TechnologyType.valueOf(type.toUpperCase());
List<Technology> 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<Technology> updateTechnology(@PathVariable Long id, @RequestBody Technology updatedTechnology) {
Optional<Technology> 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<Void> deleteTechnology(@PathVariable Long id) {
technologyService.deleteTechnology(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.isaacandrade.blog.domain.projects;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ProjectRepository extends JpaRepository<Project, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.isaacandrade.blog.domain.technologies;

public record CreateTechnologyDto(
String name,
TechnologyType type

) {

}
Loading

0 comments on commit ba86cd5

Please sign in to comment.