io.elsci.signals.mock.entities.SignalsEntityApi Maven / Gradle / Ivy
The newest version!
package io.elsci.signals.mock.entities;
import io.elsci.signals.mock.EntityDto;
import io.elsci.signals.mock.assets.AssetFactory;
import io.elsci.signals.mock.assets.AssetsDao;
import io.elsci.signals.sdk.entities.SearchEntry;
import io.elsci.signals.sdk.entities.SearchQuery;
import io.elsci.signals.sdk.entities.SearchResult;
import org.bson.Document;
import org.springframework.web.bind.annotation.*;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.*;
import static io.elsci.signals.mock.SignalsCommonUtils.SIGNALS_API_ROOT;
@RestController
@RequestMapping(SIGNALS_API_ROOT + "/entities")
public class SignalsEntityApi {
private final AssetsDao assetsDao;
public SignalsEntityApi(AssetsDao assetsDao) {
this.assetsDao = assetsDao;
}
/**
* For now it handles only requests to find with these $and and $or
*
* {
* "query": {
* "$and": [
* {"$match": {"field": "type", "value": "asset", "mode": "keyword"}},
* {"$or": [
* {"$match": {"field": "name", "value": "PROT_ID1", "mode": "keyword"}},
* {"$match": {"field": "name", "value": "PROT_ID2", "mode": "keyword"}}
* ]
* }
* ]
* }
* }
*
*/
@PostMapping("/search")
SearchResult search(@RequestBody SearchQuery query) {
List result = new ArrayList<>();
Map mongoQuery = toMongoQuery(query.get());
assetsDao.find(new Document(mongoQuery)).forEach(doc -> {
Map attributes = new HashMap<>(doc);
attributes.remove("_id");
String id = doc.getString("_id");
attributes.put("id", id);
result.add(new SearchEntry(id, withLocalDateTimeInsteadOfDate(attributes)));
});
return new SearchResult(result);
}
@PostMapping("/random-protein")
public EntityDto createRandomProtein() {
return new EntityDto(assetsDao.save(AssetFactory.protein()));
}
@SuppressWarnings("unchecked")
private static Map toMongoQuery(Map query) {
Map result = new HashMap<>();
for (Map.Entry e : query.entrySet()) {
if (e.getKey().equals("$match")) {
Map match = (Map) e.getValue();
result.put((String) match.get("field"), match.get("value"));
continue;
}
if (e.getValue() instanceof List) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy