How can I combine 2 GET methods?

1

I have 2 methods get I use springboot and the first one brings all active people boolean active and the second one to the contacts that the person has, but I need you to bring me those contactos according to whether they are active or not , thank you

@GetMapping("/personemergencycontacts")
public List<PersonEmergencyContact> getAllActive() {
    List<PersonEmergencyContact> lista = service.findAllByActive();
    return lista;
}

@GetMapping("/personemergencycontacts/person/{id}")
public List<PersonEmergencyContact> getPersonEmergencyContactByPersonId(@PathVariable(value = "id") Long PersonEmergencyContactId) {
    List<PersonEmergencyContact> lista = service.findByPersonId(PersonEmergencyContactId);
    return lista;
}

Repository

public interface PersonEmergencyContactRepository extends CrudRepository<PersonEmergencyContact, Long> {

@Query("SELECT p FROM PersonEmergencyContact p WHERE p.active = 1")

List<PersonEmergencyContact> findAllByActive();

List<PersonEmergencyContact> findByPersonId(Long id);

}

Service, only the relevant lists in this case

@Override
public List<PersonEmergencyContact> findAll() {
    return super.findAll();
}

public List<PersonEmergencyContact> findAllByActive() { return dao.findAllByActive(); }

public List<PersonEmergencyContact> findByPersonId(Long PersonEmergencyContactId) { return dao.findByPersonId(PersonEmergencyContactId); }

public void softDelete(Long Id) {
    PersonEmergencyContact personemergencycontact = this.findById(Id);
    personemergencycontact.setDeleted_at(toISO8601UTC(new Date()));
    personemergencycontact.setActive(false);
    this.update(personemergencycontact);
}

}

and the soft delete that changes the field active (default 1) to 0

    
asked by Jeypi 07.08.2018 в 16:09
source

1 answer

0

On the get

@GetMapping ("/ personemergencycontacts / person / {id}")

Your method can be left:

public List<PersonEmergencyContact> getPersonEmergencyContactByPersonId(@PathVariable(value = "id") Long PersonEmergencyContactId) {
 List<PersonEmergencyContact> lista= new ArrayList<PersonEmergencyContact>();
 List<PersonEmergencyContact> lista2 = service.findByPersonId(PersonEmergencyContactId);
  for (PersonEmergencyContact otro : lista2 ){
    if(otro.getActive().equalsIgnoreCase("true")){
      lista.add(otro);
     }
  }
  return lista;
}

getActive (), you must add it in your PersonEmergencyContact class as:

public String getActive () {          return Active;      }

Adjust it to your model

    
answered by 07.08.2018 / 16:26
source