This commit is contained in:
yungez 2017-09-20 13:25:36 +08:00 коммит произвёл GitHub
Родитель 8645e3488b
Коммит ba39aa5907
2 изменённых файлов: 50 добавлений и 3 удалений

Просмотреть файл

@ -16,6 +16,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class SimpleDocumentDbRepository<T, ID extends Serializable> implements DocumentDbRepository<T, ID> {
@ -68,7 +69,18 @@ public class SimpleDocumentDbRepository<T, ID extends Serializable> implements D
*/
@Override
public <S extends T> Iterable<S> save(Iterable<S> entities) {
throw new UnsupportedOperationException("save not supported yet.");
// create collection if not exists
documentDbOperations.createCollectionIfNotExists(entityInformation.getCollectionName(),
entityInformation.getPartitionKeyFieldName(),
entityInformation.getRequestUint());
for (final S entity : entities) {
documentDbOperations.insert(entityInformation.getCollectionName(),
entity,
entityInformation.getPartitionKeyFieldValue(entity));
}
return entities;
}
/**
@ -90,7 +102,15 @@ public class SimpleDocumentDbRepository<T, ID extends Serializable> implements D
*/
@Override
public List<T> findAll(Iterable<ID> ids) {
throw new UnsupportedOperationException("findAll not supported yet.");
final List<T> entities = new ArrayList<T>();
for (final ID id : ids) {
final T entity = findOne(id);
if (entity != null) {
entities.add(entity);
}
}
return entities;
}
/**
@ -167,7 +187,9 @@ public class SimpleDocumentDbRepository<T, ID extends Serializable> implements D
*/
@Override
public void delete(Iterable<? extends T> entities) {
throw new UnsupportedOperationException("delete not supported yet.");
for (final T entity : entities) {
delete(entity);
}
}
/**

Просмотреть файл

@ -5,6 +5,7 @@
*/
package com.microsoft.azure.spring.data.cosmosdb.documentdb.repository;
import org.assertj.core.util.Lists;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -13,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ -99,4 +101,27 @@ public class ContactRepositoryIT {
assertThat(contact.getLogicId()).isEqualTo(updatedContact.getLogicId());
assertThat(contact.getTitle()).isEqualTo(updatedContact.getTitle());
}
@Test
public void testBatchOperations() {
final Contact contact1 = new Contact("newid1", "newtitle");
final Contact contact2 = new Contact("newid2", "newtitle");
final ArrayList<Contact> contacts = new ArrayList<Contact>();
contacts.add(contact1);
contacts.add(contact2);
repository.save(contacts);
final ArrayList<String> ids = new ArrayList<String>();
ids.add(contact1.getLogicId());
ids.add(contact2.getLogicId());
final List<Contact> result = Lists.newArrayList(repository.findAll(ids));
assertThat(result.size()).isEqualTo(2);
repository.delete(contacts);
final List<Contact> result2 = Lists.newArrayList(repository.findAll(ids));
assertThat(result2.size()).isEqualTo(0);
}
}