Skip to content

Phoenix - Dana D #26

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

Open
wants to merge 6 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
13 changes: 11 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from flask import Flask
from .db import db, migrate
from .models import task, goal
from .routes.task_routes import tasks_bp
from .routes.goal_routes import goals_bp
from flask_cors import CORS
import os

def create_app(config=None):
Expand All @@ -10,13 +13,19 @@ def create_app(config=None):
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')

if config:
# Merge `config` into the app's configuration
# to override the app's default settings for testing
app.config.update(config)

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

CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

# Register Blueprints here
app.register_blueprint(tasks_bp)
app.register_blueprint(goals_bp)

return app


return app
12 changes: 11 additions & 1 deletion app/models/goal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy import ForeignKey
from ..db import db

class Goal(db.Model):
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column(nullable=False)

tasks = relationship("Task", back_populates="goal", cascade="all, delete-orphan")

def to_dict(self):
return {
'id': self.id,
'title': self.title,
}
20 changes: 19 additions & 1 deletion app/models/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy import ForeignKey
from datetime import datetime
from ..db import db

class Task(db.Model):
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str]
description: Mapped[str]
completed_at: Mapped[datetime] = mapped_column(nullable=True)

goal_id = mapped_column(db.Integer, db.ForeignKey('goal.id'), nullable=True)

goal = relationship("Goal", back_populates="tasks")

def to_dict(self):
return {
'id': self.id,
'goal_id': self.goal_id,
'title': self.title,
'description': self.description,
'is_complete': bool(self.completed_at is not None),
}
138 changes: 137 additions & 1 deletion app/routes/goal_routes.py
Original file line number Diff line number Diff line change
@@ -1 +1,137 @@
from flask import Blueprint
from flask import Blueprint, request
from app.models.goal import Goal
from app.models.task import Task
from app.db import db

goals_bp = Blueprint("goals", __name__, url_prefix="/goals")

@goals_bp.post("")
def create_goal():
request_data = request.get_json()
title = request_data.get("title")

if not title:
return {"details": "Invalid data"}, 400

goal = Goal(title=title)
db.session.add(goal)
db.session.commit()

return {"goal": {"id": goal.id, "title": goal.title}}, 201

# Get All Goals
@goals_bp.get("")
def get_goals():
goals = Goal.query.all()
return [
{
"id": goal.id,
"title": goal.title
}
for goal in goals
], 200

# Get One Goal by ID
@goals_bp.get("/<int:goal_id>")
def get_goal(goal_id):
goal = Goal.query.get(goal_id)
if goal:
return {
"goal": {
"id": goal.id,
"title": goal.title
}
}, 200
return {
"message": f"Goal {goal_id} not found"
}, 404

@goals_bp.put("/<int:goal_id>")
def update_goal(goal_id):
goal = Goal.query.get(goal_id)
if not goal:
return {"message": f"Goal {goal_id} not found"}, 404

request_data = request.get_json()
title = request_data.get("title")

if not title:
return {"details": "Invalid data"}, 400

goal.title = title
db.session.commit()

return {
"goal": {
"id": goal.id,
"title": goal.title
}
}, 200

@goals_bp.delete("/<int:goal_id>")
def delete_goal(goal_id):
goal = Goal.query.get(goal_id)

if not goal:
return {
"message": f"Goal {goal_id} not found"
}, 404

db.session.delete(goal)
db.session.commit()

return {
"details": f'Goal {goal_id} "{goal.title}" successfully deleted'
}, 200


@goals_bp.post("/<int:goal_id>/tasks")
def post_task_ids_to_goal(goal_id):
goal = Goal.query.get(goal_id)
if not goal:
return {
"message": f"Goal {goal_id} not found"
}, 404

request_data = request.get_json()
task_ids = request_data.get("task_ids", [])

tasks = Task.query.filter(Task.id.in_(task_ids)).all()

if len(tasks) != len(task_ids):
return {
"message": "Some task IDs are invalid"
}, 400

for task in tasks:
task.goal_id = goal.id
db.session.commit()

return {
"id": goal.id,
"task_ids": [task.id for task in tasks]
}, 200

@goals_bp.get("/<int:goal_id>/tasks")
def get_tasks_for_goal(goal_id):
goal = Goal.query.get(goal_id)
if not goal:
return {
"message": f"Goal {goal_id} not found"
}, 404

tasks = goal.tasks

return {
"id": goal.id,
"title": goal.title,
"tasks": [
{
"id": task.id,
"goal_id": task.goal_id,
"title": task.title,
"description": task.description,
"is_complete": task.completed_at is not None
} for task in tasks
]
}, 200
162 changes: 161 additions & 1 deletion app/routes/task_routes.py
Original file line number Diff line number Diff line change
@@ -1 +1,161 @@
from flask import Blueprint
import os
import requests
from flask import Blueprint, abort, make_response, request, Response
from datetime import datetime, timezone
from app.models.task import Task
from ..db import db
from dotenv import load_dotenv
from app.models.goal import Goal

load_dotenv()

tasks_bp = Blueprint("tasks_bp", __name__, url_prefix="/tasks")

@tasks_bp.get("")
def get_tasks():
sort_order = request.args.get("sort", "asc").lower() # Default to ascending

if sort_order == "desc":
tasks = Task.query.order_by(Task.title.desc()).all()
else:
tasks = Task.query.order_by(Task.title.asc()).all()

# Prepare response data
task_list = [
{
"id": task.id,
"title": task.title,
"description": task.description,
"is_complete": task.completed_at is not None
}
for task in tasks
]

return (task_list), 200

@tasks_bp.get("/<int:task_id>")
def get_task(task_id):
task = Task.query.get(task_id)
if not task:
return {"message": f"Task {task_id} not found"}, 404

return {"task": {
"id": task.id,
"goal_id": task.goal_id,
"title": task.title,
"description": task.description,
"is_complete": task.completed_at is not None
}}, 200

@tasks_bp.post("")
def create_task():
request_body = request.get_json()

title = request_body.get("title")
description = request_body.get("description")
completed_at = request_body.get("completed_at")

if not title or not description:
return {"details": "Invalid data"}, 400

if completed_at is None:
completed_at = None

new_task = Task(title=title, description=description, completed_at=completed_at)
db.session.add(new_task)
db.session.commit()

return {"task": {
"id": new_task.id,
"title": new_task.title,
"description": new_task.description,
"is_complete": new_task.completed_at is not None
}}, 201

@tasks_bp.put("/<int:task_id>")
def update_task(task_id):
task = Task.query.get(task_id)
if not task:
return {"message": f"Task {task_id} not found"}, 404

request_body = request.get_json()
title = request_body.get("title")
description = request_body.get("description")
completed_at = request_body.get("completed_at")

# Update attributes if provided in request body
if title:
task.title = title
if description:
task.description = description
if completed_at is not None:
task.completed_at = completed_at

db.session.commit()

return {"task": {
"id": task.id,
"title": task.title,
"description": task.description,
"is_complete": task.completed_at is not None
}}, 200

@tasks_bp.delete("/<int:task_id>")
def delete_task(task_id):
task = Task.query.get(task_id)
if not task:
return {"message": f"Task {task_id} not found"}, 404

db.session.delete(task)
db.session.commit()
return {"details": f'Task {task_id} "{task.title}" successfully deleted'}, 200

@tasks_bp.patch("/<int:task_id>/mark_complete")
def task_complete(task_id):
task = Task.query.get(task_id)

if not task:
return {"message": f"Task {task_id} not found"}, 404

task.completed_at = datetime.now()
db.session.commit()

slack_token = os.environ.get('SLACK_TOKEN')
slack_url = "https://slack.com/api/chat.postMessage"
slack_data = {
"channel": "api-test-channel",
"text": "Someone just completed the task " + task.title
}
slack_header = {
"Authorization": f"Bearer {slack_token}",
}

requests.post(slack_url, data=slack_data, headers=slack_header)

return {
"task": {
"id": task.id,
"title": task.title,
"description": task.description,
"is_complete": True
}
}, 200

@tasks_bp.patch("/<int:task_id>/mark_incomplete")
def task_incomplete(task_id):
task = Task.query.get(task_id)

if not task:
return {"message": f"Task {task_id} not found"}, 404

task.completed_at = None
db.session.commit()

return {
"task": {
"id": task.id,
"title": task.title,
"description": task.description,
"is_complete": False
}
}, 200
1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Single-database configuration for Flask.
Loading