Skip to content

Commit

Permalink
Improve hexagonal architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
hdescottes committed Jan 14, 2024
1 parent b7a68ac commit ab69ae4
Show file tree
Hide file tree
Showing 24 changed files with 667 additions and 190 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.project.reactdashboard.app.stock;

import com.project.reactdashboard.domain.stock.model.Stock;
import com.project.reactdashboard.domain.stock.StockMapper;
import com.project.reactdashboard.domain.stock.model.StockDomain;
import com.project.reactdashboard.domain.stock.mapper.StockDomainMapper;
import com.project.reactdashboard.domain.stock.StockService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,47 +19,44 @@
@RequestMapping("/api/stocks")
public class StockController {

private final StockMapper mapper;
private final StockDomainMapper mapper;

private final StockService service;

public StockController(StockMapper mapper, StockService service) {
public StockController(StockDomainMapper mapper, StockService service) {
this.mapper = mapper;
this.service = service;
}

@GetMapping("/latest")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<StockDto>> findAllLatest() {
List<Stock> stocks = service.findAllLatest();
List<StockDto> dtos = mapper.toListDto(stocks);
List<StockDomain> stockDomains = service.findAllLatest();
List<StockDto> dtos = mapper.toListDto(stockDomains);
return ResponseEntity.ok(dtos);
}

@GetMapping("/{symbol}")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<StockDto>> findBySymbol(@PathVariable("symbol") String symbol) {
List<Stock> stocks = service.findBySymbol(symbol);
List<StockDto> dtos = mapper.toListDto(stocks);
List<StockDomain> stockDomains = service.findBySymbol(symbol);
List<StockDto> dtos = mapper.toListDto(stockDomains);
return ResponseEntity.ok(dtos);
}

@GetMapping("/{symbol}/last-working-day")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<StockDto> findLastWorkingDayBySymbol(@PathVariable("symbol") String symbol) {
Stock stock = service.findLastWorkingDayBySymbol(symbol);
StockDto dto = new StockDto.StockDtoBuilder().build();
if (stock != null) {
dto = mapper.toDto(stock);
}
StockDomain stockDomain = service.findLastWorkingDayBySymbol(symbol);
StockDto dto = mapper.toDto(stockDomain);
return ResponseEntity.ok(dto);
}

@PostMapping("/all")
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Integer> createAll(@RequestBody final List<StockDto> stockDtos) {
List<Stock> stocks = mapper.toList(stockDtos);
service.createAll(stocks);
List<StockDomain> stockDomains = mapper.toListDomain(stockDtos);
service.createAll(stockDomains);
return ResponseEntity.ok(1);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.project.reactdashboard.domain.stock;

import com.project.reactdashboard.domain.stock.model.Stock;
import com.project.reactdashboard.domain.stock.model.StockDomain;

import java.util.List;

public interface StockApi {

void createAll(List<Stock> stocks);
void createAll(List<StockDomain> stockDomains);

List<Stock> findBySymbol(String symbol);
List<StockDomain> findBySymbol(String symbol);

List<Stock> findAllLatest();
List<StockDomain> findAllLatest();

Stock findLastWorkingDayBySymbol(String symbol);
StockDomain findLastWorkingDayBySymbol(String symbol);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.project.reactdashboard.domain.stock;

import com.project.reactdashboard.domain.stock.model.Stock;
import com.project.reactdashboard.domain.stock.model.StockDomain;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
Expand All @@ -18,20 +18,20 @@ public StockService(StockSpi stockSpi) {
this.stockSpi = stockSpi;
}

public void createAll(List<Stock> stocks) {
stockSpi.createAll(stocks);
public void createAll(List<StockDomain> stockDomains) {
stockSpi.createAll(stockDomains);
}

public List<Stock> findBySymbol(String symbol) {
public List<StockDomain> findBySymbol(String symbol) {
OffsetDateTime date = OffsetDateTime.now().minusMonths(1);
return stockSpi.findBySymbol(symbol, date);
}

public List<Stock> findAllLatest() {
public List<StockDomain> findAllLatest() {
return stockSpi.findAllLatest();
}

public Stock findLastWorkingDayBySymbol(String symbol) {
public StockDomain findLastWorkingDayBySymbol(String symbol) {
OffsetDateTime lastWorkingDay = lastWorkingDay()
.withHour(0)
.withMinute(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.project.reactdashboard.domain.stock;

import com.project.reactdashboard.domain.stock.model.Stock;
import com.project.reactdashboard.domain.stock.model.StockDomain;

import java.time.OffsetDateTime;
import java.util.List;

public interface StockSpi {

void createAll(List<Stock> stocks);
void createAll(List<StockDomain> stockDomains);

List<Stock> findBySymbol(String symbol, OffsetDateTime date);
List<StockDomain> findBySymbol(String symbol, OffsetDateTime date);

List<Stock> findAllLatest();
List<StockDomain> findAllLatest();

Stock findLastWorkingDayBySymbol(String symbol, OffsetDateTime lastWorkingDay);
StockDomain findLastWorkingDayBySymbol(String symbol, OffsetDateTime lastWorkingDay);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.project.reactdashboard.domain.stock;
package com.project.reactdashboard.domain.stock.mapper;

import com.project.reactdashboard.domain.stock.model.Stock;
import com.project.reactdashboard.domain.stock.model.StockDomain;
import com.project.reactdashboard.app.stock.StockDto;
import org.springframework.stereotype.Component;

Expand All @@ -9,10 +9,10 @@
import static com.project.reactdashboard.domain.stock.utils.Utils.parseToISO;

@Component
public class StockMapper {
public class StockDomainMapper {

public Stock toEntity(StockDto dto) {
return new Stock.StockBuilder()
public StockDomain toDomain(StockDto dto) {
return new StockDomain.StockDomainBuilder()
.withId(dto.getId())
.withSymbol(dto.getSymbol())
.withDate(parseToISO(dto.getDate()))
Expand All @@ -24,12 +24,12 @@ public Stock toEntity(StockDto dto) {
.build();
}

public StockDto toDto(Stock entity) {
public StockDto toDto(StockDomain entity) {
return new StockDto.StockDtoBuilder()
.withId(entity.getId())
.withSymbol(entity.getSymbol())
.withName(entity.getSymbolValues().getName())
.withDate(entity.getDate().toString())
.withName(entity.getSymbolValues() != null ? entity.getSymbolValues().getName() : null)
.withDate(entity.getDate() != null ? entity.getDate().toString() : null)
.withVolume(entity.getVolume())
.withOpen(entity.getOpen())
.withClose(entity.getClose())
Expand All @@ -38,15 +38,15 @@ public StockDto toDto(Stock entity) {
.build();
}

public List<StockDto> toListDto(List<Stock> stocks) {
return stocks.stream()
public List<StockDto> toListDto(List<StockDomain> stockDomains) {
return stockDomains.stream()
.map(this::toDto)
.toList();
}

public List<Stock> toList(List<StockDto> stockDtos) {
public List<StockDomain> toListDomain(List<StockDto> stockDtos) {
return stockDtos.stream()
.map(this::toEntity)
.map(this::toDomain)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package com.project.reactdashboard.domain.stock.model;

import java.io.Serializable;
import java.time.OffsetDateTime;

public class StockDomain implements Serializable {

private Long id;

private OffsetDateTime date;

private String symbol;

private double open;

private double high;

private double low;

private double close;

private double volume;

private SymbolValuesDomain symbolValuesDomain;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public OffsetDateTime getDate() {
return date;
}

public void setDate(OffsetDateTime date) {
this.date = date;
}

public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public SymbolValuesDomain getSymbolValues() {
return symbolValuesDomain;
}

public void setSymbolValues(SymbolValuesDomain symbolValuesDomain) {
this.symbolValuesDomain = symbolValuesDomain;
}

public double getOpen() {
return open;
}

public void setOpen(double open) {
this.open = open;
}

public double getHigh() {
return high;
}

public void setHigh(double high) {
this.high = high;
}

public double getLow() {
return low;
}

public void setLow(double low) {
this.low = low;
}

public double getClose() {
return close;
}

public void setClose(double close) {
this.close = close;
}

public double getVolume() {
return volume;
}

public void setVolume(double volume) {
this.volume = volume;
}

public StockDomain() {
}

private StockDomain(StockDomainBuilder builder) {
this.id = builder.id;
this.date = builder.date;
this.symbol = builder.symbol;
this.symbolValuesDomain = builder.symbolValuesDomain;
this.open = builder.open;
this.close = builder.close;
this.high = builder.high;
this.low = builder.low;
this.volume = builder.volume;
}

public static class StockDomainBuilder {

private Long id;

private OffsetDateTime date;

private String symbol;

private SymbolValuesDomain symbolValuesDomain;

private double open;

private double high;

private double low;

private double close;

private double volume;

public StockDomainBuilder(){
}

public StockDomainBuilder withId(Long id) {
this.id = id;
return this;
}

public StockDomainBuilder withDate(OffsetDateTime date) {
this.date = date;
return this;
}

public StockDomainBuilder withSymbol(String symbol) {
this.symbol = symbol;
return this;
}

public StockDomainBuilder withSymbolValues(SymbolValuesDomain symbolValuesDomain) {
this.symbolValuesDomain = symbolValuesDomain;
return this;
}

public StockDomainBuilder withOpen(double open) {
this.open = open;
return this;
}

public StockDomainBuilder withLow(double low) {
this.low = low;
return this;
}

public StockDomainBuilder withHigh(double high) {
this.high = high;
return this;
}

public StockDomainBuilder withClose(double close) {
this.close = close;
return this;
}

public StockDomainBuilder withVolume(double volume) {
this.volume = volume;
return this;
}

public StockDomain build(){
return new StockDomain(this);
}

}
}
Loading

0 comments on commit ab69ae4

Please sign in to comment.