-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: with repositories and use case
- Loading branch information
1 parent
b5333b0
commit a06a39c
Showing
9 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
service/src/main/java/io/github/gabbloquet/features/rent/application/RentRequestUseCase.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.gabbloquet.features.rent.application; | ||
|
||
import io.github.gabbloquet.rent.domain.CarRepository; | ||
import io.github.gabbloquet.rent.domain.CustomerRepository; | ||
import io.github.gabbloquet.rent.domain.model.Car; | ||
import io.github.gabbloquet.rent.domain.model.Customer; | ||
import io.github.gabbloquet.rent.domain.model.RentRequest; | ||
import io.github.gabbloquet.rent.domain.model.RentalRequest; | ||
import io.github.gabbloquet.rent.infra.InMemoryCarRepository; | ||
import io.github.gabbloquet.rent.infra.InMemoryCustomerRepository; | ||
import lombok.AllArgsConstructor; | ||
|
||
public class RentRequestUseCase { | ||
|
||
private CarRepository carRepository; | ||
private CustomerRepository customerRepository; | ||
|
||
public RentRequestUseCase(CarRepository carRepository, CustomerRepository customerRepository) { | ||
this.carRepository = carRepository; | ||
this.customerRepository = customerRepository; | ||
} | ||
|
||
public RentalRequest execute(RentRequest command) { | ||
Car car = carRepository.get(command.carName()); | ||
Customer customer = customerRepository.get(command.name()); | ||
return customer.rent(car); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
service/src/main/java/io/github/gabbloquet/rent/domain/CarRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.gabbloquet.rent.domain; | ||
|
||
import io.github.gabbloquet.rent.domain.model.Car; | ||
|
||
public interface CarRepository { | ||
void add(Car car); | ||
|
||
Car get(String carName); | ||
} |
9 changes: 9 additions & 0 deletions
9
service/src/main/java/io/github/gabbloquet/rent/domain/CustomerRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.gabbloquet.rent.domain; | ||
|
||
import io.github.gabbloquet.rent.domain.model.Customer; | ||
|
||
public interface CustomerRepository { | ||
void add(Customer customer); | ||
|
||
Customer get(String name); | ||
} |
2 changes: 1 addition & 1 deletion
2
service/src/main/java/io/github/gabbloquet/rent/domain/model/Customer.java
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
4 changes: 4 additions & 0 deletions
4
service/src/main/java/io/github/gabbloquet/rent/domain/model/RentRequest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.github.gabbloquet.rent.domain.model; | ||
|
||
public record RentRequest(String name, String carName) { | ||
} |
22 changes: 22 additions & 0 deletions
22
service/src/main/java/io/github/gabbloquet/rent/infra/InMemoryCarRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.gabbloquet.rent.infra; | ||
|
||
import io.github.gabbloquet.rent.domain.CarRepository; | ||
import io.github.gabbloquet.rent.domain.model.Car; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InMemoryCarRepository implements CarRepository { | ||
|
||
private final List<Car> cars = new ArrayList<>(); | ||
|
||
@Override | ||
public void add(Car car) { | ||
cars.add(car); | ||
} | ||
|
||
@Override | ||
public Car get(String carName) { | ||
return cars.stream().filter(car -> car.name().equals(carName)).findFirst().get(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
service/src/main/java/io/github/gabbloquet/rent/infra/InMemoryCustomerRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.github.gabbloquet.rent.infra; | ||
|
||
import io.github.gabbloquet.rent.domain.CustomerRepository; | ||
import io.github.gabbloquet.rent.domain.model.Customer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InMemoryCustomerRepository implements CustomerRepository { | ||
|
||
private final List<Customer> customers = new ArrayList<>(); | ||
|
||
@Override | ||
public void add(Customer customer) { | ||
customers.add(customer); | ||
} | ||
|
||
@Override | ||
public Customer get(String name) { | ||
return customers.stream() | ||
.filter(customer -> customer.name().equals(name)) | ||
.findFirst() | ||
.get(); | ||
} | ||
} |
25 changes: 18 additions & 7 deletions
25
service/src/test/java/io/github/gabbloquet/features/rent/domain/specs/ValidateRentSpec.java
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
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