Spaces:
Sleeping
Sleeping
bachephysicdun
commited on
Commit
·
9c7476c
1
Parent(s):
78c941e
completed the data modeling part
Browse files- app/crud.py +10 -2
- app/models.py +6 -6
- app/schemas.py +11 -4
- app/test.db +0 -0
app/crud.py
CHANGED
@@ -17,7 +17,15 @@ def add_message(db: Session, message: schemas.MessageBase, username: str):
|
|
17 |
# - create a models.Message instance
|
18 |
# - pass the retrieved user to the message instance
|
19 |
# - save the message instance to the database
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def get_user_chat_history(db: Session, username: str):
|
23 |
-
|
|
|
|
17 |
# - create a models.Message instance
|
18 |
# - pass the retrieved user to the message instance
|
19 |
# - save the message instance to the database
|
20 |
+
user = get_or_create_user(db, username)
|
21 |
+
msg = models.Message(**message.dict())
|
22 |
+
msg.user = user
|
23 |
+
db.add(msg)
|
24 |
+
db.commit()
|
25 |
+
db.refresh(msg)
|
26 |
+
return msg
|
27 |
+
|
28 |
|
29 |
def get_user_chat_history(db: Session, username: str):
|
30 |
+
user = db.query(models.User).filter(models.User.username == username).first()
|
31 |
+
return user.messages if user is not None else []
|
app/models.py
CHANGED
@@ -10,7 +10,7 @@ class User(Base):
|
|
10 |
# This defines the name of the table in the database.
|
11 |
# Here, the class User is mapped to a table called users.
|
12 |
__tablename__ = "users"
|
13 |
-
__table_args__ = {'extend_existing': True}
|
14 |
|
15 |
# This line defines a column called id in the users table.
|
16 |
# Integer: The data type of this column is an integer.
|
@@ -44,12 +44,12 @@ class User(Base):
|
|
44 |
|
45 |
class Message(Base):
|
46 |
__tablename__ = "messages"
|
47 |
-
__table_args__ = {'extend_existing': True}
|
48 |
|
49 |
-
id =
|
|
|
50 |
message = Column(String, nullable=False)
|
51 |
-
type = Column(String
|
52 |
timestamp = Column(DateTime, default=datetime.utcnow, nullable=False)
|
53 |
-
|
54 |
-
|
55 |
user = relationship("User", back_populates="messages")
|
|
|
10 |
# This defines the name of the table in the database.
|
11 |
# Here, the class User is mapped to a table called users.
|
12 |
__tablename__ = "users"
|
13 |
+
# __table_args__ = {'extend_existing': True}
|
14 |
|
15 |
# This line defines a column called id in the users table.
|
16 |
# Integer: The data type of this column is an integer.
|
|
|
44 |
|
45 |
class Message(Base):
|
46 |
__tablename__ = "messages"
|
47 |
+
# __table_args__ = {'extend_existing': True}
|
48 |
|
49 |
+
id = Column(Integer, primary_key=True, index=True)
|
50 |
+
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
51 |
message = Column(String, nullable=False)
|
52 |
+
type = Column(String, nullable=False)
|
53 |
timestamp = Column(DateTime, default=datetime.utcnow, nullable=False)
|
54 |
+
|
|
|
55 |
user = relationship("User", back_populates="messages")
|
app/schemas.py
CHANGED
@@ -1,18 +1,25 @@
|
|
1 |
from pydantic import BaseModel
|
|
|
2 |
|
3 |
class UserQuestion(BaseModel):
|
4 |
question: str
|
5 |
|
6 |
-
#
|
7 |
class HistoryInput(BaseModel):
|
8 |
-
|
|
|
9 |
|
10 |
-
#
|
11 |
# This will be used to parse the input request.
|
12 |
class UserRequest(BaseModel):
|
13 |
username: str
|
|
|
14 |
|
15 |
# TODO: implement MessageBase as a schema mapping from the database model to the
|
16 |
# FastAPI data model. Basically MessageBase should have the same attributes as models.Message
|
17 |
class MessageBase(BaseModel):
|
18 |
-
|
|
|
|
|
|
|
|
|
|
1 |
from pydantic import BaseModel
|
2 |
+
from datetime import datetime
|
3 |
|
4 |
class UserQuestion(BaseModel):
|
5 |
question: str
|
6 |
|
7 |
+
# create a HistoryInput data model with a chat_history and question attributes.
|
8 |
class HistoryInput(BaseModel):
|
9 |
+
chat_history: str
|
10 |
+
question: str
|
11 |
|
12 |
+
# let's create a UserRequest data model with a question and username attribute.
|
13 |
# This will be used to parse the input request.
|
14 |
class UserRequest(BaseModel):
|
15 |
username: str
|
16 |
+
question: str
|
17 |
|
18 |
# TODO: implement MessageBase as a schema mapping from the database model to the
|
19 |
# FastAPI data model. Basically MessageBase should have the same attributes as models.Message
|
20 |
class MessageBase(BaseModel):
|
21 |
+
id: int
|
22 |
+
user_id: int
|
23 |
+
message: str
|
24 |
+
type: str
|
25 |
+
timestamp: datetime
|
app/test.db
DELETED
Binary file (24.6 kB)
|
|