Skip to content

Commit

Permalink
feat: adapting get function to receive query param and filter consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroVidalDev committed Jun 12, 2024
1 parent 69ec796 commit c69e82f
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("consumers")
public class ConsumerController implements ControllerIN<ConsumerDataInput, ConsumerDataEditInput>{
public class ConsumerController{

@Autowired
private ConsumerRepository repository;
Expand All @@ -29,7 +30,6 @@ public class ConsumerController implements ControllerIN<ConsumerDataInput, Consu
@Autowired
private MessageService messageService;

@Override
@PostMapping
@Transactional
public ResponseEntity create(@RequestBody @Valid ConsumerDataInput data, @RequestHeader("Language") String language){
Expand All @@ -40,7 +40,6 @@ public ResponseEntity create(@RequestBody @Valid ConsumerDataInput data, @Reques

}

@Override
@PutMapping("/{ra}")
@Transactional
public ResponseEntity update(@PathVariable String ra, @RequestBody @Valid ConsumerDataEditInput data, @RequestHeader("Language") String language){
Expand All @@ -50,7 +49,6 @@ public ResponseEntity update(@PathVariable String ra, @RequestBody @Valid Consum
return ResponseEntity.ok(dto);
}

@Override
@DeleteMapping("/{ra}")
@Transactional
public ResponseEntity delete(@PathVariable String ra){
Expand All @@ -59,10 +57,20 @@ public ResponseEntity delete(@PathVariable String ra){
return ResponseEntity.noContent().build();
}

@Override

@GetMapping
public ResponseEntity<List<ConsumerDataComplete>> get(){
var listConsumers = repository.findAll().stream().map(ConsumerDataComplete::new).toList();
public ResponseEntity<List<ConsumerDataComplete>> get(@RequestParam("permission") Optional<Integer> permission){

List<ConsumerDataComplete> listConsumers;

if(permission.isPresent()){
listConsumers = repository.findAllByPermission_Level(permission.get()).stream().map(ConsumerDataComplete::new).toList();

}

else{
listConsumers = repository.findAll().stream().map(ConsumerDataComplete::new).toList();
}

return ResponseEntity.ok(listConsumers);
}
Expand Down

0 comments on commit c69e82f

Please sign in to comment.