-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
173 lines (136 loc) · 5.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from functools import lru_cache
from fastapi import FastAPI, Depends
from neo4j import GraphDatabase
from neo4j.exceptions import ClientError
import os
import logging
from config import Settings
from models.models import Supplier, Order, Product, Customer
from fastapi.responses import RedirectResponse
app = FastAPI()
@lru_cache
def get_settings():
return Settings()
URI = os.environ["URI"]
USER = os.environ["USER"]
PASSWORD = os.environ["PASSWORD"]
DATABASE = os.environ["DATABASE"]
driver = GraphDatabase.driver(URI, auth=(USER, PASSWORD))
@app.get("/")
async def root(settings: Settings = Depends(get_settings)):
return RedirectResponse(url='/docs')
def _add_products(tx, product):
query = (
"MERGE (p:Product { productID: $prod_id, productName: $prod_name }) "
"RETURN p AS products "
)
result = tx.run(query, prod_id=product.productID, prod_name=product.productName)
try:
return [(row["products"]) for row in result]
except ClientError as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise exception
@app.post("/add_product/", tags=["insert-node"])
async def add_products(product: Product):
with driver.session(database=DATABASE) as session:
result = session.write_transaction(
_add_products, product)
return result
def _delete_products(tx, product):
query = (
"MATCH (p:Product { productID: $prod_id }) "
"DETACH DELETE p "
)
result = tx.run(query, prod_id=product.productID, prod_name=product.productName)
try:
return {"message": "Product {} has been deleted ".format(product.productID)}
except ClientError as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise exception
@app.post("/delete_product/", tags=["delete-node"])
async def delete_products(product: Product):
with driver.session(database=DATABASE) as session:
result = session.write_transaction(
_delete_products, product)
return result
def _get_nodes(tx, name):
query = (
"MATCH (n:{node_name} ) "
"RETURN n AS nodes "
"LIMIT 25 "
).format(node_name=name)
result = tx.run(query, node_name=name)
return [(row["nodes"]) for row in result]
@app.get("/nodes/{name}")
async def get_nodes(name: str):
with driver.session(database=DATABASE) as session:
result = session.read_transaction(_get_nodes, name)
return result
def _get_sales_employee(tx, emp_id):
query = (
"MATCH(e:Employee) - [:SOLD] -> (o:Order) "
"WHERE e.employeeId = $emp_id "
"RETURN count(o.orderID) AS OrderCount "
)
result = tx.run(query, emp_id=emp_id)
return [(row["OrderCount"]) for row in result]
@app.get("/sales/{employeeId}")
async def get_sales_employee(employeeId: int):
with driver.session(database=DATABASE) as session:
result = session.read_transaction(_get_sales_employee, employeeId)
return result
def _get_product_supplier(tx, prod_id):
query = (
"MATCH (s:Supplier)-[:SUPPLY]->(p:Product) "
"WHERE p.productID = $prod_id "
"RETURN s.companyName AS company, s.contactName AS contact, s.phone AS phone"
)
result = tx.run(query, prod_id=prod_id)
return [{"company": row["company"], "contact": row["contact"], "phone": row["phone"]} for row in result]
@app.get("/supplier/{prod_id}")
async def get_product_supplier(prod_id: int):
with driver.session(database=DATABASE) as session:
result = session.read_transaction(_get_product_supplier, prod_id)
return result
def _add_supplier(tx, supplier):
query = (
"MERGE (s:Supplier { supplierID:$sup_id, companyName: $comp_name, contactName: $cont_name, contactTitle: $cont_title, fax: $supp_cont, phone: $supp_phone, supp_homepage: $homepage}) "
"RETURN s AS sup "
)
result = tx.run(query, sup_id=supplier.supplierID, comp_name=supplier.companyName, cont_name=supplier.contactName,
cont_title=supplier.contactTitle, supp_cont=supplier.fax, supp_phone=supplier.phone,
supp_homepage=supplier.homepage)
try:
return [row["sup"] for row in result]
except ClientError as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise exception
@app.post("/supplier/", tags=["insert-node"])
async def add_supplier(supplier: Supplier):
with driver.session(database=DATABASE) as session:
result = session.write_transaction(
_add_supplier, supplier)
return result
def _add_customer_purchase_order(tx, order, customer):
query = (
"MATCH (c:Customer { customerID:$cust_id }) "
"MERGE (o:Order { orderId: $ord_id, orderDate: datetime() }) "
"MERGE (c) - [:PLACED] -> (o) "
"RETURN o AS ord "
)
result = tx.run(query, cust_id=customer.customerID, ord_id=order.orderID)
try:
return [row["ord"] for row in result]
except ClientError as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise exception
@app.post("/purchase_order/", tags=["insert-relationship"])
async def add_customer_purchase_order(order: Order, customer: Customer):
with driver.session(database=DATABASE) as session:
result = session.write_transaction(
_add_customer_purchase_order, order, customer)
return result