-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (117 loc) · 3.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
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
"""API for contact database"""
from typing import List, Optional
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from sqlalchemy.orm import Session
import models
from db import SessionLocal
app = FastAPI()
# allows cross-origin requests from React
origins = [
"http://localhost",
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Contact(BaseModel):
"""Contact model"""
id: Optional[int] = None
first_name: str
last_name: str
company: Optional[str] = None
telephone: Optional[str] = None
email: Optional[str] = None
address: Optional[str] = None
notes: Optional[str] = None
class Config:
"""Pydantic config"""
orm_mode = True
def get_db():
"""creates seperate sessions for each request"""
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/all-contacts", response_model=List[Contact], status_code=status.HTTP_200_OK)
def get_all_contacts(db: Session = Depends(get_db)):
"""READ: Get all contacts"""
return db.query(models.Contact).all()
@app.get(
"/get-contact/{contact_id}", response_model=Contact, status_code=status.HTTP_200_OK
)
def get_contact(contact_id: int, db: Session = Depends(get_db)):
"""READ: Get a contact by id"""
return db.query(models.Contact).filter(models.Contact.id == contact_id).first()
@app.post(
"/create-contact", response_model=Contact, status_code=status.HTTP_201_CREATED
)
def create_contact(contact: Contact, db: Session = Depends(get_db)):
"""CREATE: Create a new contact"""
db_contact = (
db.query(models.Contact)
.filter(
models.Contact.first_name == contact.first_name
and models.Contact.last_name == contact.last_name
and models.Contact.company == contact.company
and models.Contact.telephone == contact.telephone
and models.Contact.email == contact.email
and models.Contact.address == contact.address
and models.Contact.notes == contact.notes
)
.first()
)
if db_contact is not None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="🤨 Contact already exists"
)
new_contact = models.Contact(
first_name=contact.first_name,
last_name=contact.last_name,
company=contact.company,
telephone=contact.telephone,
email=contact.email,
address=contact.address,
notes=contact.notes,
)
db.add(new_contact)
db.commit()
return new_contact
@app.patch(
"/update-contact/{contact_id}",
response_model=Contact,
status_code=status.HTTP_200_OK,
)
def update_contact(contact_id: int, contact: Contact, db: Session = Depends(get_db)):
"""UPDATE: Update a contact"""
contact_to_update = (
db.query(models.Contact).filter(models.Contact.id == contact_id).first()
)
contact_to_update.first_name = contact.first_name
contact_to_update.last_name = contact.last_name
contact_to_update.company = contact.company
contact_to_update.telephone = contact.telephone
contact_to_update.email = contact.email
contact_to_update.address = contact.address
contact_to_update.notes = contact.notes
db.commit()
return contact_to_update
@app.delete("/delete-contact/{contact_id}")
def delete_contact(contact_id: int, db: Session = Depends(get_db)):
"""DELETE: Delete a contact"""
contact_to_delete = (
db.query(models.Contact).filter(models.Contact.id == contact_id).first()
)
if contact_to_delete is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="✋ Contact does not exsist"
)
db.delete(contact_to_delete)
db.commit()
return {"message": "👌 Contact deleted"}