Add Patch and Delete

This commit is contained in:
Andros Fenollosa 2018-02-06 21:29:09 +01:00
parent 40a9963eee
commit 32209620e1
3 changed files with 68 additions and 16 deletions

72
app.py
View File

@ -85,7 +85,7 @@ comments_schema = CommentSchema(many=True)
class Signup(Resource):
def post(self):
return {'hello': 'world'}
return {'message': 'ok'}
# Login
@ -93,7 +93,7 @@ class Signup(Resource):
class Login(Resource):
def post(self):
return {'hello': 'world'}
return {'message': 'ok'}
# Logout
@ -101,7 +101,7 @@ class Login(Resource):
class Logout(Resource):
def get(self):
return {'hello': 'world'}
return {'message': 'ok'}
# User
@ -117,8 +117,11 @@ class UserList(Resource):
class UserSingle(Resource):
def get(self, id):
all_users = User.query.get(id)
return user_schema.jsonify(all_users)
my_user = User.query.get(id)
if my_user:
return user_schema.jsonify(my_user)
else:
return {'message': 'No existe el usuario'}, 400
# Notice
@ -130,7 +133,25 @@ class NoticeList(Resource):
return news_schema.jsonify(my_news)
def post(self):
return request.form
json_data = request.get_json()
if not json_data:
return {'message': 'Datos inválidos'}, 400
# Validations
try:
title = json_data['title']
url = json_data['url']
user_id = json_data['user_id']
except Exception as e:
return {'message': 'No existen los campos necesarios'}, 400
# Save data
my_notice = Notice(title=title, url=url, user_id=user_id)
db.session.add(my_notice)
try:
db.session.commit()
except Exception as e:
db.session.rollback()
return {'message': 'No se ha podido guardar la información'}, 500
return {'message': 'ok'}, 200
@api.route(PRE_URL + 'notice/<int:id>')
@ -140,13 +161,45 @@ class NewsSingle(Resource):
my_notice = Notice.query.get(id)
return notice_schema.jsonify(my_notice)
def path(self, id):
def patch(self, id):
my_notice = Notice.query.get(id)
return notice_schema.jsonify(my_notice)
if not my_notice:
return {'message': 'No existe la noticia'}, 400
json_data = request.get_json()
if not json_data:
return {'message': 'Datos inválidos'}, 400
# Validations
try:
title = json_data['title']
url = json_data['url']
user_id = json_data['user_id']
except Exception as e:
return {'message': 'No existen los campos necesarios'}, 400
# Update data
my_notice.title = title
my_notice.url = url
my_notice.user_id = user_id
db.session.add(my_notice)
try:
db.session.commit()
except Exception as e:
db.session.rollback()
return {'message': 'No se ha podido guardar la información'}, 500
return {'message': 'ok'}, 200
def delete(self, id):
my_notice = Notice.query.get(id)
return notice_schema.jsonify(my_notice)
if my_notice:
db.session.delete(my_notice)
db.session.commit()
try:
db.session.commit()
except Exception as e:
db.session.rollback()
return {'message': 'No se ha podido guardar la información'}, 500
else:
return {'message': 'No existe la noticia'}, 400
return {'message': 'ok'}, 200
# Comment
@ -166,4 +219,3 @@ class Comments(Resource):
# =========================
if __name__ == '__main__':
app.run(debug=True if os.environ.get('DEBUG') == 'True' else False)

View File

@ -25,10 +25,10 @@ print('Users created')
# Make 1000 fake news
for num in range(1000):
title = fake.sentence()
link = fake.uri()
url = fake.uri()
user_id = randint(1, 100)
# Save in database
my_notice = Notice(title=title, link=link, user_id=user_id)
my_notice = Notice(title=title, url=url, user_id=user_id)
db.session.add(my_notice)
print('News created')

View File

@ -50,7 +50,7 @@ class Notice(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
link = db.Column(db.String(500))
url = db.Column(db.String(500))
user_id = db.Column(
db.Integer, db.ForeignKey('users.id'), nullable=False)
created_at = db.Column(
@ -58,7 +58,7 @@ class Notice(db.Model):
# Relations
user = db.relationship(
'User', backref=db.backref('Notice', lazy=True))
'User', backref=db.backref('Notice', lazy=True, cascade="all, delete-orphan"))
def __repr__(self):
return '<Notice Table {0}>'.format(self.title)
@ -82,9 +82,9 @@ class Comment(db.Model):
# Relations
user = db.relationship(
'User', backref=db.backref('Comment', lazy=True))
'User', backref=db.backref('Comment', lazy=True, cascade="all, delete-orphan"))
notice = db.relationship(
'Notice', backref=db.backref('Comment', lazy=True))
'Notice', backref=db.backref('Comment', lazy=True, cascade="all, delete-orphan"))
def __repr__(self):
return '<Comment Table {0}>'.format(self.id)