Skip to content

Commit 5ebaff0

Browse files
committed
improve deleteRecords api,support delete record every 10 only
1 parent f802e26 commit 5ebaff0

4 files changed

Lines changed: 216 additions & 1 deletion

File tree

client/src/main/java/cn/vika/client/api/RecordApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.stream.Collectors;
2425
import java.util.stream.Stream;
2526

2627
import cn.vika.client.api.exception.ApiException;
@@ -36,6 +37,7 @@
3637
import cn.vika.client.api.model.UpdateRecordRequest;
3738
import cn.vika.core.http.GenericTypeReference;
3839
import cn.vika.core.http.HttpHeader;
40+
import cn.vika.core.utils.CollectionUtil;
3941
import cn.vika.core.utils.MapUtil;
4042
import cn.vika.core.utils.StringUtil;
4143

@@ -127,6 +129,24 @@ public void deleteRecords(String datasheetId, List<String> recordIds) throws Api
127129
if (recordIds.isEmpty()) {
128130
throw new ApiException("record id array must be not empty");
129131
}
132+
if (recordIds.size() > 10) {
133+
List<List<String>> splitList = CollectionUtil.splitListParallel(recordIds, 10);
134+
splitList.forEach(split -> deleteLimitSize(datasheetId, split));
135+
}
136+
else {
137+
deleteLimitSize(datasheetId, recordIds);
138+
}
139+
}
140+
141+
public void deleteAllRecords(String datasheetId) throws ApiException {
142+
Stream<Record> recordStream = getRecordsAsStream(datasheetId);
143+
List<String> recordIds = recordStream.map(Record::getRecordId).collect(Collectors.toList());
144+
if (!recordIds.isEmpty()) {
145+
deleteRecords(datasheetId, recordIds);
146+
}
147+
}
148+
149+
private void deleteLimitSize(String datasheetId, List<String> recordIds) {
130150
Map<String, String> uriVariables = MapUtil.listToUriVariableMap("recordIds", recordIds);
131151
String uri = String.format(PATH, datasheetId) + MapUtil.extractKeyToVariables(uriVariables);
132152
getDefaultHttpClient().delete(uri, new HttpHeader(), Void.class, uriVariables);

client/src/test/java/cn/vika/client/api/RecordOperationTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void testDeleteRecordBatch() throws IOException, ApiException {
325325
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, createRecordRequest);
326326
assertThat(newRecords).isNotNull();
327327
assertThat(newRecords).isNotEmpty();
328-
assertThat(newRecords).hasSize(3);
328+
assertThat(newRecords).hasSize(10);
329329

330330
List<String> recordIds = newRecords.stream().map(Record::getRecordId).collect(Collectors.toList());
331331

@@ -339,6 +339,33 @@ void testDeleteRecordBatch() throws IOException, ApiException {
339339
assertThat(pager.getTotalItems()).isZero();
340340
}
341341

342+
@Test
343+
@Order(61)
344+
public void testDeleteAllRecordsOnEmptyData() {
345+
vikaApiClient.getRecordApi().deleteAllRecords(DATASHEET_ID);
346+
}
347+
348+
@Test
349+
@Order(62)
350+
public void testDeleteAllRecordsOnNoEmptyData() throws IOException {
351+
ClassLoader classLoader = getClass().getClassLoader();
352+
InputStream inputStream = classLoader.getResourceAsStream("delete-many-record.json");
353+
assertThat(inputStream).isNotNull();
354+
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalInputStreamToList(RecordMap.class, inputStream);
355+
CreateRecordRequest createRecordRequest = new CreateRecordRequest().withRecords(recordMaps);
356+
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, createRecordRequest);
357+
assertThat(newRecords).isNotNull();
358+
assertThat(newRecords).isNotEmpty();
359+
assertThat(newRecords).hasSize(10);
360+
361+
vikaApiClient.getRecordApi().deleteAllRecords(DATASHEET_ID);
362+
363+
// assert query whether record exist
364+
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID);
365+
assertThat(pager).isNotNull();
366+
assertThat(pager.getTotalItems()).isZero();
367+
}
368+
342369
public static class TestFieldDTO {
343370

344371
@JsonProperty("ShortText")

client/src/test/resources/create-record.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,89 @@
2222
"LL"
2323
]
2424
}
25+
},
26+
{
27+
"fields": {
28+
"ShortText": "This is from unit Test create record 4",
29+
"LongText": "This is from unit Test create record 4",
30+
"Options": [
31+
"F"
32+
],
33+
"MultiSelect": [
34+
"LL"
35+
]
36+
}
37+
},
38+
{
39+
"fields": {
40+
"ShortText": "This is from unit Test create record 5",
41+
"LongText": "This is from unit Test create record 5",
42+
"Options": [
43+
"F"
44+
],
45+
"MultiSelect": [
46+
"LL"
47+
]
48+
}
49+
},
50+
{
51+
"fields": {
52+
"ShortText": "This is from unit Test create record 6",
53+
"LongText": "This is from unit Test create record 6",
54+
"Options": [
55+
"F"
56+
],
57+
"MultiSelect": [
58+
"LL"
59+
]
60+
}
61+
},
62+
{
63+
"fields": {
64+
"ShortText": "This is from unit Test create record 7",
65+
"LongText": "This is from unit Test create record 7",
66+
"Options": [
67+
"F"
68+
],
69+
"MultiSelect": [
70+
"LL"
71+
]
72+
}
73+
},
74+
{
75+
"fields": {
76+
"ShortText": "This is from unit Test create record 8",
77+
"LongText": "This is from unit Test create record 8",
78+
"Options": [
79+
"F"
80+
],
81+
"MultiSelect": [
82+
"LL"
83+
]
84+
}
85+
},
86+
{
87+
"fields": {
88+
"ShortText": "This is from unit Test create record 9",
89+
"LongText": "This is from unit Test create record 9",
90+
"Options": [
91+
"F"
92+
],
93+
"MultiSelect": [
94+
"LL"
95+
]
96+
}
97+
},
98+
{
99+
"fields": {
100+
"ShortText": "This is from unit Test create record 10",
101+
"LongText": "This is from unit Test create record 10",
102+
"Options": [
103+
"F"
104+
],
105+
"MultiSelect": [
106+
"LL"
107+
]
108+
}
25109
}
26110
]

client/src/test/resources/delete-many-record.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,89 @@
3434
"EE"
3535
]
3636
}
37+
},
38+
{
39+
"fields": {
40+
"ShortText": "This is from unit Test create record 4",
41+
"LongText": "This is from unit Test create record 4",
42+
"Options": [
43+
"F"
44+
],
45+
"MultiSelect": [
46+
"LL"
47+
]
48+
}
49+
},
50+
{
51+
"fields": {
52+
"ShortText": "This is from unit Test create record 5",
53+
"LongText": "This is from unit Test create record 5",
54+
"Options": [
55+
"F"
56+
],
57+
"MultiSelect": [
58+
"LL"
59+
]
60+
}
61+
},
62+
{
63+
"fields": {
64+
"ShortText": "This is from unit Test create record 6",
65+
"LongText": "This is from unit Test create record 6",
66+
"Options": [
67+
"F"
68+
],
69+
"MultiSelect": [
70+
"LL"
71+
]
72+
}
73+
},
74+
{
75+
"fields": {
76+
"ShortText": "This is from unit Test create record 7",
77+
"LongText": "This is from unit Test create record 7",
78+
"Options": [
79+
"F"
80+
],
81+
"MultiSelect": [
82+
"LL"
83+
]
84+
}
85+
},
86+
{
87+
"fields": {
88+
"ShortText": "This is from unit Test create record 8",
89+
"LongText": "This is from unit Test create record 8",
90+
"Options": [
91+
"F"
92+
],
93+
"MultiSelect": [
94+
"LL"
95+
]
96+
}
97+
},
98+
{
99+
"fields": {
100+
"ShortText": "This is from unit Test create record 9",
101+
"LongText": "This is from unit Test create record 9",
102+
"Options": [
103+
"F"
104+
],
105+
"MultiSelect": [
106+
"LL"
107+
]
108+
}
109+
},
110+
{
111+
"fields": {
112+
"ShortText": "This is from unit Test create record 10",
113+
"LongText": "This is from unit Test create record 10",
114+
"Options": [
115+
"F"
116+
],
117+
"MultiSelect": [
118+
"LL"
119+
]
120+
}
37121
}
38122
]

0 commit comments

Comments
 (0)