Skip to content

Commit fd44b67

Browse files
author
Ben Lei
committed
Update RecordDeleteRequest for the latest protocol
1 parent a78ba3d commit fd44b67

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

skygear/src/androidTest/java/io/skygear/skygear/RecordDeleteRequestUnitTest.java

+25-19
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,37 @@ public void testRecordDeleteRequestNormalFlow() throws Exception {
6161
= new RecordDeleteRequest(new Record[]{note1, note2}, instrumentationPublicDatabase);
6262
Map<String, Object> data = request.data;
6363
assertEquals("_public", data.get("database_id"));
64-
assertEquals("Note", data.get("recordType"));
65-
66-
JSONArray recordIDs = (JSONArray) data.get("recordIDs");
67-
68-
assertEquals(2, recordIDs.length());
69-
assertEquals("9C0F4536-FEA7-42DB-B8EF-561CCD175E06", recordIDs.getString(0));
70-
assertEquals("05A2946A-72DC-4F20-99F9-129BD1FCB52A", recordIDs.getString(1));
7164

7265
JSONArray deprecatedIDs = (JSONArray) data.get("ids");
7366
assertEquals(2, deprecatedIDs.length());
7467
assertEquals("Note/9C0F4536-FEA7-42DB-B8EF-561CCD175E06", deprecatedIDs.getString(0));
7568
assertEquals("Note/05A2946A-72DC-4F20-99F9-129BD1FCB52A", deprecatedIDs.getString(1));
7669

70+
JSONArray recordIdentifiers = (JSONArray) data.get("records");
71+
assertEquals(2, recordIdentifiers.length());
72+
assertEquals(
73+
"Note/9C0F4536-FEA7-42DB-B8EF-561CCD175E06",
74+
recordIdentifiers.getJSONObject(0).getString("_id"));
75+
assertEquals(
76+
"Note",
77+
recordIdentifiers.getJSONObject(0).getString("_recordType")
78+
);
79+
assertEquals(
80+
"9C0F4536-FEA7-42DB-B8EF-561CCD175E06",
81+
recordIdentifiers.getJSONObject(0).getString("_recordID")
82+
);
83+
assertEquals(
84+
"Note/05A2946A-72DC-4F20-99F9-129BD1FCB52A",
85+
recordIdentifiers.getJSONObject(1).getString("_id"));
86+
assertEquals(
87+
"Note",
88+
recordIdentifiers.getJSONObject(1).getString("_recordType")
89+
);
90+
assertEquals(
91+
"05A2946A-72DC-4F20-99F9-129BD1FCB52A",
92+
recordIdentifiers.getJSONObject(1).getString("_recordID")
93+
);
94+
7795
request.validate();
7896
}
7997

@@ -83,16 +101,4 @@ public void testRecordSaveRequestNotAllowSaveNoRecords() throws Exception {
83101
= new RecordDeleteRequest(new Record[]{}, instrumentationPublicDatabase);
84102
request.validate();
85103
}
86-
87-
@Test(expected = InvalidParameterException.class)
88-
public void testRecordSaveRequestNotAllowMultiTypeRecords() throws Exception {
89-
RecordDeleteRequest request = new RecordDeleteRequest(
90-
new Record[]{
91-
new Record("Note"),
92-
new Record("Comment")
93-
},
94-
instrumentationPublicDatabase
95-
);
96-
request.validate();
97-
}
98104
}

skygear/src/main/java/io/skygear/skygear/RecordDeleteRequest.java

+38-28
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,35 @@
1717

1818
package io.skygear.skygear;
1919

20+
import android.util.Log;
21+
2022
import org.json.JSONArray;
23+
import org.json.JSONException;
24+
import org.json.JSONObject;
2125

2226
import java.security.InvalidParameterException;
2327
import java.util.ArrayList;
24-
import java.util.Arrays;
2528
import java.util.HashMap;
26-
import java.util.HashSet;
2729
import java.util.List;
28-
import java.util.Set;
30+
31+
import static io.skygear.skygear.RecordSerializer.RecordIdentifier;
2932

3033
/**
3134
* The Skygear Record Delete Request.
3235
*/
3336
public class RecordDeleteRequest extends Request {
37+
private static final String TAG = "Skygear SDK";
38+
3439
private String databaseId;
35-
private List<Record> records;
40+
private List<RecordIdentifier> recordIdentifiers;
3641

3742
/**
3843
* Instantiates a new record delete request with default properties.
3944
*/
4045
RecordDeleteRequest() {
4146
super("record:delete");
4247
this.data = new HashMap<>();
43-
this.records = new ArrayList<>();
48+
this.recordIdentifiers = new ArrayList<>();
4449
}
4550

4651
/**
@@ -52,30 +57,44 @@ public class RecordDeleteRequest extends Request {
5257
public RecordDeleteRequest(Record[] records, Database database) {
5358
this();
5459
this.databaseId = database.getName();
55-
this.records = Arrays.asList(records);
60+
61+
for (Record perRecord : records) {
62+
this.recordIdentifiers.add(
63+
new RecordIdentifier(perRecord.type, perRecord.id)
64+
);
65+
}
66+
5667
this.updateData();
5768
}
5869

5970
private void updateData() {
60-
String recordType = null;
61-
JSONArray recordIDs = new JSONArray();
6271
JSONArray deprecatedIDs = new JSONArray();
63-
for (Record perRecord : this.records) {
64-
if (recordType == null) {
65-
recordType = perRecord.getType();
66-
}
72+
JSONArray recordIdentifiers = new JSONArray();
73+
74+
try {
75+
for (RecordIdentifier perRecordIdentifier : this.recordIdentifiers) {
76+
String perRecordDeprecatedID = String.format(
77+
"%s/%s",
78+
perRecordIdentifier.type,
79+
perRecordIdentifier.id
80+
);
81+
82+
deprecatedIDs.put(perRecordDeprecatedID);
6783

68-
recordIDs.put(perRecord.getId());
69-
deprecatedIDs.put(String.format("%s/%s", perRecord.getType(), perRecord.getId()));
84+
JSONObject perRecordIdentifierData = new JSONObject();
85+
perRecordIdentifierData.put("_id", perRecordDeprecatedID);
86+
perRecordIdentifierData.put("_recordType", perRecordIdentifier.type);
87+
perRecordIdentifierData.put("_recordID", perRecordIdentifier.id);
88+
89+
recordIdentifiers.put(perRecordIdentifierData);
90+
}
91+
} catch(JSONException e) {
92+
Log.w(TAG, "Fail to serialize record identifiers");
7093
}
7194

7295
this.data.put("ids", deprecatedIDs);
73-
this.data.put("recordIDs", recordIDs);
96+
this.data.put("records", recordIdentifiers);
7497
this.data.put("database_id", this.databaseId);
75-
76-
if (recordType != null) {
77-
this.data.put("recordType", recordType);
78-
}
7998
}
8099

81100
@Override
@@ -86,14 +105,5 @@ protected void validate() throws Exception {
86105
if (ids.length() == 0) {
87106
throw new InvalidParameterException("No records to be processed");
88107
}
89-
90-
Set<String> typeSet = new HashSet<>();
91-
for (Record perRecord : this.records) {
92-
typeSet.add(perRecord.getType());
93-
}
94-
95-
if (typeSet.size() > 1) {
96-
throw new InvalidParameterException("Only records in the same type are allowed");
97-
}
98108
}
99109
}

0 commit comments

Comments
 (0)