Skip to content

Commit 347efb8

Browse files
feat: basic crud made
1 parent 52a7d81 commit 347efb8

13 files changed

Lines changed: 290 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.idea

README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
1-
# fastapi-session
1+
<p style="text-align: center;">
2+
<a href="https://dscvit.com">
3+
<img src="https://user-images.githubusercontent.com/30529572/92081025-fabe6f00-edb1-11ea-9169-4a8a61a5dd45.png" alt="DSC VIT"/>
4+
</a>
5+
<h2 style="text-align: center;"> FastAPI Session </h2>
6+
<h4 style="text-align: center;"> 2CC Session Course API </h4>
7+
8+
9+
---
10+
[![Join Us](https://img.shields.io/badge/Join%20Us-Developer%20Student%20Clubs-red)](https://dsc.community.dev/vellore-institute-of-technology/)
11+
[![Discord Chat](https://img.shields.io/discord/760928671698649098.svg)](https://discord.gg/498KVdSKWR)
12+
13+
## Features
14+
15+
- [x] Database Connection Using SQLAlchemy
16+
- [x] FastAPI Server
17+
18+
<br>
19+
20+
## Dependencies
21+
22+
- Python 3.7+
23+
- Pip
24+
- Other listed in requirements.txt
25+
26+
## Running
27+
28+
- Clone the repo using
29+
30+
```bash
31+
git clone https://github.com/mdhishaamakhtar/fastapi-session
32+
```
33+
34+
- Create a Virtual Environment using
35+
36+
```bash
37+
sudo pip install virtualenv
38+
virtualenv env
39+
```
40+
41+
- Activate the virtualenv
42+
43+
```bash
44+
env\Scripts\activate # for windows
45+
source env/bin/activate # for linux and mac
46+
```
47+
48+
- Install dependencies
49+
50+
```bash
51+
pip install -r requirements.txt
52+
```
53+
54+
- To run the project
55+
56+
```bash
57+
uvicorn main:app
58+
```
59+
60+
## Contributors
61+
62+
<table>
63+
<tr style="text-align: center;">
64+
<td>
65+
Md Hishaam Akhtar
66+
<p style="text-align: center;">
67+
<img src = "https://user-images.githubusercontent.com/58990970/103586688-9cde9700-4f0b-11eb-915c-0d8b9a555159.JPG" width="150" height="150" alt="Md Hishaam Akhtar">
68+
</p>
69+
<p style="text-align: center;">
70+
<a href = "https://github.com/mdhishaamakhtar">
71+
<img src = "https://www.iconninja.com/files/241/825/211/round-collaboration-social-github-code-circle-network-icon.svg" width="36" height = "36" alt="GitHub"/>
72+
</a>
73+
<a href = "https://www.linkedin.com/in/mdhishaamakhtar">
74+
<img src = "https://www.iconninja.com/files/863/607/751/network-linkedin-social-connection-circular-circle-media-icon.svg" width="36" height="36" alt="LinkedIn"/>
75+
</a>
76+
</p>
77+
</td>
78+
</tr>
79+
</table>
80+
81+
<p style="text-align: center;">
82+
Made with :heart: by <a href="https://dscvit.com">DSC VIT</a>
83+
</p>

database/__init__.py

Whitespace-only changes.

database/connection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from decouple import config
2+
from sqlalchemy import create_engine
3+
from sqlalchemy.ext.declarative import declarative_base
4+
from sqlalchemy.orm import sessionmaker
5+
6+
# SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
7+
SQLALCHEMY_DATABASE_URL = config("DATABASE_URL")
8+
9+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
10+
11+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
12+
13+
Base = declarative_base()
14+
15+
16+
def get_db():
17+
db = SessionLocal()
18+
try:
19+
yield db
20+
finally:
21+
db.close()

database/models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import uuid
2+
3+
from sqlalchemy import Column, String
4+
from sqlalchemy.dialects.postgresql import UUID
5+
6+
from database.connection import Base, engine
7+
8+
9+
class Posts(Base):
10+
__tablename__ = "posts"
11+
12+
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
13+
title = Column(String)
14+
description = Column(String)
15+
16+
17+
Base.metadata.create_all(engine)

main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from fastapi import FastAPI
2+
3+
from schemas.schemas import HealthResponse
4+
from routes.posts import router
5+
6+
app = FastAPI()
7+
8+
app.include_router(router=router, prefix="/posts")
9+
10+
11+
@app.get("/", response_model=HealthResponse)
12+
async def health():
13+
return HealthResponse(status="Ok")

requirements.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
aiofiles==0.5.0
2+
aniso8601==7.0.0
3+
appdirs==1.4.4
4+
async-exit-stack==1.0.1
5+
async-generator==1.10
6+
black==20.8b1
7+
certifi==2020.12.5
8+
chardet==4.0.0
9+
click==7.1.2
10+
colorama==0.4.4
11+
dnspython==2.1.0
12+
email-validator==1.1.2
13+
fastapi==0.63.0
14+
graphene==2.1.8
15+
graphql-core==2.3.2
16+
graphql-relay==2.0.1
17+
greenlet==1.0.0
18+
h11==0.12.0
19+
idna==2.10
20+
itsdangerous==1.1.0
21+
Jinja2==2.11.3
22+
MarkupSafe==1.1.1
23+
mypy-extensions==0.4.3
24+
orjson==3.5.2
25+
pathspec==0.8.1
26+
promise==2.3
27+
psycopg2==2.8.6
28+
pydantic==1.8.1
29+
python-decouple==3.4
30+
python-dotenv==0.17.0
31+
python-multipart==0.0.5
32+
PyYAML==5.4.1
33+
regex==2021.4.4
34+
requests==2.25.1
35+
Rx==1.6.1
36+
six==1.15.0
37+
SQLAlchemy==1.4.10
38+
starlette==0.13.6
39+
toml==0.10.2
40+
typed-ast==1.4.3
41+
typing-extensions==3.7.4.3
42+
ujson==3.2.0
43+
urllib3==1.26.4
44+
uvicorn==0.13.4
45+
watchgod==0.7
46+
websockets==8.1

routes/__init__.py

Whitespace-only changes.

routes/posts.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import List
2+
3+
from fastapi import APIRouter, status, Depends, HTTPException
4+
from sqlalchemy.orm import Session
5+
6+
from database.connection import get_db
7+
from schemas.schemas import Post, DeletePostResponse
8+
from utils.post_crud import (
9+
post_create,
10+
posts_get_all,
11+
post_delete,
12+
post_get_one,
13+
post_update,
14+
)
15+
16+
router = APIRouter(tags=["posts"])
17+
18+
19+
@router.post("/create", status_code=status.HTTP_201_CREATED, response_model=Post)
20+
def create_post(post: Post, db: Session = Depends(get_db)):
21+
return post_create(db=db, post=post)
22+
23+
24+
@router.get("/list/all", status_code=status.HTTP_200_OK, response_model=List[Post])
25+
def get_all_posts(db: Session = Depends(get_db)):
26+
return posts_get_all(db=db)
27+
28+
29+
@router.get("/get/{id}", status_code=status.HTTP_200_OK, response_model=Post)
30+
def get_all_posts(id, db: Session = Depends(get_db)):
31+
return post_get_one(db=db, id=id)
32+
33+
34+
@router.delete(
35+
"/delete/{id}", status_code=status.HTTP_200_OK, response_model=DeletePostResponse
36+
)
37+
def delete_post(id, db: Session = Depends(get_db)):
38+
delete_status = post_delete(db=db, id=id)
39+
if delete_status.detail == "Doesnt Exist":
40+
raise HTTPException(
41+
status_code=status.HTTP_404_NOT_FOUND, detail="Post Not Found"
42+
)
43+
else:
44+
return delete_status
45+
46+
47+
@router.patch("/update", status_code=status.HTTP_200_OK, response_model=Post)
48+
def update_post(post: Post, db: Session = Depends(get_db)):
49+
return post_update(db=db, post=post)

schemas/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)