Update Operation – REST API

We can build one more REST API that will allow my client application to update the account details inside my database.

Requirement

Clients can send the below data as a request to the update API.

They can pass the same data to the update API. While passing this data, they can change any of this data except account number, because based upon my business logic, once the account number is generated, I don’t want to give flexibility to my end user to update the account number.

You can update the name, email, mobileNumber, he can update his account type, he can update branch address, anything he can update, but not the account number.

Add abstract method in Service Interface

/**
*
* @param customerDto - CustomerDto Object
* @return boolean indicating if the update of Account details is successful or not
*/
boolean updateAccount(CustomerDto customerDto);

Implementing the method – Service implementation

Overriding the method – this is how it looks at the beginning.

/**
* @param customerDto - CustomerDto Object
* @return boolean - if the update of Account details is successful or not
*/
@Override
public boolean updateAccount(CustomerDto customerDto) {

return false;
}

Adding the update logic –

/**
* @param customerDto - CustomerDto Object
* @return boolean - if the update of Account details is successful or not
*/
@Override
public boolean updateAccount(CustomerDto customerDto) {
boolean isUpdated = false;
AccountsDto accountsDto = customerDto.getAccountsDto();
if(accountsDto !=null ){
Accounts accounts =
accountsRepository.findById(accountsDto.getAccountNumber())
.orElseThrow(
() -> new ResourceNotFoundException("Account",
"AccountNumber", accountsDto.getAccountNumber().toString())
);
AccountsMapper.mapToAccounts(accountsDto, accounts);
accounts = accountsRepository.save(accounts);

Long customerId = accounts.getCustomerId();
Customer customer =
customerRepository.findById(customerId)
.orElseThrow(
() -> new ResourceNotFoundException("Customer", "CustomerID",
customerId.toString())
);
CustomerMapper.mapToCustomer(customerDto,customer);
customerRepository.save(customer);
isUpdated = true;
}
return isUpdated;
}

I’m trying to first get the AccountsDto from the CustomerDto which I have received.

Using the AccountsDto, I’m trying to fetch the account number that I received from the client application. Using the account number, I’m trying to fetch the accounts record.

Since the accountNumber is the primary key inside my Accounts entity, I can use the findById() method available inside the spring data JPA framework.

So whenever we are using findById, my spring data JPA framework will go for the Accounts entity and it will look at what is the primary key column. This is the primary key column because we have mentioned @Id, so with this logic it is going to fetch the account details based upon the account number. If there is no record, we are throwing the ResourceNotFoundException.

save() method

save() is a very smart method inside the spring data JPA. If we don’t find any primary key values inside the given entity object, it is going to simply do the insert operation.

If we find the record inside the database, it is going to perform the update operation based upon the primary key value that we shared inside the entity POJO class.

Adding the REST API in Controller layer

Code – 

@PutMapping("/update")
public ResponseEntity<ResponseDto> updateAccountDetails(
@RequestBody CustomerDto customerDto)
{
boolean isUpdated = service.updateAccount(customerDto);
if(isUpdated) {
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResponseDto(
AccountsConstants.STATUS_200,
AccountsConstants.MESSAGE_200)
);
}else{
return ResponseEntity
.status(HttpStatus.INERTERNAL_SERVER_ERROR)
.body(new ResponseDto(
AccountsConstants.STATUS_500,
AccountsConstants.MESSAGE_500)
);
}
}

Build and deploy the code.

Build and Deploy

The application is started at port number 8080.

Using Postman, you can invoke the REST APIs.

If we send the wrong account number, it will throw this message.

Fetching the details 

Output :