Skip to content

Instantly share code, notes, and snippets.

@must1
Last active January 3, 2019 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save must1/1929fedff64af9857362ea963818351a to your computer and use it in GitHub Desktop.
Save must1/1929fedff64af9857362ea963818351a to your computer and use it in GitHub Desktop.
package bookrental.account;
import bookrental.bookrentals.BookRentals;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface AccountRepository {
void deleteById(Integer id);
List<Account> findAll();
boolean doesAccountExistsWithGivenID(@Param("accountID") int accountID);
Account save(Account account);
Optional<Account> findById(int id);
}
package bookrental.account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class AccountService {
private final AccountRepository accountRepository;
@Autowired
public AccountService(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
public Account createAccount(Account account) {
return accountRepository.save(account);
}
public Account deleteAccount(int id) {
Account bookToDelete = accountRepository.findById(id).orElse(null);
accountRepository.deleteById(id);
return bookToDelete;
}
public List<Account> getAllAccounts() {
return new ArrayList<>(accountRepository.findAll());
}
}
package bookrental.account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public class PostgreSQLAccountRepository implements AccountRepository {
private final CrudRepository<Account, Integer> repository;
@Autowired
public PostgreSQLAccountRepository(CrudRepository<Account, Integer> repository) {
this.repository = repository;
}
@Override
public void deleteById(Integer id) {
this.repository.deleteById(id);
}
@Override
public List<Account> findAll() {
return (List<Account>) repository.findAll();
}
@Override
public boolean doesAccountExistsWithGivenID(int accountID) {
return repository.existsById(accountID);
}
@Override
public Account save(Account account) {
return repository.save(account);
}
@Override
public Optional<Account> findById(int id) {
return repository.findById(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment