Skip to content

Commit dc26cd5

Browse files
committed
Added UserModel.delete_from_db(), modified get user by id response format.
1 parent b14d593 commit dc26cd5

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

section11/models/user.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ def json(self):
1818
'username': self.username
1919
}
2020

21-
def save_to_db(self):
22-
db.session.add(self)
23-
db.session.commit()
24-
2521
@classmethod
2622
def find_by_username(cls, username):
2723
return cls.query.filter_by(username=username).first()
2824

2925
@classmethod
3026
def find_by_id(cls, _id):
3127
return cls.query.filter_by(id=_id).first()
28+
29+
def save_to_db(self):
30+
db.session.add(self)
31+
db.session.commit()
32+
33+
def delete_from_db(self):
34+
db.session.delete(self)
35+
db.session.commit()

section11/resources/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get(cls, user_id: int):
7272
user = UserModel.find_by_id(user_id)
7373
if not user:
7474
return {'message': 'User Not Found'}, 404
75-
return {'user': user.json()}, 200
75+
return user.json(), 200
7676

7777
@classmethod
7878
def delete(cls, user_id: int):

0 commit comments

Comments
 (0)