Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Utilisation d'annotations JSR-330 #4

Open
wants to merge 9 commits into
base: solution
Choose a base branch
from
Open
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: 0 additions & 5 deletions courtage-application-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,21 @@
<artifactId>cucumber-java8</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -92,7 +88,6 @@
<artifactId>xml-path</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import devoxx.lab.archihexa.courtage.application.springboot.adapters.mixin.AchatJsonDto;
import devoxx.lab.archihexa.courtage.domain.model.Achat;
import devoxx.lab.archihexa.courtage.domain.port.primaire.ServiceCourtage;
import devoxx.lab.archihexa.courtage.domain.port.secondaire.PortefeuilleRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.EventListener;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.util.Optional;

@SpringBootApplication
@ComponentScan(
basePackageClasses = {CourtageSpringbootApplication.class, ServiceCourtage.class}
)
public class CourtageSpringbootApplication {
private static ConfigurableApplicationContext applicationContext = null;
private int port = -1;
Expand All @@ -35,6 +41,9 @@ public static Optional<Integer> getPort() {
}

static void raz() {
Optional.ofNullable(applicationContext)
.map(ctx -> ctx.getBean(PortefeuilleRepository.class))
.ifPresent(PortefeuilleRepository::purge);
}

@EventListener
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package devoxx.lab.archihexa.courtage.application.springboot.adapters.persistence;

import devoxx.lab.archihexa.courtage.domain.model.Portefeuille;
import devoxx.lab.archihexa.courtage.domain.port.secondaire.PortefeuilleRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@Repository
public class PortefeuilleRepositorySpringDataImpl implements PortefeuilleRepository {
private final PortefeuilleSpringDataCrudRepository repo;

public PortefeuilleRepositorySpringDataImpl(PortefeuilleSpringDataCrudRepository repo) {
this.repo = repo;
}

@Override
public boolean existe(String nomPortefeuille) {
return repo.existsById(nomPortefeuille);
}

@Override
public void sauvegarde(Portefeuille portefeuille) {
repo.save(portefeuille);
}

@Override
public Optional<Portefeuille> recupere(String id) {
return repo.findById(id);
}

@Override
public Stream<Portefeuille> recupereTous() {
return StreamSupport.stream(repo.findAll().spliterator(), false);
}

@Override
public void purge() {
repo.deleteAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package devoxx.lab.archihexa.courtage.application.springboot.adapters.persistence;

import devoxx.lab.archihexa.courtage.domain.model.Portefeuille;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.stream.Stream;

public interface PortefeuilleSpringDataCrudRepository extends CrudRepository<Portefeuille, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package devoxx.lab.archihexa.courtage.application.springboot.adapters.rest;

import devoxx.lab.archihexa.courtage.domain.port.secondaire.ServiceBourse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;

import static java.util.Optional.ofNullable;

@Service
public class ServiceBourseHttpAdapter implements ServiceBourse {

private final RestTemplate restTemplate;
private final String bourseUri;

public ServiceBourseHttpAdapter(@Value("${application.bourse.baseUri}") String bourseUri) {
this.bourseUri = bourseUri;
this.restTemplate = new RestTemplate();
}

@Override
public BigDecimal recupererCours(String action) {
return ofNullable(
restTemplate
.getForEntity(bourseUri + "/finance/quote/" + action, Quote.class)
.getBody()
)
.map(Quote::getRegularMarketPriceAsBigDecimal)
.orElseThrow();
}

static class Quote {
private String regularMarketPrice;

void setRegularMarketPrice(String regularMarketPrice) {
this.regularMarketPrice = regularMarketPrice;
}

BigDecimal getRegularMarketPriceAsBigDecimal() {
return new BigDecimal(regularMarketPrice);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,91 @@
package devoxx.lab.archihexa.courtage.application.springboot.controller;

import devoxx.lab.archihexa.courtage.domain.exception.PortefeuilleDejaExistantException;
import devoxx.lab.archihexa.courtage.domain.exception.PortefeuilleNonGereException;
import devoxx.lab.archihexa.courtage.domain.model.Achat;
import devoxx.lab.archihexa.courtage.domain.port.primaire.ServiceCourtage;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;

@RestController
@RequestMapping("/courtage")
@Validated
public class CourtageResource {

private final ServiceCourtage serviceCourtage;

public CourtageResource(ServiceCourtage serviceCourtage) {
this.serviceCourtage = serviceCourtage;
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ResponseEntity<String> handleConstraintViolationException(MethodArgumentNotValidException e) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(e.getFieldErrors().stream()
.map(fe -> "\t" + fe.getField() + " " + fe.getDefaultMessage())
.collect(Collectors.joining("\n", "Donnée(s) erronée(s): \\n\"", "")));
}

@PostMapping("/portefeuilles/{nomPortefeuille}")
public ResponseEntity<String> creationPortefeuille(@PathVariable(value = "nomPortefeuille") String nomPortefeuille) {
try {
serviceCourtage.creerPortefeuille(nomPortefeuille);
} catch (PortefeuilleDejaExistantException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Portefeuille déjà géré");
}
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().build(nomPortefeuille);
return ResponseEntity.created(uri).build();
}

@PostMapping("/portefeuilles/{nomPortefeuille}/actions")
public ResponseEntity<String> ajoutActionsDansPortefeuille(
@PathVariable(value = "nomPortefeuille") String nomPortefeuille,
@Valid @RequestBody Achat achat
) {
try {
serviceCourtage.ajouteAction(nomPortefeuille, achat);
} catch (PortefeuilleNonGereException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Portefeuille non géré");
}
return ResponseEntity.ok().build();
}

@GetMapping("/portefeuilles/{nomPortefeuille}")
public ResponseEntity<Void> portefeuilleExiste(@PathVariable(value = "nomPortefeuille") String nomPortefeuille) {
if (serviceCourtage.gere(nomPortefeuille)) {
return ResponseEntity.ok().build();
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}

@GetMapping("/portefeuilles/{nomPortefeuille}/valorisation")
public ResponseEntity<String> calculValorisationPortefeuille(@PathVariable(value = "nomPortefeuille") String nomPortefeuille) {
try {
return ResponseEntity.ok(serviceCourtage.calculerValeurPortefeuille(nomPortefeuille).toString());
} catch (PortefeuilleNonGereException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Portefeuille non géré");
}
}

@GetMapping("/portefeuilles/avoirs")
public ResponseEntity<String> valeurEnsemblePortefeuilles() {
return ResponseEntity.ok(serviceCourtage.calculerValeurEnsemblePortefeuilles().toString());
}

@GetMapping(path = "/version", produces = MediaType.TEXT_PLAIN_VALUE)
public String version() {
return ofNullable(CourtageResource.class.getPackage().getImplementationVersion())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm https://jakarta.ee/xml/ns/persistence/orm/orm_3_0.xsd"
version="3.0">
<entity class="devoxx.lab.archihexa.courtage.domain.model.Portefeuille">
<attributes>
<id name="nom" access="FIELD"/>
<element-collection name="actions" target-class="java.lang.Integer" access="FIELD">
<map-key-column name="action"/>
<column name="nombre"/>
<collection-table>
<join-column name="portefeuille_nom"/>
</collection-table>
</element-collection>
</attributes>
</entity>
</entity-mappings>
Loading