Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nathalia/boards #2

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ def create_app():
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_DATABASE_URI")


from app.models.board import Board
from app.models.card import Card

db.init_app(app)
migrate.init_app(app, db)

from .routes import hello_world_bp
app.register_blueprint(hello_world_bp)

from .routes import boards_bp
app.register_blueprint(boards_bp)

Expand Down
4 changes: 1 addition & 3 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def create_app(test_config=None):
migrate.init_app(app, db)

# from app.models.planet import Planet


# from .routes import planets_bp
# app.register_blueprint(planets_bp)

return app
return app
4 changes: 3 additions & 1 deletion app/models/board.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from app import db


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)
owner = db.Column(db.String)
cards = db.relationship('Card', backref='cards', lazy=True)
5 changes: 4 additions & 1 deletion app/models/card.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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)
likes_count = db.Column(db.Integer)
board_id = db.Column(db.Integer, db.ForeignKey(
"board.board_id"), nullable=True)
58 changes: 50 additions & 8 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
boards_bp = Blueprint("boards", __name__, url_prefix="/boards")
hello_world_bp = Blueprint("hello_world", __name__)


load_dotenv()


@hello_world_bp.route("/hello-world", methods=["GET"])
def hello_world():
my_beautiful_response_body = "Hello, World!"
Expand All @@ -25,6 +27,7 @@ def hello_world():
# headers = {'Authorization': f"Bearer {SLACK_TOKEN}"}
# request.post(slack_path, params=query_params, headers=headers)


@boards_bp.route("", methods=["GET", "POST"])
def handle_boards():
if request.method == "GET":
Expand All @@ -42,41 +45,80 @@ def handle_boards():
title = request_body.get("title")
owner = request_body.get("owner")
new_board = Board(title=request_body["title"],
owner=request_body["owner"])
owner=request_body["owner"])

db.session.add(new_board)
db.session.commit()

return make_response(f"Board {new_board.title} successfully created", 201)


@boards_bp.route("/<board_id>", methods=["GET", "PUT", "DELETE"])
def handle_board(board_id):
board = Board.query.get(board_id)

if request.method == "GET":
if board == None:
return make_response("That board does not exist", 404)
return {
"id": board.board_id,
"title": board.title,
"owner": board.owner
"owner": board.owner,
"cards": board.cards
}
elif request.method == "PUT":
if board == None:
return make_response("Board does not exist", 404)
form_data = request.get_json()

board.title = form_data["title"]
board.owner = form_data["owner"]

db.session.commit()

return make_response(f"Board: {board.title} sucessfully updated.")

elif request.method == "DELETE":
if board == None:
return make_response("Board does not exist", 404)
db.session.delete(board)
db.session.commit()
return make_response(f"Board: {board.title} sucessfully deleted.")
# example_bp = Blueprint('example_bp', __name__)


@boards_bp.route("/<board_id>/cards", methods=["POST", "GET"])
def handle_cards(board_id):
board = Board.query.get(board_id)

if board is None:
return make_response("", 404)

if request.method == "GET":
cards = Board.query.get(board_id).cards
cards_response = []
for card in cards:
cards_response.append({
"message": card.message
})

return make_response(
{
"cards": cards_response
}, 200)
elif request.method == "POST":
request_body = request.get_json()
if 'message' not in request_body:
return {"details": "Invalid data"}, 400
new_card = Card(message=request_body["message"],
board_id=board_id)

db.session.add(new_card)
db.session.commit()

return {
"card": {
"id": new_card.card_id,
"message": new_card.message
}
}, 201
34 changes: 34 additions & 0 deletions migrations/versions/456c575cd4a3_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""empty message

Revision ID: 456c575cd4a3
Revises: 0998518428e5
Create Date: 2021-07-13 11:00:17.480900

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '456c575cd4a3'
down_revision = '0998518428e5'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('board', sa.Column('card_id', sa.Integer(), nullable=True))
op.create_foreign_key(None, 'board', 'card', ['card_id'], ['card_id'])
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')
op.drop_constraint(None, 'board', type_='foreignkey')
op.drop_column('board', 'card_id')
# ### end Alembic commands ###
30 changes: 30 additions & 0 deletions migrations/versions/476dbf543b0b_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""empty message

Revision ID: 476dbf543b0b
Revises: 456c575cd4a3
Create Date: 2021-07-13 11:02:51.043402

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '476dbf543b0b'
down_revision = '456c575cd4a3'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('board_card_id_fkey', 'board', type_='foreignkey')
op.drop_column('board', 'card_id')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('board', sa.Column('card_id', sa.INTEGER(), autoincrement=False, nullable=True))
op.create_foreign_key('board_card_id_fkey', 'board', 'card', ['card_id'], ['card_id'])
# ### end Alembic commands ###