-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathposts.py
More file actions
66 lines (51 loc) · 1.95 KB
/
posts.py
File metadata and controls
66 lines (51 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import List
from sqlalchemy.orm import Session
from fastapi import HTTPException
from datetime import datetime
import models, schemas
def get_all_posts(db: Session, skip: int = 0, limit: int = 10) -> List[models.Post]:
records = db.query(models.Post).filter().offset(skip).limit(limit).all()
for record in records:
record.id = str(record.id)
return records
def get_post_by_id(post_id: str, db: Session) -> models.Post:
record = db.query(models.Post).filter(models.Post.id == post_id).first()
if not record:
raise HTTPException(status_code=404, detail="Not Found")
record.id = str(record.id)
return record
def get_posts_by_title(title: str, db: Session) -> List[models.Post]:
records = db.query(models.Post).filter(models.Post.title == title).all()
for record in records:
record.id = str(record.id)
return records
def update_post(post_id: str, db: Session, post: schemas.Post) -> models.Post:
db_post = get_post_by_id(post_id=post_id, db=db)
for var, value in vars(post).items():
setattr(db_post, var, value) if value else None
db_post.updated_at = datetime.now()
db.add(db_post)
db.commit()
db.refresh(db_post)
return db_post
def delete_post(post_id: str, db: Session) -> models.Post:
db_post = get_post_by_id(post_id=post_id, db=db)
db.delete(db_post)
db.commit()
return db_post
def delete_all_posts(db: Session) -> List[models.Post]:
records = db.query(models.Post).filter()
for record in records:
db.delete(record)
db.commit()
return records
def create_post(db: Session, post: schemas.Post) -> models.Post:
record = db.query(models.Post).filter(models.Post.id == post.id).first()
if record:
raise HTTPException(status_code=409, detail="Already exists")
db_post = models.Post(**post.dict())
db.add(db_post)
db.commit()
db.refresh(db_post)
db_post.id = str(db_post.id)
return db_post