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

Amethyst - Angela, Hannah, Elaine, Maz, Raina #22

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
079839a
migrations
Rainacam12 Jun 26, 2023
4e04f67
model import statements added into init.py
Rainacam12 Jun 26, 2023
2ce2c1b
Created class Card model
angela100743 Jun 26, 2023
f475c4e
board model added
Rainacam12 Jun 26, 2023
eb23010
Fixed mistake on autoincrement
angela100743 Jun 26, 2023
d3cb87f
corrected import statments
Rainacam12 Jun 26, 2023
eb52013
Merge branch 'main' of https://github.com/Hannah1Watkins/back-end-ins…
Rainacam12 Jun 26, 2023
c4d6592
est. relationship between card and board in board
Rainacam12 Jun 26, 2023
e87501f
Set up relationship with Board in class Card
angela100743 Jun 26, 2023
502c477
connected card and board w/ foreignkey
Rainacam12 Jun 26, 2023
baceba5
Merge branch 'main' of https://github.com/Hannah1Watkins/back-end-ins…
angela100743 Jun 26, 2023
fdef0aa
Merge branch 'main' of https://github.com/Hannah1Watkins/back-end-ins…
angela100743 Jun 26, 2023
26dffc0
Create from_dict and to_dict for CARD
angela100743 Jun 27, 2023
3d853ca
Post method created for card - not working on Postman yet
angela100743 Jun 27, 2023
550fcba
Added GET and DELETE routes for CARD
angela100743 Jun 27, 2023
659579c
added board routes
Rainacam12 Jun 27, 2023
d312aad
created separate routes in folder
Rainacam12 Jun 27, 2023
7e74ba3
to_dict and from_dict funcs restored in board.py
Rainacam12 Jun 27, 2023
55e8ac8
registered bps
Rainacam12 Jun 27, 2023
884504b
Indentation fixed for from_dict and to_dict
angela100743 Jun 27, 2023
8453573
Get all cards route in progress, instructions added
angela100743 Jun 27, 2023
496f4e9
Migrations folder updated
angela100743 Jun 27, 2023
e2dd482
Created entire new migrations folder
angela100743 Jun 27, 2023
b748918
added deletions
mlweir98 Jun 27, 2023
4a8a6d9
added migrations
mlweir98 Jun 27, 2023
5eed4a2
Patch endpoint created to update liked_count, POST by board_id, GET c…
angela100743 Jun 27, 2023
24a38c0
updated routes to make functionality work
mlweir98 Jun 28, 2023
2dd52c0
delete board route created
Rainacam12 Jun 28, 2023
df93adb
updated board model to_dict method to include cards
mlweir98 Jun 28, 2023
e4d2a4c
updated the board component, cards attribute to serialize
ewatki Jun 29, 2023
0a71256
updated cors headers to fix error
mlweir98 Jun 29, 2023
c206a64
added user login routes
Hannah1Watkins Jul 18, 2023
fba7c36
backend reconnect
Rainacam12 Jul 18, 2023
6f0eb34
changed user.py file to include first name & last for registration
Hannah1Watkins Jul 19, 2023
473223d
Merge branch 'main' of https://github.com/Hannah1Watkins/back-end-ins…
Hannah1Watkins Jul 19, 2023
2c6f0a0
changed user_routes.py file to include first name & last for registra…
Hannah1Watkins Jul 19, 2023
1e3f16f
commit
mlweir98 Jul 20, 2023
770057e
Updated render link to init file
angela100743 Jul 21, 2023
fe63fdc
New migrations created after render connection built
angela100743 Jul 21, 2023
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
18 changes: 15 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
def create_app():
app = Flask(__name__)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_DATABASE_URI")
"RENDER_DATABASE_URI")

# Import models here for Alembic setup
# from app.models.ExampleModel import ExampleModel
Expand All @@ -25,7 +26,18 @@ def create_app():

# Register Blueprints here
# from .routes import example_bp
from app.routes.board_routes import board_bp
from app.routes.card_routes import card_bp
from app.routes.user_routes import user_bp

app.register_blueprint(board_bp)
app.register_blueprint(card_bp)
app.register_blueprint(user_bp)

# app.register_blueprint(example_bp)

CORS(app)
from app.models.card import Card
from app.models.board import Board
from app.models.user import User

return app
23 changes: 23 additions & 0 deletions app/models/board.py
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
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")

@classmethod
# data to give that will be organized into a dict
def from_dict(cls, board_data):
new_board = Board(
title=board_data["title"],
owner=board_data["owner"]
)
return new_board

# turn response into dict, using this on an object
def to_dict(self):
return {
"board_id": self.board_id,
"title": self.title,
"owner": self.owner
}
26 changes: 26 additions & 0 deletions app/models/card.py
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
from app import db

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


@classmethod
def from_dict(cls, card_data):
new_card = Card(
message = card_data["message"],
liked_count = card_data["liked_count"]
)

return new_card


def to_dict(self):
return {
"card_id": self.card_id,
"message": self.message,
"liked_count": self.liked_count,
"board_id": self.board_fk
}
22 changes: 22 additions & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from app import db

class User(db.Model):
user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
first_name = db.Column(db.String, nullable=False)
last_name = db.Column(db.String, nullable=False)

def __init__(self, username, password, first_name, last_name):
self.username = username
self.password = password
self.first_name = first_name
self.last_name = last_name

def to_dict(self):
return {
"user_id": self.user_id,
"username": self.username,
"first_name": self.first_name,
"last_name": self.last_name
}
4 changes: 0 additions & 4 deletions app/routes.py

This file was deleted.

70 changes: 70 additions & 0 deletions app/routes/board_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from flask import Blueprint, request, jsonify, make_response, abort
from app import db
from app.models.board import Board

board_bp =Blueprint("boards", __name__, url_prefix="/boards" )

def validate_model(cls, model_id):
try:
model_id = int(model_id)
except:
abort(make_response({"message": f"{model_id} invalid type ({type(model_id)})"}, 400))

model = cls.query.get(model_id)

if not model:
abort(make_response({"message":f"{model_id} invalid"}, 400))

return model


# CREATE new board
@board_bp.route("", methods=["POST"])
def create_new_board():
request_body = request.get_json()

# pass in request_body
new_board = Board.from_dict(request_body)

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

return jsonify(new_board.to_dict()), 201


# READ all boards
@board_bp.route("", methods=["GET"])
def read_all_boards():
boards_response = []

board_title_query = request.args.get("title")
board_owner_query = request.args.get("owner")

if board_title_query:
boards = Board.query.filter_by(title=board_title_query)
elif board_owner_query:
boards = Board.query.filter_by(owner=board_owner_query)
else:
boards = Board.query.all()

for board in boards:
boards_response.append(board.to_dict())
return jsonify(boards_response)


# READ a specific board
@board_bp.route("/<board_id>", methods=["GET"])
def read_one_board(board_id):
board = validate_model(Board, board_id)

return board.to_dict(), 200

# DELETE one board
@board_bp.route("/<board_id>", methods=["DELETE"])
def delete_one_board(board_id):
board = validate_model(Board, board_id)

db.session.delete(board)
db.session.commit()

return make_response(board.to_dict())
81 changes: 81 additions & 0 deletions app/routes/card_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from flask import Blueprint, request, jsonify, make_response, abort
from app import db
from app.models.card import Card
from app.models.board import Board
from app.routes.board_routes import board_bp


card_bp = Blueprint("cards", __name__, url_prefix="/cards")
### Validate model ###
def validate_model(cls, model_id):
try:
model_id = int(model_id)
except:
abort(make_response({"message": f"{model_id} is not a valid type ({type(model_id)})"}))

model = cls.query.get(model_id)

if not model:
abort(make_response({"message":f"{cls.__name__} {model_id} does not exist"}, 404))

return model


### Post a new card under a board ###
@board_bp.route("/<board_id>/cards", methods = ["POST"])
def create_card_by_board_id(board_id):
board = validate_model(Board, board_id)

request_body = request.get_json()

new_card = Card(
message=request_body["message"],
liked_count=0,
board=board
)

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

return jsonify(new_card.to_dict()), 201
# we want response to be new_card object with id, message, board, like count

### Get all cards from a board ###
@board_bp.route("/<board_id>/cards", methods=["GET"])
def get_all_cards_with_board_id(board_id):
board = validate_model(Board, board_id)

card_response = []

for card in board.cards:
card_response.append(card.to_dict())

return jsonify(card_response),200


### Update liked_count by card ###
@card_bp.route('/<card_id>', methods=['PATCH'])
def update_liked_count(card_id):
card = validate_model(Card, card_id)

if not card.liked_count:
card.liked_count = 0

card.liked_count = card.liked_count + 1

db.session.commit()

return card.to_dict(), 200


### Delete card ###
@card_bp.route("/<card_id>", methods=["DELETE"])
def delete_card(card_id):
card = validate_model(Card, card_id)

db.session.delete(card)
db.session.commit()

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


45 changes: 45 additions & 0 deletions app/routes/user_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from flask import Blueprint, request, jsonify, make_response
from app import db
from app.models.user import User

user_bp = Blueprint("user", __name__, url_prefix="/user")

@user_bp.route("/login", methods=["POST"])
def login_user():
data = request.get_json()
username = data.get("username")
password = data.get("password")

if not username or not password:
return make_response(jsonify({"message": "Username and password are required."}), 400)

user = User.query.filter_by(username=username).first()

if not user or user.password != password:
return make_response(jsonify({"message": "Invalid username or password."}), 401)

return jsonify({"message": "Login successful.", "user": user.to_dict()}), 200


@user_bp.route("/register", methods=["POST"])
def register_user():
data = request.get_json()
username = data.get("username")
password = data.get("password")
first_name = data.get("firstName")
last_name = data.get("lastName")

if not username or not password or not first_name or not last_name:
return make_response(jsonify({"message": "Username, password, first name, and last name are required."}), 400)

existing_user = User.query.filter_by(username=username).first()
if existing_user:
return make_response(jsonify({"message": "Username already taken."}), 409)

new_user = User(username=username, password=password, first_name=first_name, last_name=last_name)
db.session.add(new_user)
db.session.commit()

return new_user.to_dict(), 201


1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
45 changes: 45 additions & 0 deletions migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -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
Loading