11package com .example .demo ;
22
3- import data . MockFutureEitherClientRepository ;
4- import data . MockFutureEitherEmployeeRepository ;
5- import operators . Op ;
3+ import Commands . GetClientCommand ;
4+ import Commands . GetClientCommandHandler ;
5+ import Pipelinr . Pipeline ;
66import org .springframework .boot .SpringApplication ;
77import org .springframework .boot .autoconfigure .SpringBootApplication ;
88import org .springframework .http .HttpStatus ;
99import org .springframework .http .ResponseEntity ;
10- import org .springframework .web .bind .annotation .*;
11- import org .springframework .boot .SpringApplication ;
12- import org .springframework .boot .autoconfigure .SpringBootApplication ;
10+ import org .springframework .web .bind .annotation .RequestMapping ;
11+ import org .springframework .web .bind .annotation .RequestMethod ;
12+ import org .springframework .web .bind .annotation .RequestParam ;
13+ import org .springframework .web .bind .annotation .RestController ;
1314
14- import static io .vavr .API .*;
15- import static io .vavr .Patterns .$Left ;
16- import static io .vavr .Patterns .$Right ;
15+ import java .util .stream .Stream ;
16+
17+ import static io .vavr .API .Try ;
18+ import static operators .Op .throwableMessage ;
1719
1820@ SpringBootApplication
1921@ RestController
2022public class DemoApplication {
21-
23+ static Pipeline mediator ;
2224 public static void main (String [] args ) {
2325 SpringApplication .run (DemoApplication .class , args );
26+ mediator = new Pipelinr .Pipelinr ()
27+ .with (
28+ () -> Stream .of (new GetClientCommandHandler ())
29+ );
2430 }
2531
2632 @ RequestMapping (value = "/search" , method = RequestMethod .GET )
2733 public ResponseEntity <SearchViewModel > search (@ RequestParam (value = "clientId" , defaultValue = "0" ) Integer clientId ) {
28- try {
2934
30- var clients = new MockFutureEitherClientRepository ();
31- var employees = new MockFutureEitherEmployeeRepository ();
35+ return Try (mediator .send (new GetClientCommand (clientId ))::get )
36+ .getOrElseGet (throwableMessage ())
37+ .fold (
38+ error -> new ResponseEntity <>(new SearchViewModel (error ), HttpStatus .BAD_REQUEST ),
39+ value -> new ResponseEntity <>(new SearchViewModel (value .getName ()), HttpStatus .OK )
40+ );
3241
33- var eitherEmployeeFuture =
34- clients .getClientById (clientId )
35- .thenApplyAsync (Op .map (x -> x .getEmployeeId ()))
36- .thenComposeAsync (Op .bind (employees ::getClientById ))
37- .get ();
3842
39- var result = Match (eitherEmployeeFuture ).of (
40- Case ($Right ($ ()), value -> value .getName ()),
41- Case ($Left ($ ()), error -> error ));
43+ }
4244
43- var resultViewModel = new SearchViewModel ();
44- resultViewModel .setResult (result );
4545
46- return new ResponseEntity <>( resultViewModel , HttpStatus . OK );
46+ /* public ResponseEntity<SearchViewModel> search1(Integer clientId) {
4747
48- } catch (Exception e ) {
49- return new ResponseEntity <>(null , HttpStatus .BAD_REQUEST );
50- }
48+ var eitherEmployeeFuture = clients
49+ .getClientById(clientId)
50+ .thenApplyAsync(map(Client::getEmployeeId))
51+ .thenComposeAsync(bind(employees::getClientById));
5152
52- }
53+ var eitherEmployee =
54+ Try(eitherEmployeeFuture::get)
55+ .getOrElseGet(x -> Either.left(x.getMessage()));
56+
57+ var resultViewModel = Match(eitherEmployee).of(
58+ Case($Right($()), value -> new ResponseEntity<>(new SearchViewModel(value.getName()), HttpStatus.OK)),
59+ Case($Left($()), error -> new ResponseEntity<>(new SearchViewModel(error), HttpStatus.BAD_REQUEST)));
5360
61+ return resultViewModel;
5462
55- }
63+ }*/
64+ }
0 commit comments