-
Notifications
You must be signed in to change notification settings - Fork 32
/
models.py
45 lines (30 loc) · 1.06 KB
/
models.py
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
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.orm import Session
from database import Base
class ToDo(Base):
__tablename__ = "todos"
id = Column(Integer, primary_key=True, index=True)
content = Column(String)
session_key = Column(String)
def create_todo(db: Session, content: str, session_key: str):
todo = ToDo(content=content, session_key=session_key)
db.add(todo)
db.commit()
db.refresh(todo)
return todo
def get_todo(db: Session, item_id: int):
return db.query(ToDo).filter(ToDo.id == item_id).first()
def update_todo(db: Session, item_id: int, content: str):
todo = get_todo(db, item_id)
todo.content = content
db.commit()
db.refresh(todo)
return todo
def get_todos(db: Session, session_key: str, skip: int = 0, limit: int = 100):
return db.query(ToDo).filter(ToDo.session_key == session_key).offset(skip).limit(limit).all()
def delete_todo(db: Session, item_id: int):
todo = get_todo(db, item_id)
db.delete(todo)
db.commit()