Skip to content

Commit 9a7bd0d

Browse files
surajitsurajit
authored andcommitted
move contact api endpoint to contact route
1 parent 1c3cb3b commit 9a7bd0d

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

main.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
from sqlalchemy.orm import Session
77

88
from crud.company import CompanyExistException
9-
from crud.contact import ContactExistException
109
from dependencies.dependencies import get_db
1110
from schemas import schema
1211
from hubspot_api import utils
1312
from crud import company as _company
14-
from crud import contact as _contact
1513
from routes import email
14+
from routes import contact
1615

1716
app = FastAPI()
1817
app.include_router(email.router)
19-
18+
app.include_router(contact.router)
2019
templates = Jinja2Templates(directory="templates")
2120

2221

@@ -27,24 +26,6 @@ def read_root(request: Request):
2726
)
2827

2928

30-
@app.post("/contacts")
31-
def create_contact(contact: schema.CreateContact, db: Session = Depends(get_db)):
32-
db_contact = None
33-
hubspot_contact = None
34-
try:
35-
org_name = contact.company_name
36-
db_org = _company.filter_company_by_name(db, org_name)
37-
if db_org:
38-
org = db_org.all()[0]
39-
db_contact = _contact.create_contact(db, contact, org.id)
40-
hubspot_contact = utils.create_contact(data=contact.dict())
41-
except (utils.ContactException, ContactExistException) as exc:
42-
if isinstance(exc, utils.ContactException) and db_contact:
43-
_contact.delete_contact(db, db_contact.id)
44-
raise HTTPException(status_code=200, detail=str(exc))
45-
return hubspot_contact
46-
47-
4829
@app.post("/companies")
4930
def create_company(company: schema.CreateCompany, db: Session = Depends(get_db)):
5031
db_company = None
@@ -65,12 +46,3 @@ def get_company(company_name):
6546
except utils.CompanyException as exc:
6647
raise HTTPException(status_code=200, detail=str(exc))
6748
return company
68-
69-
70-
@app.get("/contact/{email}/")
71-
def get_contact(email):
72-
try:
73-
contact = utils.get_contact_by_email(email)
74-
except utils.ContactException as exc:
75-
raise HTTPException(status_code=200, detail=str(exc))
76-
return contact

routes/contact.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
# TODO - Move contact routes from main.py to here
1+
from fastapi import APIRouter
2+
from fastapi import Depends
3+
from fastapi import HTTPException
4+
from sqlalchemy.orm import Session
5+
6+
from crud.contact import ContactExistException
7+
from crud import contact as _contact
8+
from schemas import schema
9+
from hubspot_api import utils
10+
from dependencies.dependencies import get_db
11+
from crud import company as _company
12+
13+
router = APIRouter(
14+
tags=["contact"],
15+
responses={404: {"description": "Not found"}},
16+
)
17+
18+
19+
@router.post("/contacts/")
20+
def create_contact(contact: schema.CreateContact, db: Session = Depends(get_db)):
21+
db_contact = None
22+
hubspot_contact = None
23+
try:
24+
org_name = contact.company_name
25+
db_org = _company.filter_company_by_name(db, org_name)
26+
if db_org:
27+
org = db_org.all()[0]
28+
db_contact = _contact.create_contact(db, contact, org.id)
29+
hubspot_contact = utils.create_contact(data=contact.dict())
30+
except (utils.ContactException, ContactExistException) as exc:
31+
if isinstance(exc, utils.ContactException) and db_contact:
32+
_contact.delete_contact(db, db_contact.id)
33+
raise HTTPException(status_code=200, detail=str(exc))
34+
return hubspot_contact
35+
36+
37+
@router.get("/contact/{email}/")
38+
def get_contact(email):
39+
try:
40+
contact = utils.get_contact_by_email(email)
41+
except utils.ContactException as exc:
42+
raise HTTPException(status_code=200, detail=str(exc))
43+
return contact

0 commit comments

Comments
 (0)