Skip to content

Commit 70027aa

Browse files
dimitdimit
authored andcommitted
added mediator
1 parent 4b4973f commit 70027aa

File tree

7 files changed

+115
-34
lines changed

7 files changed

+115
-34
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</properties>
2020

2121
<dependencies>
22+
2223
<dependency>
2324
<groupId>org.springframework.boot</groupId>
2425
<artifactId>spring-boot-starter-web</artifactId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Commands;
2+
3+
import Pipelinr.Command;
4+
import io.vavr.control.Either;
5+
import model.Employee;
6+
7+
import java.util.concurrent.CompletableFuture;
8+
9+
public class GetClientCommand implements Command<CompletableFuture<Either<String, Employee>>> {
10+
11+
public Integer getClientId() {
12+
return clientId;
13+
}
14+
15+
private Integer clientId;
16+
17+
public GetClientCommand(Integer clientId) {
18+
this.clientId = clientId;
19+
}
20+
21+
22+
}
23+
24+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Commands;
2+
3+
import Pipelinr.Command;
4+
import data.MockFutureEitherClientRepository;
5+
import data.MockFutureEitherEmployeeRepository;
6+
import io.vavr.control.Either;
7+
import model.Client;
8+
import model.Employee;
9+
10+
import java.util.concurrent.CompletableFuture;
11+
12+
import static operators.Op.bind;
13+
import static operators.Op.map;
14+
15+
public class GetClientCommandHandler implements Command.Handler<GetClientCommand, CompletableFuture<Either<String, Employee>>> {
16+
17+
MockFutureEitherClientRepository clients = new MockFutureEitherClientRepository();
18+
MockFutureEitherEmployeeRepository employees = new MockFutureEitherEmployeeRepository();
19+
20+
@Override
21+
public CompletableFuture<Either<String, Employee>> handle(GetClientCommand command) {
22+
23+
return clients
24+
.getClientById(command.getClientId())
25+
.thenApplyAsync(map(Client::getEmployeeId))
26+
.thenComposeAsync(bind(employees::getClientById));
27+
}
28+
}
Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
11
package 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;
66
import org.springframework.boot.SpringApplication;
77
import org.springframework.boot.autoconfigure.SpringBootApplication;
88
import org.springframework.http.HttpStatus;
99
import 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
2022
public 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+
}

src/main/java/com/example/demo/SearchViewModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public class SearchViewModel {
99
private String type;
1010
private String result;
1111

12-
public SearchViewModel() {
12+
public SearchViewModel(String result) {
13+
this.result=result;
1314
}
1415

1516
public String getType() {

src/main/java/model/Client.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package model;
22

3-
43
public class Client {
54
private Integer id;
65
private String name;

src/main/java/operators/Op.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package operators;
22

3+
import com.example.demo.SearchViewModel;
4+
import io.vavr.Value;
35
import io.vavr.control.Either;
6+
import model.Employee;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
49

510
import java.util.concurrent.CompletableFuture;
611
import java.util.concurrent.ExecutionException;
@@ -11,15 +16,29 @@
1116
import static io.vavr.Patterns.$Right;
1217

1318
public class Op {
14-
public static <L, T, T1> Function<Either<L, T>, Either<L, T1>> map(Function<T, T1> fn)
15-
throws InterruptedException, ExecutionException {
19+
public static <R> Function<Throwable,Either<String, R>> throwableMessage( ) {
20+
return x->Either.left(x.getMessage());
21+
}
22+
23+
public static <L, T, T1> Function<Either<L, T>, Either<L, T1>> map(Function<T, T1> fn) {
1624
return $source -> $source.map(fn);
1725
}
1826

1927
public static <L, T, T1> Function<Either<L, T>, CompletableFuture<Either<L, T1>>> bind(Function<T, CompletableFuture<Either<L, T1>>> fn)
20-
throws InterruptedException, ExecutionException {
28+
{
2129
return $source -> Match($source).of(
2230
Case($Right($()), value -> fn.apply(value)),
2331
Case($Left($()), error -> CompletableFuture.completedFuture(Either.left(error))));
2432
}
33+
34+
35+
// public static <L, T, T1> Function<Either<L, T>, CompletableFuture<Either<L, T1>>> bind(Function<T, CompletableFuture<Either<L, T1>>> fn)
36+
// {
37+
// var resultViewModel = Match(eitherEmployee).of(
38+
// Case($Right($()), value -> new ResponseEntity<>(new SearchViewModel(value.getName()), HttpStatus.OK)),
39+
// Case($Left($()), error -> new ResponseEntity<>(new SearchViewModel(error), HttpStatus.BAD_REQUEST)));
40+
//
41+
// }
42+
43+
2544
}

0 commit comments

Comments
 (0)