Skip to content

Commit ddfa522

Browse files
committed
refac: Create interface for CustomerRepo
- Add ICustomerRepo which is an interface that contains all the necessary methods for interacting with the database. The benefit here is that we could easily swap out the implementation if necessary. Another class can implement this interface and use any database technology to do so. - Rename CustomerRepo to CustomerJpaRepo. - Rename CustomerRepoFacade to CustomerRepo.
1 parent 484bd1a commit ddfa522

File tree

5 files changed

+82
-54
lines changed

5 files changed

+82
-54
lines changed

apps/api/src/main/kotlin/com/github/thorlauridsen/service/CustomerService.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.github.thorlauridsen.service
33
import com.github.thorlauridsen.model.Customer
44
import com.github.thorlauridsen.model.CustomerInput
55
import com.github.thorlauridsen.exception.CustomerNotFoundException
6-
import com.github.thorlauridsen.persistence.CustomerRepoFacade
6+
import com.github.thorlauridsen.persistence.ICustomerRepo
77
import org.slf4j.LoggerFactory
88
import org.springframework.stereotype.Service
99
import java.util.UUID
@@ -13,10 +13,10 @@ import java.util.UUID
1313
* - Saving customers.
1414
* - Fetching customers.
1515
*
16-
* @param customerRepo [CustomerRepoFacade] to interact with the database.
16+
* @param customerRepo [ICustomerRepo] to interact with the database.
1717
*/
1818
@Service
19-
class CustomerService(private val customerRepo: CustomerRepoFacade) {
19+
class CustomerService(private val customerRepo: ICustomerRepo) {
2020

2121
private val logger = LoggerFactory.getLogger(this::class.java)
2222

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.thorlauridsen.persistence
2+
3+
import java.util.UUID
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
/**
7+
* Customer repository interface.
8+
* This is a JPA repository for the [CustomerEntity].
9+
* It extends the [JpaRepository] interface which allows us to easily define CRUD methods.
10+
*/
11+
interface CustomerJpaRepo : JpaRepository<CustomerEntity, UUID>
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
package com.github.thorlauridsen.persistence
22

3+
import com.github.thorlauridsen.model.Customer
4+
import com.github.thorlauridsen.model.CustomerInput
35
import java.util.UUID
4-
import org.springframework.data.jpa.repository.JpaRepository
6+
import kotlin.jvm.optionals.getOrNull
7+
import org.springframework.stereotype.Repository
58

69
/**
7-
* Customer repository interface.
8-
* This is a JPA repository for the customer entity.
9-
* It extends the [JpaRepository] interface which allows us to easily define CRUD methods.
10+
* Customer repository facade class.
11+
*
12+
* This class is a facade for the [CustomerJpaRepo].
13+
* A service class can use this facade to easily interact with the
14+
* repository without needing to know about the database entity [CustomerEntity].
15+
*
16+
* It is annotated with [Repository] to allow Spring to automatically
17+
* detect it as a bean and inject it where needed.
18+
*
19+
* @param customerRepo [CustomerJpaRepo] to interact with the database.
1020
*/
11-
interface CustomerRepo : JpaRepository<CustomerEntity, UUID>
21+
@Repository
22+
class CustomerRepo(private val customerRepo: CustomerJpaRepo) : ICustomerRepo {
23+
24+
/**
25+
* Save a customer.
26+
* @param customer [CustomerInput] to save.
27+
* @return [Customer] retrieved from database.
28+
*/
29+
override fun save(customer: CustomerInput): Customer {
30+
val entity = CustomerEntity(
31+
mail = customer.mail,
32+
)
33+
val created = customerRepo.save(entity)
34+
return created.toModel()
35+
}
36+
37+
/**
38+
* Get a customer given an id.
39+
* @param id [UUID] to fetch customer.
40+
* @return [Customer] or null if not found.
41+
*/
42+
override fun find(id: UUID): Customer? {
43+
val entity = customerRepo.findById(id).getOrNull()
44+
return entity?.toModel()
45+
}
46+
}

modules/persistence/src/main/kotlin/com/github/thorlauridsen/persistence/CustomerRepoFacade.kt

-46
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.thorlauridsen.persistence
2+
3+
import com.github.thorlauridsen.model.Customer
4+
import com.github.thorlauridsen.model.CustomerInput
5+
import java.util.UUID
6+
7+
/**
8+
* Customer repository interface.
9+
* This is an interface containing methods for interacting with the customer table.
10+
* A repository class will implement this interface to provide the actual implementation.
11+
* This interface makes it easier to swap out the implementation of the repository if needed.
12+
*/
13+
interface ICustomerRepo {
14+
15+
/**
16+
* Save a customer to the database.
17+
* @param customer [CustomerInput] to save.
18+
* @return [Customer] retrieved from database.
19+
*/
20+
fun save(customer: CustomerInput): Customer
21+
22+
/**
23+
* Get a customer given an id.
24+
* @param id [UUID] to fetch customer.
25+
* @return [Customer] or null if not found.
26+
*/
27+
fun find(id: UUID): Customer?
28+
}

0 commit comments

Comments
 (0)