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

add update card API #4

Merged
merged 2 commits into from
Dec 25, 2024
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
2 changes: 2 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .models.board import Board
from .models.card import Card
from .routes.board_routes import boards_bp
from .routes.card_routes import bp as cards_bp
import os

# Import models, blueprints, and anything else needed to set up the app or database
Expand All @@ -24,6 +25,7 @@ def create_app(config=None):

# Register Blueprints
app.register_blueprint(boards_bp)
app.register_blueprint(cards_bp)

CORS(app)
return app
28 changes: 22 additions & 6 deletions app/routes/card_routes.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@

from flask import Blueprint, abort, make_response, request, Response
from app.models.board import Board
from app.models.card import Card
from .route_utilities import validate_model
from ..db import db
import requests
import json
import os

bp=Blueprint("cards_bp", __name__, url_prefix="/boards/<board_id>/cards")
bp = Blueprint("cards_bp", __name__, url_prefix="/boards/<board_id>/cards")


@bp.post("")
def create_card(board_id):

request_body = request.get_json()


try:
new_card = Card.from_dict(request_body)
except KeyError as e:
missing_key = e.args[0]
response = {"details": f"Invalid request body: Missing key '{missing_key}'"}
abort(make_response(response, 400))

if len(request_body["message"]) > 40:
response = {"details": "Invalid request body: Message must be 40 characters or less"}
response = {
"details": "Invalid request body: Message must be 40 characters or less"
}
abort(make_response(response, 400))

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

response = {"card": new_card.to_dict()}
return response, 201
return response, 201


@bp.put("/<card_id>")
def agree_with_card(board_id, card_id):
try:
card = validate_model(Card, card_id)
except:
response = {"details": f"Card {card_id} not found"}
abort(make_response(response, 404))

card.likes_count += 1
db.session.commit()

response = {"card": card.to_dict()}
return response
11 changes: 11 additions & 0 deletions app/routes/route_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask import abort, make_response
from ..db import db


def validate_model(cls, model_id):
model_id = int(model_id)
query = db.select(cls).where(cls.id == model_id)
model = db.session.scalar(query)
if not model:
raise Exception("model not found")
return model