From 1836bae25724da1f6fcd58ebd98b3f4bf01cf27e Mon Sep 17 00:00:00 2001 From: PigeonsHouse Date: Mon, 14 Mar 2022 13:48:49 +0900 Subject: [PATCH] =?UTF-8?q?[add]=20DB=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/models.py | 27 +++++++++++++++++++++++++-- routers/bot.py | 0 schemas/api.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) delete mode 100644 routers/bot.py create mode 100644 schemas/api.py diff --git a/db/models.py b/db/models.py index 042e971..9a867da 100644 --- a/db/models.py +++ b/db/models.py @@ -1,6 +1,5 @@ -import datetime from typing import Any -from sqlalchemy import Boolean, Column, DateTime, func +from sqlalchemy import Boolean, Column, Date, DateTime, func from sqlalchemy.ext.declarative import as_declarative from sqlalchemy.sql.schema import ForeignKey from sqlalchemy.sql.sqltypes import String, Integer @@ -14,3 +13,27 @@ class Base: def generate_uuid(): return str(uuid4()) + +class Read(Base): + __tablename__ = 'reads' + user_id = Column(String, ForeignKey('users.user_id'), primary_key=True) + isbn = Column(String, ForeignKey('books.isbn'), primary_key=True) + created_at = Column(DateTime, default=func.now()) + +class User(Base): + __tablename__ = 'users' + user_id = Column(String, primary_key=True) + name = Column(String) + + books = relationship('Book', secondary=Read.__tablename__, backref='Book') + read = relationship('Read') + +class Book(Base): + __tablename__ = 'books' + isbn = Column(String, primary_key=True) + title = Column(String, nullable=True) + author = Column(String, nullable=True) + thumbnail_url = Column(String, nullable=True) + published_date = Column(Date, nullable=True) + + users = relationship('User', secondary=Read.__tablename__) diff --git a/routers/bot.py b/routers/bot.py deleted file mode 100644 index e69de29..0000000 diff --git a/schemas/api.py b/schemas/api.py new file mode 100644 index 0000000..cc10bcb --- /dev/null +++ b/schemas/api.py @@ -0,0 +1,45 @@ +from datetime import datetime +from typing import List, Optional +from pydantic import BaseModel + +from db import models + +class Book(BaseModel): + isbn: str + title: Optional[str] + author: Optional[str] + thumbnail_url: Optional[str] + published_date: Optional[datetime] + created_at: datetime = None + + class Config: + orm_mode = True + +class Read(BaseModel): + user_id: str + isbn: str + created_at: datetime + + class Config: + orm_mode = True + +class User(BaseModel): + user_id: str + name: str + books: List[Book] + read: List[Read] + + def from_orm_custom(orm: models.User): + user = User.from_orm(orm) + for i, read in enumerate(user.read): + user.books[i].created_at = read.created_at + del user.read + return user + + class Config: + orm_mode = True + +class MockUser(BaseModel): + user_id: str + name: str + books: List[Book]