Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ bootBuildImage {

spotless {
java {
googleJavaFormat('1.22.0')
googleJavaFormat('1.22.0').aosp()
removeUnusedImports()
trimTrailingWhitespace()
leadingTabsToSpaces()
endWithNewline()
importOrder '', 'java', 'javax', 'org', 'com'
target 'src/**/*.java'
}

format 'misc', {
target '*.gradle', '*.md', '.gitignore'
indentWithSpaces()
leadingTabsToSpaces()
trimTrailingWhitespace()
endWithNewline()
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/autoinvestor/CoreApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class CoreApplication {

public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.autoinvestor.exceptions.DuplicatedException;

public class AssetAlreadyExists extends DuplicatedException {
public AssetAlreadyExists(String message) {
super(message);
}
public AssetAlreadyExists(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.autoinvestor.exceptions.BadRequestException;

public class AssetNotFoundException extends BadRequestException {
public AssetNotFoundException(String message) {
super(message);
}
public AssetNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
@Service
public class GetAllAssetsCommandHandler {

private final AssetRepository repository;
private final AssetRepository repository;

public GetAllAssetsCommandHandler(AssetRepository repository) {
this.repository = repository;
}
public GetAllAssetsCommandHandler(AssetRepository repository) {
this.repository = repository;
}

public List<GetAssetResponse> handle() {
List<Asset> assets = this.repository.findAll();
return assets.stream()
.map(asset -> new GetAssetResponse(asset.id(), asset.mic(), asset.ticker(), asset.name()))
.collect(Collectors.toList());
}
public List<GetAssetResponse> handle() {
List<Asset> assets = this.repository.findAll();
return assets.stream()
.map(
asset ->
new GetAssetResponse(
asset.id(), asset.mic(), asset.ticker(), asset.name()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
@Service
public class GetAssetCommandHandler {

private final AssetRepository repository;
private final AssetRepository repository;

public GetAssetCommandHandler(AssetRepository repository) {
this.repository = repository;
}
public GetAssetCommandHandler(AssetRepository repository) {
this.repository = repository;
}

public GetAssetResponse handle(GetAssetCommand command) {
Optional<Asset> asset = repository.findById(AssetId.of(command.assetId()));
public GetAssetResponse handle(GetAssetCommand command) {
Optional<Asset> asset = repository.findById(AssetId.of(command.assetId()));

if (asset.isEmpty()) {
throw new AssetNotFoundException("Asset not found with ID: " + command.assetId());
}
if (asset.isEmpty()) {
throw new AssetNotFoundException("Asset not found with ID: " + command.assetId());
}

return new GetAssetResponse(
asset.get().id(), asset.get().mic(), asset.get().ticker(), asset.get().name());
}
return new GetAssetResponse(
asset.get().id(), asset.get().mic(), asset.get().ticker(), asset.get().name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
@Service
public class GetAssetPriceCommandHandler {

private final AssetPriceFetcher fetcher;
private final AssetRepository repository;
private final AssetPriceFetcher fetcher;
private final AssetRepository repository;

public GetAssetPriceCommandHandler(AssetRepository repository, AssetPriceFetcher fetcher) {
this.repository = repository;
this.fetcher = fetcher;
}
public GetAssetPriceCommandHandler(AssetRepository repository, AssetPriceFetcher fetcher) {
this.repository = repository;
this.fetcher = fetcher;
}

public GetAssetPriceResponse handle(GetAssetPriceCommand command) {
Optional<Asset> asset = repository.findById(AssetId.of(command.assetId()));
public GetAssetPriceResponse handle(GetAssetPriceCommand command) {
Optional<Asset> asset = repository.findById(AssetId.of(command.assetId()));

if (asset.isEmpty()) {
throw new AssetNotFoundException("Asset not found with ID: " + command.assetId());
}
if (asset.isEmpty()) {
throw new AssetNotFoundException("Asset not found with ID: " + command.assetId());
}

float price = fetcher.priceOn(asset.get(), command.date());
return new GetAssetPriceResponse(price, command.date());
}
float price = fetcher.priceOn(asset.get(), command.date());
return new GetAssetPriceResponse(price, command.date());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@
@Service
public class RegisterAssetCommandHandler {

private final AssetRepository repository;
private final EventPublisher eventPublisher;

public RegisterAssetCommandHandler(AssetRepository repository, EventPublisher eventPublisher) {
this.repository = repository;
this.eventPublisher = eventPublisher;
}

public RegisterAssetResponse handle(RegisterAssetCommand command) {
if (this.repository.exists(command.mic(), command.ticker())) {
throw new AssetAlreadyExists(
"Duplicated asset for this mic: " + command.mic() + " and ticker: " + command.ticker());
private final AssetRepository repository;
private final EventPublisher eventPublisher;

public RegisterAssetCommandHandler(AssetRepository repository, EventPublisher eventPublisher) {
this.repository = repository;
this.eventPublisher = eventPublisher;
}

Asset asset = Asset.create(command.mic(), command.ticker(), command.name());
public RegisterAssetResponse handle(RegisterAssetCommand command) {
if (this.repository.exists(command.mic(), command.ticker())) {
throw new AssetAlreadyExists(
"Duplicated asset for this mic: "
+ command.mic()
+ " and ticker: "
+ command.ticker());
}

Asset asset = Asset.create(command.mic(), command.ticker(), command.name());

this.repository.save(asset);
this.eventPublisher.publish(asset.releaseEvents());
this.repository.save(asset);
this.eventPublisher.publish(asset.releaseEvents());

return new RegisterAssetResponse(asset.id(), asset.mic(), asset.ticker(), asset.name());
}
return new RegisterAssetResponse(asset.id(), asset.mic(), asset.ticker(), asset.name());
}
}
24 changes: 12 additions & 12 deletions src/main/java/io/autoinvestor/domain/AggregateRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
import java.util.List;

public class AggregateRoot {
private final List<Event<?>> events;
private final List<Event<?>> events;

public AggregateRoot() {
this.events = new ArrayList<>();
}
public AggregateRoot() {
this.events = new ArrayList<>();
}

protected void recordEvent(Event<? extends EventPayload> event) {
this.events.add(event);
}
protected void recordEvent(Event<? extends EventPayload> event) {
this.events.add(event);
}

public List<Event<?>> releaseEvents() {
List<Event<?>> events = new ArrayList<>(this.events);
this.events.clear();
return events;
}
public List<Event<?>> releaseEvents() {
List<Event<?>> events = new ArrayList<>(this.events);
this.events.clear();
return events;
}
}
Loading