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

Commit

Permalink
Resolve bugs stopping creation of a new Message
Browse files Browse the repository at this point in the history
References: #17
  • Loading branch information
SuyashD95 committed Feb 11, 2021
1 parent a6576e1 commit 57e92c2
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions API/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MessageModel(db.Model):
body = db.Column(db.Text, nullable=False)
sender_id = db.Column(db.Integer,
db.ForeignKey('users._id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False
nullable=False
)
room_id = db.Column(db.Integer,
db.ForeignKey('rooms._id', onupdate='CASCADE', ondelete='CASCADE'),
Expand Down Expand Up @@ -415,13 +415,13 @@ def post(self):
new_message_args = message_post_reqparser.parse_args(strict=True)

sender = db.session.query(UserModel).filter_by(name=new_message_args['sender_name']).first()
if sender:
if not sender:
abort(409, error_code=409,
error_msg='Cannot create a new message because the given sender doesn\'t exist in the database.'
)

room = db.session.query(RoomModel).filter_by(name=new_message_args['room_name']).first()
if room:
if not room:
abort(409, error_code=409,
error_msg='Cannot create a new message because the given room doesn\'t exist in the database.'
)
Expand All @@ -431,10 +431,8 @@ def post(self):
error_msg='Cannot create a message with an empty body.'
)

new_message = RoomModel(body=new_message_args['body'],
sender_id=sender, room_id=room
)
db.session.add(new_message)
new_message = MessageModel(body=new_message_args['body'], room_id=room._id)
sender.sends.append(new_message)
db.session.commit()

return new_message, 201
Expand Down

0 comments on commit 57e92c2

Please sign in to comment.