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

Lions- jupuabmi (Miranda, Puja, Julia, Abby) #35

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ 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)

# Register Blueprints here
# from .routes import example_bp
# app.register_blueprint(example_bp)
from .routes import board_bp
app.register_blueprint(board_bp)
from .routes import card_bp
app.register_blueprint(card_bp)


CORS(app)
return app
38 changes: 38 additions & 0 deletions app/models/board.py
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
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)
cards=db.relationship("Card", back_populates="board")


# class Author(db.Model):
# id = db.Column(db.Integer, primary_key=True, autoincrement=True)
# name = db.Column(db.String)
# books = db.relationship("Book", back_populates="author")


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

}
if self.board_id:
board_dict["board_id"] = self.board_id
return board_dict

@classmethod
def from_dict(cls, board_dict):
if "title" in board_dict and "owner" in board_dict:
new_obj = cls(
# board_id=board_dict["board_id"],
title=board_dict["title"],
owner= board_dict["owner"])

return new_obj




56 changes: 56 additions & 0 deletions app/models/card.py
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
from app import db

class Card(db.Model):
card_id= db.Column(db.Integer, primary_key=True)
message= db.Column(db.String)
likes_count= db.Column(db.Integer)
board_id = db.Column(db.Integer, db.ForeignKey('board.board_id'))
board = db.relationship("Board", back_populates="cards")

# id = db.Column(db.Integer, primary_key=True, autoincrement=True)
# title = db.Column(db.String)
# description = db.Column(db.String)
# author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
# author = db.relationship("Author", back_populates="books")

def to_dict(self):
card_dict = {
"card_id" : self.card_id,
"message" : self.message,
"likes_count" : self.likes_count,
"board_id": self.board_id
}
if self.card_id:
card_dict["card_id"] = self.card_id

return card_dict

@classmethod
def from_dict(cls, data_dict):
if "message" in data_dict :
new_obj = cls(
message = data_dict["message"])
return new_obj
# def to_dict(self):
# task_dict = {
# "id": self.task_id,
# "title": self.title,
# "description": self.description,
# "is_complete": False if self.completed_at is None else True
# }
# if self.goal_id:
# task_dict["goal_id"] = self.goal_id

# return task_dict


# @classmethod
# def from_dict(cls, data_dict):
# if "title" in data_dict and "description" in data_dict and "is_complete" in data_dict:
# new_obj = cls(
# title=data_dict["title"],
# description=data_dict["description"],
# is_complete= data_dict["is_complete"])

# return new_obj


131 changes: 131 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,135 @@
from flask import Blueprint, request, jsonify, make_response
from app import db
from app.models.board import Board
from app.models.card import Card
from .routes_helper import get_one_obj_or_abort


# example_bp = Blueprint('example_bp', __name__)
board_bp = Blueprint("board_bp", __name__, url_prefix="/boards")
card_bp = Blueprint("card_bp", __name__, url_prefix="/cards")

# @card_bp.route("", methods=["POST"])
# def create_card():
# response_body = request.get_json()

# new_card = Card.from_dict(response_body)
# db.session.add(new_card)
# db.session.commit()

# return {"card_id": new_card.card_id}, 201

# @card_bp.route("", methods=["GET"])
# def get_all_cards():

# cards = Card.query.order_by(Card.card_id).all()
# response = [card.to_dict() for card in cards]

# return jsonify(response), 200



@card_bp.route("/<card_id>", methods=["DELETE"])
def delete_one_card(card_id):
chosen_card = get_one_obj_or_abort(Card, card_id)

db.session.delete(chosen_card)

db.session.commit()

return jsonify({"message": f"Successfully deleted card with id `{card_id}`"}), 200

@card_bp.route("/<card_id>/like", methods=["PUT"])
def update_count_like(card_id):
chosen_card = get_one_obj_or_abort(Card, card_id)

request_body = request.get_json()
chosen_card.likes_count = request_body["likes_count"]

db.session.commit()

return make_response(f"Card #{card_id} successfully updated")

# @books_bp.route("/<book_id>", methods=["PUT"])
# def update_book(book_id):
# book = validate_book(book_id)

# request_body = request.get_json()

# book.title = request_body["title"]
# book.description = request_body["description"]

# db.session.commit()

# return make_response(f"Book #{book.id} successfully updated")

####################
@board_bp.route("", methods=["POST"])
def create_board():
response_body = request.get_json()

new_board = Board.from_dict(response_body)
db.session.add(new_board)
db.session.commit()

return {"board_id": new_board.board_id}, 201

# @task_bp.route("", methods=["GET"])
# def get_all_tasks():
@board_bp.route("", methods=["GET"])
def get_all_boards():

boards = Board.query.order_by(Board.board_id).all()
response = [board.to_dict() for board in boards]

return jsonify(response), 200


@board_bp.route("/<board_id>/cards", methods=["GET"])
def get_all_cards_belong_to_a_board(board_id):
board = get_one_obj_or_abort(Board, board_id)

card_response = [card.to_dict() for card in board.cards]

return jsonify(card_response), 200


# @board_bp.route("/boards", methods=["DELETE"])
# def delete_board(board_id):
# chosen_board = get_one_obj_or_abort(Board, board_id)

# db.session.delete(chosen_board)

# db.session.commit()

# return jsonify({"message": f"Successfully deleted board with id `{board_id}`"}), 200

@board_bp.route("/<board_id>/cards", methods=["POST"])
def post_card_belonging_to_a_board(board_id):
parent_board = get_one_obj_or_abort(Board, board_id)

request_body = request.get_json()

new_card = Card.from_dict(request_body)
new_card.board = parent_board

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

# return jsonify({"message":f"Card {new_card.message} belonging to {new_card.board.title} successfully added"}), 201
return jsonify(new_card.to_dict())



# @books_bp.route("/<book_id>", methods=["PUT"])
# def update_book(book_id):
# book = validate_book(book_id)

# request_body = request.get_json()

# book.title = request_body["title"]
# book.description = request_body["description"]

# db.session.commit()

# return make_response(f"Book #{book.id} successfully updated")
16 changes: 16 additions & 0 deletions app/routes_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from flask import jsonify, abort, make_response

def get_one_obj_or_abort(cls, obj_id):
try:
obj_id = int(obj_id)
except ValueError:
response_str = f"Invalid ID: `{obj_id}`. ID must be an integer"
abort(make_response(jsonify({"message":response_str}), 400))

matching_obj = cls.query.get(obj_id)

if not matching_obj:
response_str = f"{cls.__name__} with id `{obj_id}` was not found in the database."
abort(make_response(jsonify({"message":response_str}), 404))

return matching_obj