Skip to content

Commit 677d3d3

Browse files
committed
feat: implement product controller with CRUD-Ops
1 parent 77315bb commit 677d3d3

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package br.com.catalog.controller;
2+
3+
import br.com.catalog.controller.responses.PaginationResponse;
4+
import br.com.catalog.controller.responses.Response;
5+
import br.com.catalog.services.ICrudService;
6+
import br.com.catalog.services.impls.dtos.ProductDTO;
7+
import br.com.catalog.services.impls.dtos.UpdatedProductDTO;
8+
import io.swagger.v3.oas.annotations.Operation;
9+
import io.swagger.v3.oas.annotations.media.Content;
10+
import io.swagger.v3.oas.annotations.media.Schema;
11+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
12+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
13+
import jakarta.validation.Valid;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.web.bind.annotation.*;
17+
18+
@RestController
19+
@RequestMapping("/api/v1/product")
20+
public class ProductController {
21+
private final ICrudService<ProductDTO, UpdatedProductDTO> productService;
22+
23+
public ProductController(ICrudService<ProductDTO, UpdatedProductDTO> productService) {
24+
this.productService = productService;
25+
}
26+
27+
@Operation(summary = "Retorna todos os produtos em paginação")
28+
@ApiResponses(value = {
29+
@ApiResponse(responseCode = "200", description = "Retorna a paginação de produtos",
30+
content = @Content(mediaType = "application/json",
31+
schema = @Schema(implementation = PaginationResponse.class))),
32+
@ApiResponse(responseCode = "400", description = "Erro de validação dos parâmetros")
33+
})
34+
@GetMapping("/getAll")
35+
public ResponseEntity<Response<PaginationResponse<ProductDTO>>> getAllProducts(
36+
@RequestParam(defaultValue = "0") int page,
37+
@RequestParam(defaultValue = "10")int size) {
38+
return ResponseEntity.ok(new Response<>(
39+
HttpStatus.OK.toString(),
40+
"Products found successfully",
41+
productService.findAll(page, size)));
42+
}
43+
44+
@Operation(summary = "Cria um novo produto")
45+
@ApiResponses(value = {
46+
@ApiResponse(responseCode = "201", description = "Produto criado com sucesso",
47+
content = @Content(mediaType = "application/json",
48+
schema = @Schema(implementation = ProductDTO.class))),
49+
@ApiResponse(responseCode = "400", description = "Erro de validação dos parâmetros")
50+
})
51+
@PostMapping("/create")
52+
public ResponseEntity<Response<ProductDTO>> createProduct(@RequestBody @Valid ProductDTO dto) {
53+
return ResponseEntity.ok(new Response<>(
54+
HttpStatus.CREATED.toString(),
55+
"Product created successfully",
56+
productService.create(dto)));
57+
}
58+
59+
@Operation(summary = "Retorna um produto pelo ID")
60+
@ApiResponses(value = {
61+
@ApiResponse(responseCode = "200", description = "Produto encontrado com sucesso",
62+
content = @Content(mediaType = "application/json",
63+
schema = @Schema(implementation = ProductDTO.class))),
64+
@ApiResponse(responseCode = "404", description = "Produto não encontrado")
65+
})
66+
@GetMapping("/getById/{id}")
67+
public ResponseEntity<Response<ProductDTO>> getProductById(@PathVariable Long id) throws Exception {
68+
return ResponseEntity.ok(new Response<>(
69+
HttpStatus.OK.toString(),
70+
"Product found successfully",
71+
productService.findById(id)));
72+
}
73+
74+
@Operation(summary = "Retorna um produto pelo nome")
75+
@ApiResponses(value = {
76+
@ApiResponse(responseCode = "200", description = "Produto encontrado com sucesso",
77+
content = @Content(mediaType = "application/json",
78+
schema = @Schema(implementation = ProductDTO.class))),
79+
@ApiResponse(responseCode = "404", description = "Produto não encontrado")
80+
})
81+
@GetMapping("/getByName/{name}")
82+
public ResponseEntity<Response<ProductDTO>> getProductByName(@PathVariable String name) throws Exception {
83+
return ResponseEntity.ok(new Response<>(
84+
HttpStatus.OK.toString(),
85+
"Product found successfully",
86+
productService.findByName(name)));
87+
}
88+
89+
@Operation(summary = "Atualiza um produto")
90+
@ApiResponses(value = {
91+
@ApiResponse(responseCode = "200", description = "Produto atualizado com sucesso",
92+
content = @Content(mediaType = "application/json",
93+
schema = @Schema(implementation = ProductDTO.class))),
94+
@ApiResponse(responseCode = "400", description = "Erro de validação dos parâmetros"),
95+
@ApiResponse(responseCode = "404", description = "Produto não encontrado")
96+
})
97+
@PutMapping("/update/{id}")
98+
public ResponseEntity<Response<ProductDTO>> updateProduct(@PathVariable Long id,
99+
@RequestBody @Valid UpdatedProductDTO dto) throws Exception {
100+
return ResponseEntity.ok(new Response<>(
101+
HttpStatus.OK.toString(),
102+
"Product updated successfully",
103+
productService.update(id, dto)));
104+
}
105+
106+
@Operation(summary = "Deleta logicamente um produto")
107+
@ApiResponses(value = {
108+
@ApiResponse(responseCode = "204", description = "Produto deletado com sucesso"),
109+
@ApiResponse(responseCode = "404", description = "Produto não encontrado")
110+
})
111+
@DeleteMapping("/delete/{id}")
112+
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) throws Exception {
113+
productService.delete(id);
114+
return ResponseEntity.noContent().build();
115+
}
116+
}

0 commit comments

Comments
 (0)