Last active
December 31, 2019 09:28
-
-
Save ehsaniara/14b1c6ffab1dced1eb997df01f48c2b5 to your computer and use it in GitHub Desktop.
CustomerServiceImpl part of https://medium.com/@ehsaniara/spring-boot-2-with-multiple-datasource-for-postgres-data-replication-182c89124f54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ehsaniara.multidatasource.service; | |
import com.ehsaniara.multidatasource.model.Customer; | |
import com.ehsaniara.multidatasource.repository.readRepository.CustomerReadRepository; | |
import com.ehsaniara.multidatasource.repository.writeRepository.CustomerWriteRepository; | |
import org.springframework.stereotype.Service; | |
import org.springframework.util.Assert; | |
import java.util.Optional; | |
/** | |
* @author Jay Ehsaniara, Dec 30 2019 | |
*/ | |
@Service | |
public class CustomerServiceImpl implements CustomerService { | |
private final CustomerReadRepository customerReadRepository; | |
private final CustomerWriteRepository customerWriteRepository; | |
public CustomerServiceImpl(CustomerReadRepository customerReadRepository, CustomerWriteRepository customerWriteRepository) { | |
this.customerReadRepository = customerReadRepository; | |
this.customerWriteRepository = customerWriteRepository; | |
} | |
public Optional<Customer> getCustomer(Long id) { | |
return customerReadRepository.findById(id); | |
} | |
public Customer createCustomer(Customer customer) { | |
Assert.notNull(customer, "Invalid customer"); | |
Assert.isNull(customer.getId(), "customer id should be null"); | |
Assert.notNull(customer.getName(), "Invalid customer name"); | |
return customerWriteRepository.save(customer); | |
} | |
public Customer updateCustomer(Customer customer) { | |
Assert.notNull(customer, "Invalid customer"); | |
Assert.notNull(customer.getId(), "Invalid customer id"); | |
return customerWriteRepository.save(customer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment