Skip to content

Commit a33f047

Browse files
surajitsurajit
authored andcommitted
add charge endpoint
1 parent 1febaf7 commit a33f047

13 files changed

+200
-9
lines changed

app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
from routes.v1 import email
88
from routes.v1 import contact
99
from routes.v1 import company
10+
from routes.v1 import charge
1011

1112
app = FastAPI()
1213
app.include_router(email.router)
1314
app.include_router(contact.router)
1415
app.include_router(company.router)
16+
app.include_router(charge.router)
1517

1618
templates = Jinja2Templates(directory="templates")
1719

crud/charge.py

Whitespace-only changes.

migrations/env.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424

2525
# add your model's MetaData object here
2626
# for 'autogenerate' support
27-
from models import payment
27+
from models.payment import Charge
2828
from models import contact
2929
from models import message
30+
3031
target_metadata = Base.metadata
3132

3233

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""add created_at and modified_at to charge table
2+
3+
Revision ID: 573a2f55e475
4+
Revises: 3f4f812d4ca6
5+
Create Date: 2022-05-26 19:50:38.717366
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '573a2f55e475'
14+
down_revision = '3f4f812d4ca6'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('charge', sa.Column('created_at', sa.DateTime(), nullable=True))
22+
op.add_column('charge', sa.Column('modified_at', sa.DateTime(), nullable=True))
23+
# ### end Alembic commands ###
24+
25+
26+
def downgrade():
27+
# ### commands auto generated by Alembic - please adjust! ###
28+
op.drop_column('charge', 'modified_at')
29+
op.drop_column('charge', 'created_at')
30+
# ### end Alembic commands ###
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""remove unique constraint
2+
3+
Revision ID: 5bf098d393cd
4+
Revises: 9b9c2855cad1
5+
Create Date: 2022-05-26 20:04:41.897338
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '5bf098d393cd'
14+
down_revision = '9b9c2855cad1'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
pass
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
pass
28+
# ### end Alembic commands ###
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""add unique constraint for charge type and org_id for charge table
2+
3+
Revision ID: 9b9c2855cad1
4+
Revises: 573a2f55e475
5+
Create Date: 2022-05-26 19:53:14.981004
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '9b9c2855cad1'
14+
down_revision = '573a2f55e475'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
pass
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
pass
28+
# ### end Alembic commands ###
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""modify unique constraint
2+
3+
Revision ID: a9e00646e1d7
4+
Revises: ad9ebc89187d
5+
Create Date: 2022-05-26 23:23:53.573992
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'a9e00646e1d7'
14+
down_revision = 'ad9ebc89187d'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_unique_constraint(None, 'charge', ['org_id', 'charge_type'])
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.drop_constraint(None, 'charge', type_='unique')
28+
# ### end Alembic commands ###
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""add unique constraint to charge
2+
3+
Revision ID: ad9ebc89187d
4+
Revises: 5bf098d393cd
5+
Create Date: 2022-05-26 20:05:35.891406
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'ad9ebc89187d'
14+
down_revision = '5bf098d393cd'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
pass
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
pass
28+
# ### end Alembic commands ###

models/payment.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import enum
2+
import datetime
23

34
from sqlalchemy import Column
45
from sqlalchemy import Integer
56
from sqlalchemy import String
67
from sqlalchemy import Enum
78
from sqlalchemy import ForeignKey
9+
from sqlalchemy import DateTime
10+
from sqlalchemy import UniqueConstraint
811
from sqlalchemy.orm import relationship
912

1013
from database import Base
@@ -32,13 +35,15 @@ class Organization(Base):
3235

3336
class Charge(Base):
3437
__tablename__ = "charge"
35-
38+
__table_args__ = (UniqueConstraint("org_id", "charge_type"),)
3639
id = Column(Integer, primary_key=True, index=True)
3740
currency = Column(Enum(Currency), nullable=False, default=Currency.USD)
3841
amount = Column(Integer, nullable=False)
3942
charge_type = Column(Enum(ChargeType), nullable=False)
4043
org_id = Column(Integer, ForeignKey("organization.id"))
4144
organization = relationship("Organization", backref="charges")
45+
created_at = Column(DateTime, default=datetime.datetime.now)
46+
modified_at = Column(DateTime, default=datetime.datetime.now)
4247

4348

4449
class Payment(Base):

routes/v1/charge.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import datetime
2+
3+
from fastapi import APIRouter
4+
from fastapi import Depends
5+
from fastapi import HTTPException
6+
from fastapi import Request
7+
from sqlalchemy.orm import Session
8+
9+
from crud.charge import ChargeExistException
10+
from crud.company import filter_company_by_name
11+
from logger.log import save_log
12+
from dependencies.dependencies import get_db
13+
from crud import charge as _charge
14+
15+
router = APIRouter(
16+
prefix="/api/v1",
17+
tags=["charge"],
18+
responses={404: {"description": "Not found"}},
19+
)
20+
21+
22+
@router.post("/charges/")
23+
@save_log
24+
async def create_charge(request: Request, db: Session = Depends(get_db)):
25+
try:
26+
data = await request.json()
27+
company_name = data["company_name"]
28+
company = filter_company_by_name(db, company_name).all()[0]
29+
company_data = dict(
30+
currency=data["currency"],
31+
amount=data["amount"],
32+
charge_type=data["charge_type"],
33+
org_id=company.id,
34+
created_at=datetime.datetime.now(),
35+
)
36+
response = await _charge.add_charge(db, company_data)
37+
except ChargeExistException as exc:
38+
raise HTTPException(status_code=200, detail=str(exc))
39+
return response

0 commit comments

Comments
 (0)