-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (87 loc) · 2.97 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
from fastapi import FastAPI, HTTPException
from fastapi_sqlalchemy import DBSessionMiddleware, db
from sqlalchemy.exc import SQLAlchemyError
from models import Deals
from schema import DealsCreate,DealUpdate
app = FastAPI()
app.add_middleware(DBSessionMiddleware, db_url="postgresql+psycopg2://admin:admin@bsedb:5432/bsedeals")
@app.get("/")
def list_deals():
"""
Get a list of all deals.
Returns:
dict: A dictionary containing a list of deals.
"""
deals = db.session.query(Deals).all()
return {"message": deals}
@app.post("/")
def create_deal(deal: DealsCreate):
"""
Create a new deal.
Args:
deal: The deal data to create.
Returns:
dict: A dictionary containing a message indicating success.
"""
try:
new_deal = Deals(
deal_date=deal.deal_date,
security_code=deal.security_code,
security_name=deal.security_name,
client_name=deal.client_name,
deal_type=deal.deal_type,
quantity=deal.quantity,
price=deal.price
)
db.session.add(new_deal)
db.session.commit()
return {"message": "Deal created successfully"}
except SQLAlchemyError as e:
db.session.rollback()
raise HTTPException(status_code=400, detail="Could not create deal")
@app.patch("/")
def update_deal(deal: DealUpdate):
"""
Update an existing deal.
Args:
deal: The deal data to update.
Returns:
dict: A dictionary containing a message indicating success.
"""
try:
existing_deal = db.session.query(Deals).get(deal.id)
if existing_deal:
existing_deal.deal_date = deal.deal_date
existing_deal.security_code = deal.security_code
existing_deal.security_name = deal.security_name
existing_deal.client_name = deal.client_name
existing_deal.deal_type = deal.deal_type
existing_deal.quantity = deal.quantity
existing_deal.price = deal.price
db.session.commit()
return {"message": "Deal updated successfully"}
else:
raise HTTPException(status_code=404, detail="Deal not found")
except SQLAlchemyError as e:
db.session.rollback()
raise HTTPException(status_code=400, detail="Could not update deal")
@app.delete("/")
def delete_deal(deal_id: int):
"""
Delete a deal.
Args:
deal_id: The ID of the deal to delete.
Returns:
dict: A dictionary containing a message indicating success.
"""
try:
deal = db.session.query(Deals).get(deal_id)
if deal:
db.session.delete(deal)
db.session.commit()
return {"message": "Deal deleted successfully"}
else:
raise HTTPException(status_code=404, detail="Deal not found")
except SQLAlchemyError as e:
db.session.rollback()
raise HTTPException(status_code=400, detail="Could not delete deal")