From a0b7c6707319b73c69358896f05ace860cb265e0 Mon Sep 17 00:00:00 2001 From: Margaret Date: Thu, 22 Dec 2022 13:53:20 -0800 Subject: [PATCH 01/13] did initial setup up, no blueprints --- app/__init__.py | 2 + app/models/board.py | 5 ++ app/models/card.py | 5 ++ migrations/README | 1 + migrations/alembic.ini | 45 +++++++++++++ migrations/env.py | 96 ++++++++++++++++++++++++++++ migrations/script.py.mako | 24 +++++++ migrations/versions/9c2454f722e1_.py | 40 ++++++++++++ 8 files changed, 218 insertions(+) create mode 100644 migrations/README create mode 100644 migrations/alembic.ini create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/9c2454f722e1_.py diff --git a/app/__init__.py b/app/__init__.py index 1c821436..3036cbd8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -18,6 +18,8 @@ def create_app(): "SQLALCHEMY_DATABASE_URI") # Import models here for Alembic setup + from app.models.board import Board + from app.models.card import Card # from app.models.ExampleModel import ExampleModel db.init_app(app) diff --git a/app/models/board.py b/app/models/board.py index 147eb748..cd3b2396 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -1 +1,6 @@ from app import db + +class Board(db.Model): + board_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) + title = db.Column(db.String) + owner = db.Column(db.String) \ No newline at end of file diff --git a/app/models/card.py b/app/models/card.py index 147eb748..a03952d1 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -1 +1,6 @@ from app import db + +class Card(db.Model): + card_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) + message = db.Column(db.String) + likes_count = db.Column(db.Integer) diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..98e4f9c4 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 00000000..f8ed4801 --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..8b3fb335 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from flask import current_app + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option( + 'sqlalchemy.url', + str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..2c015630 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/9c2454f722e1_.py b/migrations/versions/9c2454f722e1_.py new file mode 100644 index 00000000..38fcdd16 --- /dev/null +++ b/migrations/versions/9c2454f722e1_.py @@ -0,0 +1,40 @@ +"""empty message + +Revision ID: 9c2454f722e1 +Revises: +Create Date: 2022-12-22 13:52:14.051231 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '9c2454f722e1' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('board', + sa.Column('board_id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('title', sa.String(), nullable=True), + sa.Column('owner', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('board_id') + ) + op.create_table('card', + sa.Column('card_id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('message', sa.String(), nullable=True), + sa.Column('likes_count', sa.Integer(), nullable=True), + sa.PrimaryKeyConstraint('card_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('card') + op.drop_table('board') + # ### end Alembic commands ### From 8cfb4399dc48cf9d0cba3111fb5d6638a4fbd6f2 Mon Sep 17 00:00:00 2001 From: Yangweiyi Li Date: Tue, 3 Jan 2023 10:54:22 -0800 Subject: [PATCH 02/13] add relationship of models --- app/models/board.py | 3 ++- app/models/card.py | 2 ++ migrations/versions/6090d125c0eb_.py | 30 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/6090d125c0eb_.py diff --git a/app/models/board.py b/app/models/board.py index cd3b2396..6087eab6 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -3,4 +3,5 @@ class Board(db.Model): board_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) title = db.Column(db.String) - owner = db.Column(db.String) \ No newline at end of file + owner = db.Column(db.String) + cards = db.relationship('Card', back_populates='board') \ No newline at end of file diff --git a/app/models/card.py b/app/models/card.py index a03952d1..0e552a5e 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -4,3 +4,5 @@ class Card(db.Model): card_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) message = db.Column(db.String) likes_count = db.Column(db.Integer) + board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'), default=None, nullable=True) + board=db.relationship('Board', back_populates='cards') diff --git a/migrations/versions/6090d125c0eb_.py b/migrations/versions/6090d125c0eb_.py new file mode 100644 index 00000000..c1306f12 --- /dev/null +++ b/migrations/versions/6090d125c0eb_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: 6090d125c0eb +Revises: 9c2454f722e1 +Create Date: 2023-01-03 10:53:29.190014 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6090d125c0eb' +down_revision = '9c2454f722e1' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('card', sa.Column('board_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'card', 'board', ['board_id'], ['board_id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'card', type_='foreignkey') + op.drop_column('card', 'board_id') + # ### end Alembic commands ### From c88307adb5fe4276a221ac3f91c98a35f722861a Mon Sep 17 00:00:00 2001 From: Yangweiyi Li Date: Tue, 3 Jan 2023 10:58:33 -0800 Subject: [PATCH 03/13] create bp and register it --- app/__init__.py | 2 ++ app/routes.py | 1 + 2 files changed, 3 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 3036cbd8..78f51491 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -28,6 +28,8 @@ def create_app(): # Register Blueprints here # from .routes import example_bp # app.register_blueprint(example_bp) + from .routes import board_bp + app.register_blueprint(board_bp) CORS(app) return app diff --git a/app/routes.py b/app/routes.py index 480b8c4b..accb25ac 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,3 +2,4 @@ from app import db # example_bp = Blueprint('example_bp', __name__) +board_bp = Blueprint('board_bp', __name__, url_prefix='/board') \ No newline at end of file From 259242ee65121eea3d9a881005ab4ba886a12cd6 Mon Sep 17 00:00:00 2001 From: Margaret Date: Tue, 3 Jan 2023 11:06:52 -0800 Subject: [PATCH 04/13] made default likes 0 --- migrations/versions/db8f93f91963_.py | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 migrations/versions/db8f93f91963_.py diff --git a/migrations/versions/db8f93f91963_.py b/migrations/versions/db8f93f91963_.py new file mode 100644 index 00000000..a41efe1f --- /dev/null +++ b/migrations/versions/db8f93f91963_.py @@ -0,0 +1,32 @@ +"""empty message + +Revision ID: db8f93f91963 +Revises: 6090d125c0eb +Create Date: 2023-01-03 11:06:18.488791 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'db8f93f91963' +down_revision = '6090d125c0eb' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('card', 'board_id', + existing_type=sa.INTEGER(), + nullable=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('card', 'board_id', + existing_type=sa.INTEGER(), + nullable=True) + # ### end Alembic commands ### From 173bdccd52da2ac5ede4b680c884c0bd9ddfeaf7 Mon Sep 17 00:00:00 2001 From: Margaret Date: Tue, 3 Jan 2023 11:13:51 -0800 Subject: [PATCH 05/13] created board post method --- app/models/board.py | 6 +++++- app/models/card.py | 8 ++++++-- app/routes.py | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/models/board.py b/app/models/board.py index 6087eab6..7fd26303 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -4,4 +4,8 @@ class Board(db.Model): board_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) title = db.Column(db.String) owner = db.Column(db.String) - cards = db.relationship('Card', back_populates='board') \ No newline at end of file + cards = db.relationship('Card', back_populates='board') + + @classmethod + def create_board(cls,request_body): + return Board(title=request_body['title'], owner=request_body['owner']) \ No newline at end of file diff --git a/app/models/card.py b/app/models/card.py index 0e552a5e..ab5290f8 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -3,6 +3,10 @@ class Card(db.Model): card_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) message = db.Column(db.String) - likes_count = db.Column(db.Integer) - board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'), default=None, nullable=True) + likes_count = db.Column(db.Integer, default=0) + board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'), default=None, nullable=False) board=db.relationship('Board', back_populates='cards') + + @classmethod + def create_card(cls,board_id,request_body): + return Card(message=request_body['message'],board_id=board_id) diff --git a/app/routes.py b/app/routes.py index accb25ac..691cd282 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,5 +1,20 @@ from flask import Blueprint, request, jsonify, make_response +from app.models.board import Board +from app.models.card import Card from app import db # example_bp = Blueprint('example_bp', __name__) -board_bp = Blueprint('board_bp', __name__, url_prefix='/board') \ No newline at end of file +board_bp = Blueprint('board_bp', __name__, url_prefix='/board') + +"""Board Routes""" +@board_bp.route("", methods=['POST']) +def add_one_board(): + response_body = request.get_json() + try: + new_board = Board.create_board(response_body) + except KeyError: + return make_response(jsonify({'warning':'invalid title or owner'}),400) + db.session.add(new_board) + db.session.commit() + + return make_response(jsonify({'msg':f'Board with id {new_board.board_id} created'}),201) \ No newline at end of file From cd3d3ffddfb94f78a7df4829c9f59ec2506e6c65 Mon Sep 17 00:00:00 2001 From: Margaret Date: Tue, 3 Jan 2023 11:19:11 -0800 Subject: [PATCH 06/13] added get route for board --- app/models/board.py | 7 +++++++ app/models/card.py | 6 ++++++ app/routes.py | 10 +++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/board.py b/app/models/board.py index 7fd26303..646bb29c 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -6,6 +6,13 @@ class Board(db.Model): owner = db.Column(db.String) cards = db.relationship('Card', back_populates='board') + def dictionfy(self): + return { + 'id':self.board_id, + 'title':self.title, + 'owner':self.owner + } + @classmethod def create_board(cls,request_body): return Board(title=request_body['title'], owner=request_body['owner']) \ No newline at end of file diff --git a/app/models/card.py b/app/models/card.py index ab5290f8..1dca578c 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -7,6 +7,12 @@ class Card(db.Model): board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'), default=None, nullable=False) board=db.relationship('Board', back_populates='cards') + def dictionfy(self): + return { + 'id':self.card_id, + 'message':self.message + } + @classmethod def create_card(cls,board_id,request_body): return Card(message=request_body['message'],board_id=board_id) diff --git a/app/routes.py b/app/routes.py index 691cd282..9baf8c55 100644 --- a/app/routes.py +++ b/app/routes.py @@ -17,4 +17,12 @@ def add_one_board(): db.session.add(new_board) db.session.commit() - return make_response(jsonify({'msg':f'Board with id {new_board.board_id} created'}),201) \ No newline at end of file + return make_response(jsonify({'msg':f'Board with id {new_board.board_id} created'}),201) + +@board_bp.route("", methods=['GET']) +def get_all_boards(): + boards=Board.query.all() + return_list=[] + for board in boards: + return_list.append(board.dictionfy()) + return make_response(jsonify(return_list),200) \ No newline at end of file From 6dba237bbc90d86f74894ad4d8fc41bd64baa8c7 Mon Sep 17 00:00:00 2001 From: Margaret Date: Tue, 3 Jan 2023 11:38:34 -0800 Subject: [PATCH 07/13] added get 1 board route --- app/models/board.py | 9 +++++++-- app/routes.py | 25 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/models/board.py b/app/models/board.py index 646bb29c..526ddd81 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -1,4 +1,5 @@ from app import db +from app.models.card import Card class Board(db.Model): board_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) @@ -7,11 +8,15 @@ class Board(db.Model): cards = db.relationship('Card', back_populates='board') def dictionfy(self): - return { + board_info = { 'id':self.board_id, 'title':self.title, - 'owner':self.owner + 'owner':self.owner, + 'cards':[] } + for card in self.cards: + board_info['cards'].append(card.dictionfy()) + return board_info @classmethod def create_board(cls,request_body): diff --git a/app/routes.py b/app/routes.py index 9baf8c55..d3ccef95 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint, request, jsonify, make_response +from flask import Blueprint, request, jsonify, make_response,abort from app.models.board import Board from app.models.card import Card from app import db @@ -25,4 +25,25 @@ def get_all_boards(): return_list=[] for board in boards: return_list.append(board.dictionfy()) - return make_response(jsonify(return_list),200) \ No newline at end of file + return make_response(jsonify(return_list),200) + +@board_bp.route('/', methods=['GET']) +def get_one_board_with_cards(board_id): + board = validate_id(Board,board_id) + board_info = board.dictionfy() + return make_response(jsonify(board_info),200) + +"""Card Routes""" + + + + +def validate_id(cls,id): + try: + model_id = int(id) + except TypeError: + abort(make_response({'details':'Invalid, id must be integer'}),400) + model = cls.query.get(model_id) + if not model: + abort(make_response({'details':'ID is invalid'}),404) + return model \ No newline at end of file From 05e957dbe1f1e5860a2edee1d09b1b05333c6262 Mon Sep 17 00:00:00 2001 From: Atefeh Mirzaei Date: Tue, 3 Jan 2023 11:57:02 -0800 Subject: [PATCH 08/13] added card post route --- app/routes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/routes.py b/app/routes.py index d3ccef95..e12387f0 100644 --- a/app/routes.py +++ b/app/routes.py @@ -35,6 +35,22 @@ def get_one_board_with_cards(board_id): """Card Routes""" +@board_bp.route('/', methods=['POST']) +def add_one_card(board_id): + request_body=request.get_json() + try: + new_card=Card.create_card(board_id,request_body) + except KeyError: + return make_response(jsonify({'warning':'invalid message'}),400) + db.session.add(new_card) + db.session.commit() + + return make_response(jsonify({'msg':f'Card with id {new_card.card_id} created'}),201) + +@board_bp.route('//', ) + + + From 3bc95cc42714fa9c5a574d1640089f5c1ddcde26 Mon Sep 17 00:00:00 2001 From: Atefeh Mirzaei Date: Tue, 3 Jan 2023 12:08:37 -0800 Subject: [PATCH 09/13] added card Delete route --- app/routes.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index e12387f0..196a3403 100644 --- a/app/routes.py +++ b/app/routes.py @@ -47,7 +47,15 @@ def add_one_card(board_id): return make_response(jsonify({'msg':f'Card with id {new_card.card_id} created'}),201) -@board_bp.route('//', ) +@board_bp.route('//', methods=['DELETE']) +def delete_one_card(board_id, card_id): + card=validate_id(Card, card_id) + db.session.delete(card) + db.session.commit() + + return make_response(jsonify({'msg':f"card {card_id} deleted from Board with id {board_id}"}),200) + + From 884c72807033857be610f0d9f3bfcd5fa973b6e0 Mon Sep 17 00:00:00 2001 From: Margaret Date: Thu, 5 Jan 2023 11:07:29 -0800 Subject: [PATCH 10/13] changed a route response --- app/models/card.py | 3 ++- app/routes.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/card.py b/app/models/card.py index 1dca578c..1d3af5ba 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -10,7 +10,8 @@ class Card(db.Model): def dictionfy(self): return { 'id':self.card_id, - 'message':self.message + 'message':self.message, + 'likes':self.likes_count } @classmethod diff --git a/app/routes.py b/app/routes.py index 196a3403..968d7af9 100644 --- a/app/routes.py +++ b/app/routes.py @@ -45,7 +45,7 @@ def add_one_card(board_id): db.session.add(new_card) db.session.commit() - return make_response(jsonify({'msg':f'Card with id {new_card.card_id} created'}),201) + return make_response(jsonify({'msg':f'Card created with id {new_card.card_id}'}),201) @board_bp.route('//', methods=['DELETE']) def delete_one_card(board_id, card_id): From 47b16af8e4b06f995d96b5ee56d1527f1944880f Mon Sep 17 00:00:00 2001 From: Margaret Date: Thu, 5 Jan 2023 11:29:08 -0800 Subject: [PATCH 11/13] modified card creation response to produce id --- app/routes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 968d7af9..d2970a2c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -45,7 +45,9 @@ def add_one_card(board_id): db.session.add(new_card) db.session.commit() - return make_response(jsonify({'msg':f'Card created with id {new_card.card_id}'}),201) + return make_response(jsonify({ + 'id':new_card.card_id, + 'msg':f'Card created with id {new_card.card_id}'}),201) @board_bp.route('//', methods=['DELETE']) def delete_one_card(board_id, card_id): From deb260926065a14673c957cc4311af68e2b8f47f Mon Sep 17 00:00:00 2001 From: Margaret Date: Wed, 18 Jan 2023 11:10:09 -0800 Subject: [PATCH 12/13] added upvote route --- app/routes.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index d2970a2c..d271c81f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -49,13 +49,17 @@ def add_one_card(board_id): 'id':new_card.card_id, 'msg':f'Card created with id {new_card.card_id}'}),201) -@board_bp.route('//', methods=['DELETE']) -def delete_one_card(board_id, card_id): +@board_bp.route('//', methods=['PATCH','DELETE']) +def modify_one_card(board_id, card_id): card=validate_id(Card, card_id) - db.session.delete(card) + method_string = "deleted" if request.method == 'DELETE' else 'upvoted' + if request.method=='DELETE': + db.session.delete(card) + else: + card.likes_count+=1 db.session.commit() - return make_response(jsonify({'msg':f"card {card_id} deleted from Board with id {board_id}"}),200) + return make_response(jsonify({'msg':f"card {card_id} {method_string} from Board with id {board_id}"}),200) From c303414e23cc27c93d77be9358ec5528e092c872 Mon Sep 17 00:00:00 2001 From: Yangweiyi Li Date: Fri, 20 Jan 2023 10:09:21 -0800 Subject: [PATCH 13/13] format the code --- app/__init__.py | 1 - app/models/board.py | 3 ++- app/routes.py | 17 +++++++---------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 78f51491..d0fc07e6 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -9,7 +9,6 @@ migrate = Migrate() load_dotenv() - def create_app(): app = Flask(__name__) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False diff --git a/app/models/board.py b/app/models/board.py index 526ddd81..59e3274e 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -20,4 +20,5 @@ def dictionfy(self): @classmethod def create_board(cls,request_body): - return Board(title=request_body['title'], owner=request_body['owner']) \ No newline at end of file + return Board(title=request_body['title'], owner=request_body['owner']) + \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index d271c81f..7c05ab93 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,7 +3,6 @@ from app.models.card import Card from app import db -# example_bp = Blueprint('example_bp', __name__) board_bp = Blueprint('board_bp', __name__, url_prefix='/board') """Board Routes""" @@ -34,14 +33,15 @@ def get_one_board_with_cards(board_id): return make_response(jsonify(board_info),200) """Card Routes""" - @board_bp.route('/', methods=['POST']) def add_one_card(board_id): request_body=request.get_json() + try: new_card=Card.create_card(board_id,request_body) except KeyError: return make_response(jsonify({'warning':'invalid message'}),400) + db.session.add(new_card) db.session.commit() @@ -53,27 +53,24 @@ def add_one_card(board_id): def modify_one_card(board_id, card_id): card=validate_id(Card, card_id) method_string = "deleted" if request.method == 'DELETE' else 'upvoted' + if request.method=='DELETE': db.session.delete(card) else: card.likes_count+=1 + db.session.commit() return make_response(jsonify({'msg':f"card {card_id} {method_string} from Board with id {board_id}"}),200) - - - - - - - +"""Helper Functions""" def validate_id(cls,id): try: model_id = int(id) except TypeError: abort(make_response({'details':'Invalid, id must be integer'}),400) model = cls.query.get(model_id) + if not model: abort(make_response({'details':'ID is invalid'}),404) - return model \ No newline at end of file + return model