Skip to content

Commit

Permalink
Merge pull request #1 from hintow/wei
Browse files Browse the repository at this point in the history
Wei
  • Loading branch information
hintow authored Dec 22, 2024
2 parents 56e30cb + 3759af2 commit 7ff7a2a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .db import db, migrate
from .models.board import Board
from .models.card import Card
from .routes.board_routes import bp as boards_bp
import os
# Import models, blueprints, and anything else needed to set up the app or database

Expand All @@ -21,6 +22,7 @@ def create_app(config=None):
migrate.init_app(app, db)

# Register Blueprints
app.register_blueprint(boards_bp)


CORS(app)
Expand Down
15 changes: 15 additions & 0 deletions app/models/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ class Board(db.Model):
board_id : Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str]
owner: Mapped[str]

def to_dict(self):
board_dict ={
"board_id": self.board_id,
"title": self.title,
"owner": self.owner
}
return board_dict

@classmethod
def from_dict(cls, board_data):
return cls(
title=board_data["title"],
owner=board_data["owner"]
)
4 changes: 3 additions & 1 deletion app/models/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
class Card(db.Model):
card_id : Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
message: Mapped[str]
likes_count: Mapped[int]
likes_count: Mapped[int]


Empty file added app/routes/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions app/routes/board_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask import Blueprint, abort, make_response, request, Response
from app.models.board import Board
from ..db import db
import requests
import json
import os

bp=Blueprint("boards_bp", __name__, url_prefix="/boards")

@bp.post("")
def create_board():
request_body = request.get_json()
try:
new_board = Board.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))

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

response = {"board": new_board.to_dict()}
return response, 201

0 comments on commit 7ff7a2a

Please sign in to comment.