Skip to content

Commit

Permalink
create user update
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedManan committed Aug 22, 2023
1 parent cd497e9 commit e0628bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
Binary file modified blog.db
Binary file not shown.
15 changes: 12 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fastapi import FastAPI, Depends, status, Response, HTTPException
# from typing import Annotated
from typing import List
import models, schemas
from database import engine, SessionLocal
from sqlalchemy.orm import Session
Expand All @@ -24,7 +24,7 @@ def get_db():
async def credits():
return {"message": "Welcome to the Blog API. Developed By MAnan Ahmed Broti. Website: AhmedManan.com"}

@app.get('/blog')
@app.get('/blog', response_model=List[schemas.ShowBlog])
def all_posts(db : Session = Depends(get_db)):
blogs = db.query(models.Blog).all()
return blogs
Expand All @@ -37,7 +37,7 @@ def create_post(request : schemas.Blog, db : Session = Depends(get_db)):
db.refresh(new_blog)
return new_blog

@app.get('/blog/{blog_id}', status_code=status.HTTP_200_OK)
@app.get('/blog/{blog_id}', status_code=status.HTTP_200_OK, response_model=schemas.ShowBlog)
def get_post(blog_id, response : Response, db : Session = Depends(get_db)):
blog = db.query(models.Blog).filter(models.Blog.id== blog_id).first()
if not blog:
Expand Down Expand Up @@ -68,3 +68,12 @@ def delete_post(blog_id, db : Session = Depends(get_db)):
blog.delete(synchronize_session=False)
db.commit()
return {f'Blog post deleted!'}

@app.post('/user')
def create_user(request: schemas.User, db: Session = Depends(get_db)):
new_user = models.User(**request.dict()) # Use the constructor to create a new instance
db.add(new_user)
db.commit()
db.refresh(new_user)

return new_user
12 changes: 12 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ class Blog(Base):
author = Column(String)
tags = Column(String)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())


class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, index=True)
name = Column(String)
email = Column(String) # Fixed typo here
password = Column(String)
user_type = Column(String) # Renamed "type" to "user_type"
status = Column(String)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
16 changes: 14 additions & 2 deletions schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@ class Blog(BaseModel):
tags:str
# published: Optional [bool] = False

class ShowBlog(Blog):
pass
class ShowBlog(BaseModel):
title:str
body:str
author:str
tags:str
class config():
orm_mode = True

class User(BaseModel):
name:str
email:str
password:str
user_type:str
status:str

0 comments on commit e0628bb

Please sign in to comment.