qa (#6)
All checks were successful
Build and Push to Private Registry101 / build (push) Successful in 8s
All checks were successful
Build and Push to Private Registry101 / build (push) Successful in 8s
Co-authored-by: stephen schuresko <mynameisdeleted@gmail.com> Reviewed-on: https://192.168.1.134:3000/posts-api/papi-py/pulls/6
This commit was merged in pull request #6.
This commit is contained in:
52
posts.py
Normal file
52
posts.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import sessionmaker, Session, declarative_base
|
||||
from sqlalchemy import create_engine, Column, Integer,String, ForeignKey
|
||||
|
||||
import db
|
||||
|
||||
class Post(db.Base):
|
||||
__tablename__ = "posts"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String)
|
||||
content = Column(String)
|
||||
author_id = Column(Integer, ForeignKey("users.id"))
|
||||
|
||||
|
||||
router = APIRouter(prefix="/posts")
|
||||
|
||||
@router.get("")
|
||||
def get_users(db: Session = Depends(db.get_db)):
|
||||
return db.query(Post).all()
|
||||
|
||||
@router.get("/{id}")
|
||||
def get_user( id, db: Session = Depends(db.get_db)):
|
||||
db_post = db.query(Post).filter(Post.id == id).first()
|
||||
return db_post
|
||||
|
||||
@router.post("")
|
||||
def create_post( post: dict, db: Session = Depends(db.get_db)):
|
||||
db_post = Post(**post)
|
||||
db.add(db_post)
|
||||
db.commit()
|
||||
db.refresh(db_post)
|
||||
return {"message":"Post created successfully","post":db_post}
|
||||
|
||||
@router.put("/{id}")
|
||||
def update_post( id, post: dict, db: Session = Depends(db.get_db)):
|
||||
db_post = db.query(Post).filter(Post.id == id).first()
|
||||
if "title" in post:
|
||||
db_post.title = post["title"]
|
||||
if "content" in post:
|
||||
db_post.content = post["content"]
|
||||
db.commit()
|
||||
db.refresh(db_post)
|
||||
return {"message":"Post updated successfully","post":db_post}
|
||||
|
||||
@router.delete("/{id}")
|
||||
def delete_post(id, db: Session = Depends(db.get_db)):
|
||||
db_post = db.query(Post).filter(Post.id == id).first()
|
||||
db.delete(db_post)
|
||||
db.commit()
|
||||
return {"message":"Post deleted successfully","post":db_post}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user