From 01c6d03ced9888d38464650ff7035a31983eb602 Mon Sep 17 00:00:00 2001 From: Devin Taylor Date: Fri, 23 Dec 2022 13:56:04 -0500 Subject: [PATCH 01/17] set up database and models --- 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 ++++++++++ 7 files changed, 178 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 diff --git a/app/__init__.py b/app/__init__.py index 1c821436..6e6a74f9 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -19,6 +19,8 @@ def create_app(): # Import models here for Alembic setup # from app.models.ExampleModel import ExampleModel + from app.models.board import Board + from app.models.card import Card db.init_app(app) migrate.init_app(app, db) diff --git a/app/models/board.py b/app/models/board.py index 147eb748..69ca47bb 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -1 +1,6 @@ from app import db + +class Board(bd.Model): + board_id = db.Column(db.Integer, primary_key=True, autoincrement=True) + 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..4063dd86 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) + message = db.Column(db.String) + likes_count = db.Column(db.Integer) \ No newline at end of file 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"} From 70ba2a659a199004c8a8eb7cec2926e4e6ed61fd Mon Sep 17 00:00:00 2001 From: Devin Taylor Date: Fri, 23 Dec 2022 13:57:54 -0500 Subject: [PATCH 02/17] typo --- app/models/board.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/board.py b/app/models/board.py index 69ca47bb..c7c454be 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -1,6 +1,6 @@ from app import db -class Board(bd.Model): +class Board(db.Model): board_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) owner = db.Column(db.String) \ No newline at end of file From 1f8327ce9f9abcf57f8a50602d62db7404455903 Mon Sep 17 00:00:00 2001 From: Devin Taylor Date: Fri, 23 Dec 2022 14:10:47 -0500 Subject: [PATCH 03/17] mirations folder --- ...6381fbbe5903_adds_board_and_card_models.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 migrations/versions/6381fbbe5903_adds_board_and_card_models.py diff --git a/migrations/versions/6381fbbe5903_adds_board_and_card_models.py b/migrations/versions/6381fbbe5903_adds_board_and_card_models.py new file mode 100644 index 00000000..128cc361 --- /dev/null +++ b/migrations/versions/6381fbbe5903_adds_board_and_card_models.py @@ -0,0 +1,40 @@ +"""adds Board and Card models + +Revision ID: 6381fbbe5903 +Revises: +Create Date: 2022-12-23 13:58:09.491135 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6381fbbe5903' +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 218812a01dd946388dea9af9b0ce0b9de0b17b3c Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Wed, 4 Jan 2023 11:36:10 -0800 Subject: [PATCH 04/17] created routes folder and route files for board cards and helper functions. Created a working post method --- app/__init__.py | 4 ++++ app/routes/__init__.py | 0 app/routes/boards_routes.py | 6 +++++ app/routes/cards_routes.py | 44 ++++++++++++++++++++++++++++++++++ app/routes/helper_functions.py | 0 5 files changed, 54 insertions(+) create mode 100644 app/routes/__init__.py create mode 100644 app/routes/boards_routes.py create mode 100644 app/routes/cards_routes.py create mode 100644 app/routes/helper_functions.py diff --git a/app/__init__.py b/app/__init__.py index 6e6a74f9..d34b9a0c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -26,8 +26,12 @@ def create_app(): migrate.init_app(app, db) # Register Blueprints here + from .routes.cards_routes import card_bp + app.register_blueprint(card_bp) # from .routes import example_bp # app.register_blueprint(example_bp) + from .routes.boards_routes import board_bp + app.register_blueprint(board_bp) CORS(app) return app diff --git a/app/routes/__init__.py b/app/routes/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/routes/boards_routes.py b/app/routes/boards_routes.py new file mode 100644 index 00000000..d6ea8962 --- /dev/null +++ b/app/routes/boards_routes.py @@ -0,0 +1,6 @@ +from flask import Blueprint, jsonify, request +from app import db +from app.models.board import Board +import os + +board_bp = Blueprint("board_bp", __name__, url_prefix="/boards") \ No newline at end of file diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py new file mode 100644 index 00000000..60b029e3 --- /dev/null +++ b/app/routes/cards_routes.py @@ -0,0 +1,44 @@ +from flask import Blueprint, jsonify, request, make_response +from app import db +from app.models.card import Card +import os + +card_bp = Blueprint("card_bp", __name__, url_prefix="/cards") + +#------------------------POST------------------------------ + +@card_bp.route("", methods=["POST"]) +def add_card(): + request_body = request.get_json() + + if "message" not in request_body: + return jsonify({"details": "Must have a message"}), 400 + + new_card = Card( + message = request_body["message"] + ) + + db.session.add(new_card) + db.session.commit() + + print(new_card) + return make_response(f"Card {new_card.message} succesfully created"), 201 + +#----------------------GET-------------------------------- + +@card_bp.route("", methods=["GET"]) +def get_all_cards(): + + cards = Card.query.all() + + response = [] + + for card in cards: + response.append(card) + return make_response({response}), 200 + +@card_bp.route("/", methods=["GET"]) +def get_one_card(card_id): + card = Card.query.get(card_id) + + return jsonify(card), 200 diff --git a/app/routes/helper_functions.py b/app/routes/helper_functions.py new file mode 100644 index 00000000..e69de29b From b7b41941d650bad65802573e86a5db213824ce01 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Wed, 4 Jan 2023 11:49:15 -0800 Subject: [PATCH 05/17] Completed functional post and get routes for cards --- app/models/card.py | 12 +++++++++++- app/routes/cards_routes.py | 8 ++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/models/card.py b/app/models/card.py index 4063dd86..69dbd214 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -3,4 +3,14 @@ class Card(db.Model): card_id = db.Column(db.Integer, primary_key=True, autoincrement=True) message = db.Column(db.String) - likes_count = db.Column(db.Integer) \ No newline at end of file + likes_count = db.Column(db.Integer) + + def create_dict(self): + + new_dict = { + "card_id": self.card_id, + "message": self.message, + "likes_count": self.likes_count + } + + return new_dict \ No newline at end of file diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index 60b029e3..a2a856a4 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -22,7 +22,7 @@ def add_card(): db.session.commit() print(new_card) - return make_response(f"Card {new_card.message} succesfully created"), 201 + return jsonify({"message": new_card.create_dict()}), 201 #----------------------GET-------------------------------- @@ -34,11 +34,11 @@ def get_all_cards(): response = [] for card in cards: - response.append(card) - return make_response({response}), 200 + response.append(card.create_dict()) + return jsonify(response), 200 @card_bp.route("/", methods=["GET"]) def get_one_card(card_id): card = Card.query.get(card_id) - return jsonify(card), 200 + return jsonify({"card": card.create_dict()}), 200 From 7bc3cd2ed6440a8ba06bbaa90e766fa758b7284f Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Wed, 4 Jan 2023 12:18:55 -0800 Subject: [PATCH 06/17] created a functional delete route for card --- app/routes/cards_routes.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index a2a856a4..ea920a3d 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -42,3 +42,14 @@ def get_one_card(card_id): card = Card.query.get(card_id) return jsonify({"card": card.create_dict()}), 200 + +#---------------------DELETE------------------------------- + +@card_bp.route("/", methods=["DELETE"]) +def delete_one_card(card_id): + card = Card.query.get(card_id) + + db.session.delete(card) + db.session.commit() + + return jsonify({"details": f"Card id: {card_id} was deleted"}), 200 \ No newline at end of file From f53af42a3a87c773269d61ff3b815a2def20a202 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Thu, 5 Jan 2023 10:31:08 -0800 Subject: [PATCH 07/17] created working helper function and updated delete and get route to use a get one or abort status codes 400 and 404 --- app/routes/cards_routes.py | 5 +++-- app/routes/helper_functions.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index ea920a3d..9d8ac394 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -1,6 +1,7 @@ from flask import Blueprint, jsonify, request, make_response from app import db from app.models.card import Card +from .helper_functions import get_one_obj_or_abort import os card_bp = Blueprint("card_bp", __name__, url_prefix="/cards") @@ -39,7 +40,7 @@ def get_all_cards(): @card_bp.route("/", methods=["GET"]) def get_one_card(card_id): - card = Card.query.get(card_id) + card = get_one_obj_or_abort(Card, card_id) return jsonify({"card": card.create_dict()}), 200 @@ -47,7 +48,7 @@ def get_one_card(card_id): @card_bp.route("/", methods=["DELETE"]) def delete_one_card(card_id): - card = Card.query.get(card_id) + card = get_one_obj_or_abort(Card, card_id) db.session.delete(card) db.session.commit() diff --git a/app/routes/helper_functions.py b/app/routes/helper_functions.py index e69de29b..d998ac48 100644 --- a/app/routes/helper_functions.py +++ b/app/routes/helper_functions.py @@ -0,0 +1,16 @@ +from flask import jsonify, abort, make_response + +def get_one_obj_or_abort(cls, id): + try: + id = int(id) + except ValueError: + response = f"ID must be an integer. '{id}' not valid" + abort(make_response(jsonify({"msg": response}), 400)) + + obj = cls.query.get(id) + + if not obj: + response = f"{cls.__name__} with ID {id} not found" + abort(make_response(jsonify({"msg": response}), 404)) + + return obj \ No newline at end of file From e58f44477c9499e5933ed618f6b03ca3cb75f8e0 Mon Sep 17 00:00:00 2001 From: Devin Taylor Date: Thu, 5 Jan 2023 11:14:31 -0800 Subject: [PATCH 08/17] set likes default to 0 and added a patch route for +1 feature --- app/models/card.py | 2 +- app/routes/cards_routes.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/card.py b/app/models/card.py index 69dbd214..947d3ca1 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -3,7 +3,7 @@ class Card(db.Model): card_id = db.Column(db.Integer, primary_key=True, autoincrement=True) message = db.Column(db.String) - likes_count = db.Column(db.Integer) + likes_count = db.Column(db.Integer, default=0) def create_dict(self): diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index 9d8ac394..8fa43b77 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -53,4 +53,12 @@ def delete_one_card(card_id): db.session.delete(card) db.session.commit() - return jsonify({"details": f"Card id: {card_id} was deleted"}), 200 \ No newline at end of file + return jsonify({"details": f"Card id: {card_id} was deleted"}), 200 + +#---------------------PATCH--------------------------------- +@card_bp.route("/", methods=["PATCH"]) +def update_likes(card_id): + card = get_one_obj_or_abort(Card, card_id) + card.likes_count += 1 + + db.session.commit() From ede64c237c847c02d65fe2d972c9fd73fec17fcf Mon Sep 17 00:00:00 2001 From: Devin Taylor Date: Thu, 5 Jan 2023 11:59:05 -0800 Subject: [PATCH 09/17] updated models to connect them and created a post route for boards --- app/models/board.py | 13 +++++++++++- app/models/card.py | 1 + app/routes/boards_routes.py | 22 +++++++++++++++++++- migrations/versions/db9ad65e0ce8_.py | 30 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/db9ad65e0ce8_.py diff --git a/app/models/board.py b/app/models/board.py index c7c454be..6ecf7114 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -3,4 +3,15 @@ class Board(db.Model): board_id = db.Column(db.Integer, primary_key=True, autoincrement=True) 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") + + def create_board_dict(self): + + new_dict = { + "board_id": self.board_id, + "title": self.title, + "owner": self.owner, + } + + return new_dict \ No newline at end of file diff --git a/app/models/card.py b/app/models/card.py index 947d3ca1..8f937904 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -4,6 +4,7 @@ class Card(db.Model): card_id = db.Column(db.Integer, primary_key=True, autoincrement=True) message = db.Column(db.String) likes_count = db.Column(db.Integer, default=0) + board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'), nullable=True) def create_dict(self): diff --git a/app/routes/boards_routes.py b/app/routes/boards_routes.py index d6ea8962..9f35f9f4 100644 --- a/app/routes/boards_routes.py +++ b/app/routes/boards_routes.py @@ -1,6 +1,26 @@ from flask import Blueprint, jsonify, request from app import db from app.models.board import Board +from .helper_functions import get_one_obj_or_abort import os -board_bp = Blueprint("board_bp", __name__, url_prefix="/boards") \ No newline at end of file +board_bp = Blueprint("board_bp", __name__, url_prefix="/boards") + +#----------------------------------POST------------------------------ +@board_bp.route("",methods=["POST"]) +def add_board(): + request_body = request.get_json() + + if "title" not in request_body or "owner" not in request_body: + return jsonify({"details": "invalid data"}), 400 + + new_board = Board( + title = request_body["title"], + owner = request_body["owner"], + ) + db.session.add(new_board) + db.session.commit() + + return jsonify({"board": f"{new_board.title} created"}), 201 + +#--------------------------------GET--------------------------------- \ No newline at end of file diff --git a/migrations/versions/db9ad65e0ce8_.py b/migrations/versions/db9ad65e0ce8_.py new file mode 100644 index 00000000..95e86714 --- /dev/null +++ b/migrations/versions/db9ad65e0ce8_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: db9ad65e0ce8 +Revises: 6381fbbe5903 +Create Date: 2023-01-05 11:48:29.468751 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'db9ad65e0ce8' +down_revision = '6381fbbe5903' +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 54d59d2fc89c57e66e8536cc9dba46335f56b587 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Fri, 6 Jan 2023 10:31:50 -0800 Subject: [PATCH 10/17] created get all route and get one for boards. Fixed the relationship between cards and boards --- app/models/board.py | 2 +- app/routes/boards_routes.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/models/board.py b/app/models/board.py index 6ecf7114..f70211f7 100644 --- a/app/models/board.py +++ b/app/models/board.py @@ -4,7 +4,7 @@ class Board(db.Model): board_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) owner = db.Column(db.String) - cards = db.relationship("Card", back_populates="board") + cards = db.relationship("Card", backref="board", lazy=True) def create_board_dict(self): diff --git a/app/routes/boards_routes.py b/app/routes/boards_routes.py index 9f35f9f4..105f0805 100644 --- a/app/routes/boards_routes.py +++ b/app/routes/boards_routes.py @@ -23,4 +23,20 @@ def add_board(): return jsonify({"board": f"{new_board.title} created"}), 201 -#--------------------------------GET--------------------------------- \ No newline at end of file +#--------------------------------GET--------------------------------- +@board_bp.route("", methods=["GET"]) +def get_all_boards(): + + boards = Board.query.all() + + response = [] + + for board in boards: + response.append(board.create_board_dict()) + return jsonify(response), 200 + +@board_bp.route("/", methods=["GET"]) +def get_one_board(board_id): + board = get_one_obj_or_abort(Board, board_id) + + return jsonify({"board": board.create_board_dict()}), 200 \ No newline at end of file From d63f4cc279cb1ab1b40197650991066e20066a2d Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Fri, 6 Jan 2023 11:10:37 -0800 Subject: [PATCH 11/17] created path to link cards to one board, path to get board with all cards --- app/routes/boards_routes.py | 38 ++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/app/routes/boards_routes.py b/app/routes/boards_routes.py index 105f0805..5620ab5c 100644 --- a/app/routes/boards_routes.py +++ b/app/routes/boards_routes.py @@ -1,6 +1,7 @@ from flask import Blueprint, jsonify, request from app import db from app.models.board import Board +from app.models.card import Card from .helper_functions import get_one_obj_or_abort import os @@ -39,4 +40,39 @@ def get_all_boards(): def get_one_board(board_id): board = get_one_obj_or_abort(Board, board_id) - return jsonify({"board": board.create_board_dict()}), 200 \ No newline at end of file + return jsonify({"board": board.create_board_dict()}), 200 + +#------------------------GET CARDS------------------------------- + +@board_bp.route("//cards", methods=["GET"]) +def get_cards_from_board(board_id): + board = get_one_obj_or_abort(Board, board_id) + + response = [card.create_dict() for card in board.cards] + + board_dict = { + "board_id": board.board_id, + "title": board.title, + "owner": board.owner, + "cards": response + } + + return jsonify(board_dict), 200 + +#------------------------POST CARDS-------------------------------- + +@board_bp.route("//cards", methods=["POST"]) +def post_card_to_board(board_id): + board = get_one_obj_or_abort(Board, board_id) + + request_body = request.get_json() + + for card_id in request_body["card_ids"]: + card = get_one_obj_or_abort(Card, card_id) + + card.board_id = board_id + + db.session.add(card) + db.session.commit() + + return jsonify({"Card with card id": request_body["card_ids"], "Linked to board id": board.board_id}), 200 \ No newline at end of file From 58faa0ed84863880505b22d423b5e7047177f1d4 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Fri, 6 Jan 2023 11:15:57 -0800 Subject: [PATCH 12/17] deleted extra routes folder not in use --- app/routes.py | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 app/routes.py diff --git a/app/routes.py b/app/routes.py deleted file mode 100644 index 480b8c4b..00000000 --- a/app/routes.py +++ /dev/null @@ -1,4 +0,0 @@ -from flask import Blueprint, request, jsonify, make_response -from app import db - -# example_bp = Blueprint('example_bp', __name__) From 5813923d902da60611e28aa234ad682c99f1f407 Mon Sep 17 00:00:00 2001 From: Devin Taylor Date: Tue, 17 Jan 2023 11:47:53 -0800 Subject: [PATCH 13/17] added an if statement to catch if a message is over 40 characters. --- app/models/card.py | 2 +- app/routes/cards_routes.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/card.py b/app/models/card.py index 8f937904..61fa6d4a 100644 --- a/app/models/card.py +++ b/app/models/card.py @@ -2,7 +2,7 @@ class Card(db.Model): card_id = db.Column(db.Integer, primary_key=True, autoincrement=True) - message = db.Column(db.String) + message = db.Column(db.VARCHAR(40)) likes_count = db.Column(db.Integer, default=0) board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'), nullable=True) diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index 8fa43b77..a5b6ab68 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -15,6 +15,9 @@ def add_card(): if "message" not in request_body: return jsonify({"details": "Must have a message"}), 400 + if len(request_body["message"]) > 40: + return jsonify({"details": "Message must be 40 characters or less"}), 400 + new_card = Card( message = request_body["message"] ) From 1282e2efbb52823112c3e82dadb6ae9eb806754f Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Wed, 18 Jan 2023 10:43:59 -0800 Subject: [PATCH 14/17] dried up get route for cards and boards using list comprehension --- app/routes/boards_routes.py | 4 +--- app/routes/cards_routes.py | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/app/routes/boards_routes.py b/app/routes/boards_routes.py index 5620ab5c..50c8efa8 100644 --- a/app/routes/boards_routes.py +++ b/app/routes/boards_routes.py @@ -30,10 +30,8 @@ def get_all_boards(): boards = Board.query.all() - response = [] + response = [board.create_board_dict() for board in boards] - for board in boards: - response.append(board.create_board_dict()) return jsonify(response), 200 @board_bp.route("/", methods=["GET"]) diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index a5b6ab68..30cd6465 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -35,10 +35,10 @@ def get_all_cards(): cards = Card.query.all() - response = [] + response = [card.create_dict() for card in cards] - for card in cards: - response.append(card.create_dict()) + # for card in cards: + # response.append(card.create_dict()) return jsonify(response), 200 @card_bp.route("/", methods=["GET"]) From c33ce84d3cd45337eb5402087854bce2c0945163 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Wed, 18 Jan 2023 10:58:27 -0800 Subject: [PATCH 15/17] Dryed up code in cards get --- app/routes/cards_routes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index 30cd6465..16093177 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -37,8 +37,6 @@ def get_all_cards(): response = [card.create_dict() for card in cards] - # for card in cards: - # response.append(card.create_dict()) return jsonify(response), 200 @card_bp.route("/", methods=["GET"]) From 7151de5c361f3571d43ab9cfaf1f7325b6e201ac Mon Sep 17 00:00:00 2001 From: Maxwell Date: Wed, 18 Jan 2023 14:56:49 -0800 Subject: [PATCH 16/17] added return statement to patch route --- app/routes/cards_routes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/routes/cards_routes.py b/app/routes/cards_routes.py index 16093177..33867b61 100644 --- a/app/routes/cards_routes.py +++ b/app/routes/cards_routes.py @@ -63,3 +63,4 @@ def update_likes(card_id): card.likes_count += 1 db.session.commit() + return jsonify({"liked": card.create_dict()}) \ No newline at end of file From 30a7b74e5ef8d1edb9cb9c57246348438272ba7f Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Thu, 19 Jan 2023 13:32:07 -0800 Subject: [PATCH 17/17] added delete board route to clear test boards --- app/routes/boards_routes.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/routes/boards_routes.py b/app/routes/boards_routes.py index 50c8efa8..37b7bafe 100644 --- a/app/routes/boards_routes.py +++ b/app/routes/boards_routes.py @@ -73,4 +73,15 @@ def post_card_to_board(board_id): db.session.add(card) db.session.commit() - return jsonify({"Card with card id": request_body["card_ids"], "Linked to board id": board.board_id}), 200 \ No newline at end of file + return jsonify({"Card with card id": request_body["card_ids"], "Linked to board id": board.board_id}), 200 + +#---------------------DELETE------------------------------- + +@board_bp.route("/", methods=["DELETE"]) +def delete_one_board(board_id): + board = get_one_obj_or_abort(Board, board_id) + + db.session.delete(board) + db.session.commit() + + return jsonify({"details": f"Board id: {board_id} was deleted"}), 200