Commit 21e89a22 authored by Alexander Lercher's avatar Alexander Lercher

404 if layers/nodes dont exist (#11)

parent 8296faab
......@@ -60,6 +60,8 @@ paths:
description: "Successful operation"
schema:
$ref: "#/definitions/Node"
'404':
description: "No nodes found"
# endregion nodes
# Layers region
......@@ -101,6 +103,8 @@ paths:
description: "Successful operation"
schema:
$ref: "#/definitions/Layer"
'404':
description: "Layer not found"
/use-cases/{use_case}/layers:
get:
......@@ -109,7 +113,7 @@ paths:
operationId: "routes.layers.get_by_use_case"
tags:
- "Layers"
summary: "Get single layer data"
summary: "Get all layer data for single use case"
parameters:
- name: "use_case"
in: "path"
......@@ -120,7 +124,9 @@ paths:
'200':
description: "Successful operation"
schema:
$ref: "#/definitions/Layer"
$ref: "#/definitions/LayerCollection"
'404':
description: "No layers found"
# endregion layers
definitions:
......
......@@ -52,6 +52,8 @@ paths:
description: "Successful operation"
schema:
$ref: "#/definitions/Node"
'404':
description: "No nodes found"
# endregion nodes
# Layers region
......@@ -90,13 +92,15 @@ paths:
description: "Successful operation"
schema:
$ref: "#/definitions/Layer"
'404':
description: "Layer not found"
/use-cases/{use_case}/layers:
get:
operationId: "routes.layers.get_by_use_case"
tags:
- "Layers"
summary: "Get single layer data"
summary: "Get all layer data for single use case"
parameters:
- name: "use_case"
in: "path"
......@@ -107,7 +111,9 @@ paths:
'200':
description: "Successful operation"
schema:
$ref: "#/definitions/Layer"
$ref: "#/definitions/LayerCollection"
'404':
description: "No layers found"
# endregion layers
definitions:
......
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.layer import Layer
from db.entities.raw_data import Raw_Dataset
from db.entities import Layer
import pymongo
import json
from typing import List, Dict
# init logging to file
import logging
LOG_FORMAT = ('%(levelname) -5s %(asctime)s %(name)s:%(funcName) -35s %(lineno) -5d: %(message)s')
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
LOGGER = logging.getLogger(__name__)
class Repository(MongoRepositoryBase):
'''This is a repository for MongoDb.'''
......@@ -14,27 +19,12 @@ class Repository(MongoRepositoryBase):
super().__init__(netconst.SEMANTIC_LINKING_DB_HOSTNAME,
netconst.SEMANTIC_LINKING_DB_PORT,
'semanticLinkingDb')
self._raw_dataset = 'raw_dataset'
self._layer_collection = 'layers'
self._layer_nodes_collection = 'layer_nodes'
self._clusters_collection = 'clusters'
self._time_slice_collection = 'time_slices'
#region Raw_data
def add_raw_data(self, dataset: Raw_Dataset):
super().insert_entry(self._raw_dataset, dataset.to_serializable_dict())
def get_all_raw_data(self) -> List[Raw_Dataset]:
entries = super().get_entries(self._raw_dataset)
return [Raw_Dataset(e) for e in entries]
def get_raw_data(self, usecase_name) -> Raw_Dataset:
entries = super().get_entries(self._raw_dataset, selection={'usecase_name': usecase_name})
return [Raw_Dataset(e) for e in entries]
# end region
# region Layers
def add_layer(self, layer: Layer):
super().insert_entry(self._layer_collection, layer.to_serializable_dict(for_db=True))
......@@ -50,7 +40,6 @@ class Repository(MongoRepositoryBase):
def get_layer(self, layer_name) -> Layer:
entries = super().get_entries(self._layer_collection, selection={'layer_name': layer_name})
entries = [Layer(e) for e in entries]
if entries is not None and len(entries) > 0:
return entries[0]
else:
......@@ -60,9 +49,13 @@ class Repository(MongoRepositoryBase):
entries = super().get_entries(self._layer_collection, selection={'use_case': use_case})
return [Layer(e) for e in entries]
def get_layers_by_name_and_use_case(self, layer_name: str, use_case: str) -> List[Layer]:
def get_layers_by_name_and_use_case(self, layer_name: str, use_case: str) -> Layer:
entries = super().get_entries(self._layer_collection, selection={'layer_name': layer_name, 'use_case': use_case})
return [Layer(e) for e in entries]
entries = list(entries) # materialize
if len(entries) > 1:
LOGGER.error(f"Use case + layer name was not unique for {use_case} and {layer_name}.")
return None
return Layer(entries[0]) if len(entries) == 1 else None
def delete_layer(self, layer_name:str):
collection = self._database[self._layer_collection]
......
......@@ -17,17 +17,17 @@ def get_by_name(name):
return Response(status=404)
def get_by_name_and_use_case(name: str, use_case: str):
return [layer.to_serializable_dict() for layer in repo.get_layers_by_name_and_use_case(name, use_case)]
res = repo.get_layers_by_name_and_use_case(name, use_case)
if res is not None:
return res.to_serializable_dict()
else:
return Response(status=404)
def get_by_use_case(use_case: str):
return [layer.to_serializable_dict() for layer in repo.get_layers_by_use_case(use_case)]
#endregion
#region nodes
def get_nodes(name):
res = repo.get_layer_nodes(name)
# print(res)
res = [layer.to_serializable_dict() for layer in repo.get_layers_by_use_case(use_case)]
if res is not None and len(res) > 0:
return res
else:
return Response(status=404)
#endregion nodes
\ No newline at end of file
#endregion
......@@ -9,4 +9,8 @@ repo = Repository()
#region nodes
def nodes_for_use_case_and_layer(layer_name: str, use_case: str) -> List[Dict]:
return repo.get_layer_nodes_with_use_case(layer_name, use_case)
\ No newline at end of file
res = repo.get_layer_nodes_with_use_case(layer_name, use_case)
if res is not None and len(res) > 0:
return res
else:
return Response(status=404)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment