Commit 6796bbfc authored by Manuel's avatar Manuel

[businessLogic] added repository for enums

parent b50f87f5
......@@ -465,6 +465,107 @@ paths:
description: "Layer does not exist"
'400':
description: "Field in request is missing"
#####
# END LAYERS
#####
#####
# ENUMS
#####
/enums:
get:
security:
- JwtRegular: []
operationId: "routes.enum.all"
tags:
- "Enums"
summary: "Retrieve all Enums from the DB"
description: "Retrieve all Enums from the DB"
responses:
'200':
description: "Successful Request"
/use-cases/{use_case}/enums:
get:
security:
- JwtRegular: []
operationId: "routes.enum.get_all_for_use_case"
tags:
- "Enums"
summary: "Retrieve one Enum from the DB"
description: "Retrieve one Enum from the DB"
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Enum belongs to"
required: true
type: "string"
responses:
'200':
description: "Successful Request"
'404':
description: "Enum does not exist"
/use-cases/{use_case}/tables/{table}/enum/{name}:
put:
security:
- JwtRegular: []
operationId: "routes.enum.put_new"
summary: "Updates an existing Enum with a new value."
description: "Updates an existing Enum with a new value."
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Enum belongs to"
required: true
type: "string"
- name: "table"
in: "path"
description: "Name of the Table the Enum belongs to"
required: true
type: "string"
- name: "name"
in: "path"
description: "Name of the Enum"
required: true
type: "string"
- in: query
name: value
schema:
type: string
description: Value of the Enum.
responses:
'200':
description: "Successful Request"
'404':
description: "Enum does not exist"
get:
security:
- JwtRegular: []
operationId: "routes.enum.one"
tags:
- "Enums"
summary: "Retrieve one Enum from the DB"
description: "Retrieve one Enum from the DB"
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Enum belongs to"
required: true
type: "string"
- name: "table"
in: "path"
description: "Name of the Table the Enum belongs to"
required: true
type: "string"
- name: "name"
in: "path"
description: "Name of the Enum"
required: true
type: "string"
responses:
'200':
description: "Successful Request"
'404':
description: "Enum does not exist"
definitions:
LayerMapping:
......
from typing import Dict
class Enum:
def __init__(self, use_case: str, table: str, name: str, value: str, index: int):
self.use_case = use_case
self.table = table
self.name = name
self.value = value
self.index = index
def to_serializable_dict(self) -> Dict:
return {
"use_case": self.use_case,
"table": self.table,
"name": self.name,
"value": self.value,
"index": self.index,
}
@staticmethod
def from_serializable_dict(enum_dict: Dict):
'''
creates a layer object from a dictionary. has to have the following keys:
- name
- properties
- cluster_properties
- use_case
'''
return Enum(
enum_dict["use_case"],
enum_dict["table"],
enum_dict["name"],
enum_dict["value"],
enum_dict["index"],
)
# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.enum import Enum
from typing import List
class EnumRepository(MongoRepositoryBase):
'''This is a repository for MongoDb.'''
def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT,
'business-logic-db')
self._enum_collection = 'enums'
def all(self) -> List[Enum]:
result = super().get_entries(
self._enum_collection, projection={'_id': False})
return [Enum.from_serializable_dict(row) for row in list(result)]
def all_for_use_case(self, use_case: str) -> List[Enum]:
result = super().get_entries(self._enum_collection, projection={
'_id': False}, selection={"use_case": use_case})
return [Enum.from_serializable_dict(row) for row in list(result)]
def all_for_use_case_and_table(self, use_case: str, table: str) -> List[Enum]:
result = super().get_entries(self._enum_collection, projection={
'_id': False}, selection={"use_case": use_case, "table": table})
return [Enum.from_serializable_dict(row) for row in list(result)]
def one(self, name: str, use_case: str, table: str) -> Enum:
result = list(super().get_entries(self._enum_collection, selection={
"name": name, "use_case": use_case, "table": table}))
if len(result) == 1:
return Enum.from_serializable_dict(result[0])
return None
def delete_all(self):
collection = self._database[self._enum_collection]
collection.delete_many({})
def add(self, enum: Enum):
super().insert_entry(self._enum_collection, enum.to_serializable_dict())
def update_use_case(self, enum: Enum, use_case: str):
collection = self._database[self._enum_collection]
collection.update_one({"name": enum.name, "use_case": use_case, "table": enum.table}, {
"$set": enum.to_serializable_dict()})
def update(self, enum: Enum):
collection = self._database[self._enum_collection]
collection.update_one({"name": enum.name, "use_case": enum.use_case, "table": enum.table}, {
"$set": enum.to_serializable_dict()})
def delete(self, enum: Enum):
collection = self._database[self._enum_collection]
collection.delete_many(
{"name": enum.name, "use_case": enum.use_case, "table": enum.table})
#global imports
from db.entities.enum import Enum
from db.entities.layer_adapter import LayerAdapter
from db.repository import Repository
from db.table_repository import TableRepository
from db.use_case_repository import UseCaseRepository
from db.enum_repository import EnumRepository
import json
from flask import Response, request
table_repository = TableRepository()
use_case_repository = UseCaseRepository()
enum_repository = EnumRepository()
def all():
return [enum.to_serializable_dict() for enum in enum_repository.all()]
def get_all_for_use_case(use_case: str):
'''
get all enums assigned to the given use_case
'''
use_case_repository.put(use_case)
return [enum.to_serializable_dict() for enum in enum_repository.all_for_use_case(use_case)]
def one(use_case: str, table: str, name: str):
'''
fetch a single enum from the DB
@params:
use_case - Required : String-identifier for the Use-Case the Enum belongs to
table - Required : unique identifier of the Table the Enum belongs to
name - Required : unique identifier for the Enum
'''
enum = enum_repository.one(name, use_case, table)
if enum == None:
return Response(status=404, response=f"Enum with name '{name}' does not exist!")
return Response(status=200, response=json.dumps(enum.to_serializable_dict()))
def put_new(use_case: str, table: str, name: str, value: str):
'''
Put a new Enum to the DB. If the value for this enum already exists, nothing happens.
@params:
use_case - Required : String-identifier for the Use-Case the Enum belongs to
table - Required : unique identifier of the Table the Enum belongs to
name - Required : unique identifier for the Enum
'''
existing_enum = enum_repository.one(name, use_case, table)
if existing_enum == None:
return Response(status=404, response=f"Enum with name '{name}' does not exist!")
# todo
return Response(status=200, response=json.dumps(existing_enum.to_serializable_dict()))
\ 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