Skip to content

Commit

Permalink
[add] DBテーブル定義の追加
Browse files Browse the repository at this point in the history
  • Loading branch information
PigeonsHouse committed Mar 14, 2022
1 parent b4b4b4b commit 1836bae
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
27 changes: 25 additions & 2 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__)
Empty file removed routers/bot.py
Empty file.
45 changes: 45 additions & 0 deletions schemas/api.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 1836bae

Please sign in to comment.