Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
refactor: pmd rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasmzsouza committed May 4, 2024
1 parent 16eb746 commit 341d324
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@RequestMapping(value = "/products")
public class ProductResource {

private static final String PATH_VARIABLE_ID = "/{id}";

@Autowired private ProductService service;

@GetMapping
Expand All @@ -22,30 +24,30 @@ public ResponseEntity<List<ProductDTO>> findAll(
return ResponseEntity.ok().body(list);
}

@GetMapping(value = "/{id}")
@GetMapping(value = PATH_VARIABLE_ID)
public ResponseEntity<ProductDTO> findById(@PathVariable Long id) {
ProductDTO dto = service.findById(id);
return ResponseEntity.ok().body(dto);
}

@PostMapping
public ResponseEntity<ProductDTO> insert(@RequestBody ProductDTO dto) {
dto = service.insert(dto);
ProductDTO dtoCreated = service.insert(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.path(PATH_VARIABLE_ID)
.buildAndExpand(dto.getId())
.toUri();
return ResponseEntity.created(uri).body(dto);
return ResponseEntity.created(uri).body(dtoCreated);
}

@PutMapping(value = "/{id}")
@PutMapping(value = PATH_VARIABLE_ID)
public ResponseEntity<ProductDTO> update(@PathVariable Long id, @RequestBody ProductDTO dto) {
dto = service.update(id, dto);
return ResponseEntity.ok().body(dto);
ProductDTO dtoUpdated = service.update(id, dto);
return ResponseEntity.ok().body(dtoUpdated);
}

@DeleteMapping(value = "/{id}")
@DeleteMapping(value = PATH_VARIABLE_ID)
public ResponseEntity<Void> delete(@PathVariable Long id) {
service.delete(id);
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ public ProductDTO update(Long id, ProductDTO dto) {
entity = repository.save(entity);
return new ProductDTO(entity);
} catch (EntityNotFoundException e) {
throw new ResourceNotFoundException("Id not found " + id);
throw new ResourceNotFoundException("Error: " + e.getMessage());
}
}

public void delete(Long id) {
try {
repository.deleteById(id);
} catch (EmptyResultDataAccessException e) {
throw new ResourceNotFoundException("Id not found " + id);
throw new ResourceNotFoundException("Error: " + e.getMessage());
} catch (DataIntegrityViolationException e) {
throw new DatabaseException("Integrity violation");
throw new DatabaseException("Error: " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package br.com.fiap.grupo30.fastfood.services.exceptions;

import java.io.Serial;

public class DatabaseException extends RuntimeException {
@Serial private static final long serialVersionUID = 1L;

public DatabaseException(String msg) {
super(msg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package br.com.fiap.grupo30.fastfood.services.exceptions;

import java.io.Serial;

public class ResourceNotFoundException extends RuntimeException {

@Serial private static final long serialVersionUID = 1L;

public ResourceNotFoundException(String msg) {
super(msg);
}
Expand Down

0 comments on commit 341d324

Please sign in to comment.