|
| 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.BrandDTO; |
| 7 | +import io.swagger.v3.oas.annotations.Operation; |
| 8 | +import io.swagger.v3.oas.annotations.media.Content; |
| 9 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 10 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 11 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 12 | +import jakarta.validation.Valid; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.http.ResponseEntity; |
| 15 | +import org.springframework.web.bind.annotation.*; |
| 16 | + |
| 17 | +@RestController |
| 18 | +@RequestMapping("/api/v1/brand") |
| 19 | +public class BrandController { |
| 20 | + private final ICrudService<BrandDTO, BrandDTO> brandService; |
| 21 | + |
| 22 | + public BrandController(ICrudService<BrandDTO, BrandDTO> service) { |
| 23 | + this.brandService = service; |
| 24 | + } |
| 25 | + |
| 26 | + @Operation(summary = "Retorna todas as marcas em paginação") |
| 27 | + @ApiResponses(value = { |
| 28 | + @ApiResponse(responseCode = "200", description = "Retorna a paginação de marcas", |
| 29 | + content = @Content(mediaType = "application/json", |
| 30 | + schema = @Schema(implementation = PaginationResponse.class))), |
| 31 | + @ApiResponse(responseCode = "400", description = "Erro de validação dos parâmetros") |
| 32 | + }) |
| 33 | + @GetMapping("/getAll") |
| 34 | + public ResponseEntity<Response<PaginationResponse<BrandDTO>>> findAll( |
| 35 | + @RequestParam(defaultValue = "0") int page, |
| 36 | + @RequestParam(defaultValue = "10") int size) { |
| 37 | + return ResponseEntity.ok(new Response<>( |
| 38 | + HttpStatus.OK.toString(), |
| 39 | + "Brands found successfully", |
| 40 | + brandService.findAll(page, size))); |
| 41 | + } |
| 42 | + |
| 43 | + @Operation(summary = "Busca uma marca pelo ID") |
| 44 | + @ApiResponses(value = { |
| 45 | + @ApiResponse(responseCode = "200", description = "Marca encontrada com sucesso", |
| 46 | + content = @Content(mediaType = "application/json", |
| 47 | + schema = @Schema(implementation = BrandDTO.class))), |
| 48 | + @ApiResponse(responseCode = "404", description = "Marca não encontrada") |
| 49 | + }) |
| 50 | + @GetMapping("/getById/{id}") |
| 51 | + public ResponseEntity<Response<BrandDTO>> findBrandById(@PathVariable Long id) throws Exception { |
| 52 | + return ResponseEntity.ok(new Response<>( |
| 53 | + HttpStatus.OK.toString(), |
| 54 | + "Brand found successfully", |
| 55 | + brandService.findById(id))); |
| 56 | + } |
| 57 | + |
| 58 | + @Operation(summary = "Busca uma marca pelo nome") |
| 59 | + @ApiResponses(value = { |
| 60 | + @ApiResponse(responseCode = "200", description = "Marca encontrada com sucesso", |
| 61 | + content = @Content(mediaType = "application/json", |
| 62 | + schema = @Schema(implementation = BrandDTO.class))), |
| 63 | + @ApiResponse(responseCode = "404", description = "Marca não encontrada") |
| 64 | + }) |
| 65 | + @GetMapping("/getByName/{name}") |
| 66 | + public ResponseEntity<Response<BrandDTO>> findBrandByName(@PathVariable String name) throws Exception { |
| 67 | + return ResponseEntity.ok(new Response<>( |
| 68 | + HttpStatus.OK.toString(), |
| 69 | + "Brand found successfully", |
| 70 | + brandService.findByName(name))); |
| 71 | + } |
| 72 | + |
| 73 | + @Operation(summary = "Cria uma nova marca") |
| 74 | + @ApiResponses(value = { |
| 75 | + @ApiResponse(responseCode = "201", description = "Marca criada com sucesso", |
| 76 | + content = @Content(mediaType = "application/json", |
| 77 | + schema = @Schema(implementation = BrandDTO.class))), |
| 78 | + @ApiResponse(responseCode = "400", description = "Dados inválidos") |
| 79 | + }) |
| 80 | + @PostMapping("/create") |
| 81 | + public ResponseEntity<Response<BrandDTO>> createbrand(@RequestBody @Valid BrandDTO dto) { |
| 82 | + return ResponseEntity.ok(new Response<>( |
| 83 | + HttpStatus.CREATED.toString(), |
| 84 | + "Brand created successfully", |
| 85 | + brandService.create(dto))); |
| 86 | + } |
| 87 | + |
| 88 | + @Operation(summary = "Atualiza uma marca existente") |
| 89 | + @ApiResponses(value = { |
| 90 | + @ApiResponse(responseCode = "200", description = "Marca atualizada com sucesso", |
| 91 | + content = @Content(mediaType = "application/json", |
| 92 | + schema = @Schema(implementation = BrandDTO.class))), |
| 93 | + @ApiResponse(responseCode = "404", description = "Marca não encontrada"), |
| 94 | + @ApiResponse(responseCode = "400", description = "Dados inválidos") |
| 95 | + }) |
| 96 | + @PutMapping("/update/{id}") |
| 97 | + public ResponseEntity<Response<BrandDTO>> updateBrand(@PathVariable Long id, |
| 98 | + @RequestBody @Valid BrandDTO dto) throws Exception { |
| 99 | + return ResponseEntity.ok(new Response<>( |
| 100 | + HttpStatus.OK.toString(), |
| 101 | + "Brand updated successfully", |
| 102 | + brandService.update(id, dto))); |
| 103 | + } |
| 104 | + |
| 105 | + @Operation(summary = "Deleta uma marca logicamente") |
| 106 | + @ApiResponses(value = { |
| 107 | + @ApiResponse(responseCode = "204", description = "Marca deletada com sucesso"), |
| 108 | + @ApiResponse(responseCode = "404", description = "Marca não encontrada") |
| 109 | + }) |
| 110 | + @DeleteMapping("/delete/{id}") |
| 111 | + public ResponseEntity<Void> deleteBrand(@PathVariable Long id) throws Exception { |
| 112 | + brandService.delete(id); |
| 113 | + return ResponseEntity.noContent().build(); |
| 114 | + } |
| 115 | +} |
0 commit comments