Add Patch and Delete
This commit is contained in:
parent
40a9963eee
commit
32209620e1
72
app.py
72
app.py
@ -85,7 +85,7 @@ comments_schema = CommentSchema(many=True)
|
|||||||
class Signup(Resource):
|
class Signup(Resource):
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
return {'hello': 'world'}
|
return {'message': 'ok'}
|
||||||
|
|
||||||
|
|
||||||
# Login
|
# Login
|
||||||
@ -93,7 +93,7 @@ class Signup(Resource):
|
|||||||
class Login(Resource):
|
class Login(Resource):
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
return {'hello': 'world'}
|
return {'message': 'ok'}
|
||||||
|
|
||||||
|
|
||||||
# Logout
|
# Logout
|
||||||
@ -101,7 +101,7 @@ class Login(Resource):
|
|||||||
class Logout(Resource):
|
class Logout(Resource):
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
return {'hello': 'world'}
|
return {'message': 'ok'}
|
||||||
|
|
||||||
|
|
||||||
# User
|
# User
|
||||||
@ -117,8 +117,11 @@ class UserList(Resource):
|
|||||||
class UserSingle(Resource):
|
class UserSingle(Resource):
|
||||||
|
|
||||||
def get(self, id):
|
def get(self, id):
|
||||||
all_users = User.query.get(id)
|
my_user = User.query.get(id)
|
||||||
return user_schema.jsonify(all_users)
|
if my_user:
|
||||||
|
return user_schema.jsonify(my_user)
|
||||||
|
else:
|
||||||
|
return {'message': 'No existe el usuario'}, 400
|
||||||
|
|
||||||
|
|
||||||
# Notice
|
# Notice
|
||||||
@ -130,7 +133,25 @@ class NoticeList(Resource):
|
|||||||
return news_schema.jsonify(my_news)
|
return news_schema.jsonify(my_news)
|
||||||
|
|
||||||
def post(self):
|
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>')
|
@api.route(PRE_URL + 'notice/<int:id>')
|
||||||
@ -140,13 +161,45 @@ class NewsSingle(Resource):
|
|||||||
my_notice = Notice.query.get(id)
|
my_notice = Notice.query.get(id)
|
||||||
return notice_schema.jsonify(my_notice)
|
return notice_schema.jsonify(my_notice)
|
||||||
|
|
||||||
def path(self, id):
|
def patch(self, id):
|
||||||
my_notice = Notice.query.get(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):
|
def delete(self, id):
|
||||||
my_notice = Notice.query.get(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
|
# Comment
|
||||||
@ -166,4 +219,3 @@ class Comments(Resource):
|
|||||||
# =========================
|
# =========================
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True if os.environ.get('DEBUG') == 'True' else False)
|
app.run(debug=True if os.environ.get('DEBUG') == 'True' else False)
|
||||||
|
|
||||||
|
@ -25,10 +25,10 @@ print('Users created')
|
|||||||
# Make 1000 fake news
|
# Make 1000 fake news
|
||||||
for num in range(1000):
|
for num in range(1000):
|
||||||
title = fake.sentence()
|
title = fake.sentence()
|
||||||
link = fake.uri()
|
url = fake.uri()
|
||||||
user_id = randint(1, 100)
|
user_id = randint(1, 100)
|
||||||
# Save in database
|
# 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)
|
db.session.add(my_notice)
|
||||||
|
|
||||||
print('News created')
|
print('News created')
|
||||||
|
@ -50,7 +50,7 @@ class Notice(db.Model):
|
|||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
title = db.Column(db.String(100))
|
title = db.Column(db.String(100))
|
||||||
link = db.Column(db.String(500))
|
url = db.Column(db.String(500))
|
||||||
user_id = db.Column(
|
user_id = db.Column(
|
||||||
db.Integer, db.ForeignKey('users.id'), nullable=False)
|
db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
@ -58,7 +58,7 @@ class Notice(db.Model):
|
|||||||
|
|
||||||
# Relations
|
# Relations
|
||||||
user = db.relationship(
|
user = db.relationship(
|
||||||
'User', backref=db.backref('Notice', lazy=True))
|
'User', backref=db.backref('Notice', lazy=True, cascade="all, delete-orphan"))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Notice Table {0}>'.format(self.title)
|
return '<Notice Table {0}>'.format(self.title)
|
||||||
@ -82,9 +82,9 @@ class Comment(db.Model):
|
|||||||
|
|
||||||
# Relations
|
# Relations
|
||||||
user = db.relationship(
|
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 = db.relationship(
|
||||||
'Notice', backref=db.backref('Comment', lazy=True))
|
'Notice', backref=db.backref('Comment', lazy=True, cascade="all, delete-orphan"))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Comment Table {0}>'.format(self.id)
|
return '<Comment Table {0}>'.format(self.id)
|
||||||
|
Loading…
Reference in New Issue
Block a user