ExampleServiceRpcClient.java
6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.infoloop.tianting.service.client;
import com.infoloop.tianting.model.common.PageInfo;
import com.infoloop.tianting.model.create.TestExampleDbCreate;
import com.infoloop.tianting.model.query.TestExampleDbQuery;
import com.infoloop.tianting.model.update.TestExampleDbUpdate;
import com.tianting.infoloop.grpc.exampleservice.BatchCreateTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.BatchCreateTestExampleDbRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.BatchUpdateTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.CreateTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.CreateTestExampleDbRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.DeleteTestExampleDbByIdsRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.ExampleServiceRpcGrpc;
import com.tianting.infoloop.grpc.exampleservice.GetAllTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.GetTestExampleDbByIdRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.GetTestExampleDbByIdsRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.QueryTestExampleDbByConditionRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.QueryTestExampleDbByPaginationRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.QueryTestExampleDbByPaginationRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.SingleTestExampleDbRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.TestExampleDbCreation;
import com.tianting.infoloop.grpc.exampleservice.TestExampleDbModification;
import com.tianting.infoloop.grpc.exampleservice.UpdateTestExampleDbRpcRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExampleServiceRpcClient {
private final ExampleServiceRpcGrpc.ExampleServiceRpcBlockingStub exampleServiceRpcBlockingStub;
/**
* RPC Client示例
*/
@Nullable
public SingleTestExampleDbRpcResponse getTestExampleDbById(int id) {
final var response = exampleServiceRpcBlockingStub.getTestExampleDbById(GetTestExampleDbByIdRpcRequest.newBuilder()
.setId(id)
.build());
if (!response.hasResponse()) {
return null;
}
return response.getResponse();
}
public List<SingleTestExampleDbRpcResponse> getTestExampleDbByIds(List<Integer> ids) {
return exampleServiceRpcBlockingStub.getTestExampleDbByIds(GetTestExampleDbByIdsRpcRequest.newBuilder()
.addAllIds(ids)
.build()).getResponsesList();
}
public List<SingleTestExampleDbRpcResponse> getAllTestExampleDb() {
return exampleServiceRpcBlockingStub.getAllTestExampleDb(GetAllTestExampleDbRpcRequest.newBuilder().build()).getResponsesList();
}
public List<SingleTestExampleDbRpcResponse> queryTestExampleDbByCondition(TestExampleDbQuery query) {
final var builder = QueryTestExampleDbByConditionRpcRequest.newBuilder();
if (query.getName() != null) {
builder.setName(query.getName());
}
if (query.getStatus() != null) {
builder.setStatus(query.getStatus());
}
return exampleServiceRpcBlockingStub.queryTestExampleDbByCondition(builder.build()).getResponsesList();
}
public QueryTestExampleDbByPaginationRpcResponse queryTestExampleDbByPagination(PageInfo pageInfo, TestExampleDbQuery query) {
final var builder = QueryTestExampleDbByPaginationRpcRequest.newBuilder();
builder.setPageNo(pageInfo.getPageNo());
builder.setPageSize(pageInfo.getPageSize());
if (query.getName() != null) {
builder.setName(query.getName());
}
if (query.getStatus() != null) {
builder.setStatus(query.getStatus());
}
return exampleServiceRpcBlockingStub.queryTestExampleDbByPagination(builder.build());
}
public CreateTestExampleDbRpcResponse createTestExampleDb(TestExampleDbCreate create) {
final var creation = TestExampleDbCreation.newBuilder()
.setName(create.getName())
.setStatus(create.getStatus())
.build();
return exampleServiceRpcBlockingStub.createTestExampleDb(CreateTestExampleDbRpcRequest.newBuilder().setCreation(creation).build());
}
public BatchCreateTestExampleDbRpcResponse batchCreateTestExampleDb(List<TestExampleDbCreate> createList) {
final var creationList = createList.stream().map(e -> TestExampleDbCreation.newBuilder()
.setName(e.getName())
.setStatus(e.getStatus())
.build()).collect(Collectors.toList());
return exampleServiceRpcBlockingStub.batchCreateTestExampleDb(BatchCreateTestExampleDbRpcRequest.newBuilder().addAllCreations(creationList).build());
}
public boolean updateTestExampleDb(TestExampleDbUpdate update) {
final var builder = TestExampleDbModification.newBuilder()
.setId(update.getId());
if (update.getName() != null) {
builder.setName(update.getName());
}
if (update.getStatus() != null) {
builder.setStatus(update.getStatus());
}
return exampleServiceRpcBlockingStub.updateTestExampleDb(UpdateTestExampleDbRpcRequest.newBuilder().setModification(builder.build()).build()).getIsUpdated();
}
public boolean batchUpdateTestExampleDb(List<TestExampleDbUpdate> updateList) {
final var modificationList = updateList.stream().map(update -> {
final var builder = TestExampleDbModification.newBuilder()
.setId(update.getId());
if (update.getName() != null) {
builder.setName(update.getName());
}
if (update.getStatus() != null) {
builder.setStatus(update.getStatus());
}
return builder.build();
}).collect(Collectors.toList());
return exampleServiceRpcBlockingStub.batchUpdateTestExampleDb(BatchUpdateTestExampleDbRpcRequest.newBuilder().addAllModifications(modificationList).build()).getIsUpdated();
}
public boolean deleteTestExampleDbByIds(List<Integer> ids) {
return exampleServiceRpcBlockingStub.deleteTestExampleDbByIds(DeleteTestExampleDbByIdsRpcRequest.newBuilder().addAllIds(ids).build()).getIsDeleted();
}
}