Databases Reference
In-Depth Information
Second, we have to implement a method that is used to return the information of a
single contact. We can implement the findById() method by following these steps:
1.
Check that the contact exists. If contact does not exist, throw
NotFoundException .
2.
Get the contact from the hash.
3.
Return the found contact.
The source code of our method is given as follows:
@Override
public Contact findById(Long id) throws NotFoundException {
if (contactDoesNotExist(id)) {
throw new NotFoundException("No contact found with id: " + id);
}
return buildContact(id);
}
Update
We can update the information of an existing contact by following these steps:
1.
Check if that contact exists. If no contact is found, throw a
NotFoundException .
2.
Save the updated contact information in the hash.
3.
Return the updated contact.
The source code of the update() method is given as follows:
@Override
public Contact update(Contact updated) throws NotFoundException {
if (contactDoesNotExist(updated.getId())) {
throw new NotFoundException("No contact found with id: " +
updated.getId());
}
persist(updated);
return updated;
}
 
Search WWH ::




Custom Search