Advertisement
Guest User

Dto

a guest
Nov 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. public Account save(Account account) throws Exception {
  2.         if (StringUtils.isEmpty(account.getServiceName())) {
  3.             throw new Exception("Service name is required");
  4.         }
  5.         if (StringUtils.isEmpty(account.getEmail())) {
  6.             throw new Exception("Email is required");
  7.         }
  8.  
  9.         if (account.getId() != null && existsById(account.getId())) {
  10.             throw new Exception("Account with id: " + account.getId() + " already exists");
  11.         }
  12.  
  13.         return accountRepository.save(account);
  14.     }
  15.  
  16. /with DTO
  17. public AccountDto save(Account account) throws Exception {
  18.         if (StringUtils.isEmpty(account.getServiceName())) {
  19.             throw new Exception("Service name is required");
  20.         }
  21.         if (StringUtils.isEmpty(account.getEmail())) {
  22.             throw new Exception("Email is required");
  23.         }
  24.  
  25.         if (account.getId() != null && existsById(account.getId())) {
  26.             throw new Exception("Account with id: " + account.getId() + " already exists");
  27.         }
  28.  
  29.         AccountDto accountDto = new AccountDto();
  30.         accountDto.setServiceName(account.getServiceName());
  31.         accountDto.setEmail(account.getEmail());
  32.         accountDto.setPassword(account.getPassword());
  33.         accountDto.setCreatedOn(account.getCreatedOn());
  34.         account.setUpdatedOn(account.getUpdatedOn());
  35.  
  36.         accountRepository.save(account);
  37.         return accountDto;    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement