Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Remove timestamp as a field of the MessageModel class
Browse files Browse the repository at this point in the history
References: #17
  • Loading branch information
SuyashD95 committed Feb 11, 2021
1 parent 6bbadd8 commit a0bc37c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
16 changes: 6 additions & 10 deletions API/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class UserModel(db.Model):
"""Model class defined for the User table."""

# Name of the table created in the database
__tablename__ = 'users'

_id = db.Column(db.Integer, primary_key=True)
Expand All @@ -36,6 +37,7 @@ def __repr__(self):
class RoomModel(db.Model):
"""Model class defined for the Room table."""

# Name of the table created in the database
__tablename__ = 'rooms'

_id = db.Column(db.Integer, primary_key=True)
Expand All @@ -54,11 +56,11 @@ def __repr__(self):
class MessageModel(db.Model):
"""Model class defined for the Message table."""

__meetingrooms__ = 'messages'
# Name of the table created in the database
__tablename__ = 'messages'

_id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False)
sender_id = db.Column(db.Integer,
db.ForeignKey('users._id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False
Expand All @@ -70,7 +72,7 @@ class MessageModel(db.Model):

def __repr__(self):
"""Object representation for a record of a Message."""
return f'Message(body={self.text}, timestamp={self.timestamp}, sender={self.user.name}, room={self.room.name}'
return f'Message(body={self.body}, sender={self.user.name}, room={self.room.name}'
# ---------------------


Expand Down Expand Up @@ -101,9 +103,6 @@ def __repr__(self):
message_post_reqparser.add_argument('body', type=str,
help='Required. Body of the message. Cannot be empty.', required=True
)
message_post_reqparser.add_argument('timestamp', type=str,
help='Required. Timestamp for the message expressed as a string.', required=True
)
message_post_reqparser.add_argument('sender_name', type=str,
help='Required. Name of the sender.', required=True
)
Expand Down Expand Up @@ -136,7 +135,6 @@ def __repr__(self):
message_fields = {
'_id': fields.Integer,
'body': fields.String,
'timestamp': fields.DateTime,
'sender_id': fields.Integer,
'room_id': fields.Integer
}
Expand Down Expand Up @@ -385,7 +383,7 @@ def get(self, room_name):
if not room:
abort(404, error_code=404, error_msg='No room with the given name exists in the database.')

results = db.session.query(MessageModel).filter_by(room_id=room).all()
results = db.session.query(MessageModel).filter_by(room_id=room._id).all()

if not results:
abort(404, error_code=404, error_msg='No messages exist in the given room.')
Expand All @@ -394,7 +392,6 @@ def get(self, room_name):
for record in results:
messages[record._id] = {
'body': record.body,
'timestamp': record.timestamp,
'sender_name': record.user.name,
'room_name': record.room.name
}
Expand Down Expand Up @@ -435,7 +432,6 @@ def post(self):
)

new_message = RoomModel(body=new_message_args['body'],
timestamp=datetime.strptime(new_message_args['timestamp']),
sender_id=sender, room_id=room
)
db.session.add(new_message)
Expand Down
6 changes: 1 addition & 5 deletions API/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,29 @@ def load_database():
messages_data = [
{
'body': 'Text for message 1 sent by User 2 in Room 1.',
'timestamp': datetime.now(),
'sender': sender_2,
'room': room_1
},
{
'body': 'Text for message 2 sent by User 1 in Room 1',
'timestamp': datetime.now(),
'sender': room_admin,
'room': room_1
},
{
'body': 'Text for message 3 sent by User 3 in Room 2',
'timestamp': datetime.now(),
'sender': sender_3,
'room': room_2
},
{
'body': 'Text for message 3 sent by User 2 in Room 2',
'timestamp': datetime.now(),
'sender': sender_2,
'room': room_2
}
]

print('Inserting mock data to the message table in the database...')
for message in messages_data:
new_message = Message(body=message['body'], timestamp=message['timestamp'], room_id=message['room']._id)
new_message = Message(body=message['body'], room_id=message['room']._id)
message['sender'].sends.append(new_message)
db.session.commit()
print(f'Added new message, "{new_message.body}", sent by {new_message.user.name} on {new_message.room.name}')
Expand Down

0 comments on commit a0bc37c

Please sign in to comment.