Skip to content
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
4 changes: 2 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask
from .db import db, migrate
from .models import task, goal
from .routes.task_routes import tasks_bp #, goal
import os

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

# Register Blueprints here
app.register_blueprint(tasks_bp)

return app
1 change: 1 addition & 0 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
34 changes: 34 additions & 0 deletions app/models/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
from sqlalchemy.orm import Mapped, mapped_column
from ..db import db
from datetime import datetime
from typing import Optional, Dict, Any

class Task(db.Model):
__tablename__ = "tasks"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str]
description: Mapped[str]
completed_at: Mapped[Optional[datetime]]


@classmethod
def from_dict(cls, data: Dict[str, Any]):
title = data.get("title", "")
description = data.get("description", "")
completed_at = data.get("completed_at", None)

if isinstance(completed_at, str):
try:
completed_at = datetime.fromisoformat(completed_at)
except ValueError:
completed_at = None

return cls(
title=title,
description=description,
completed_at=completed_at
)

def to_dict(self):
return {
"id": self.id,
"title": self.title,
"description": self.description,
"is_complete": self.completed_at is not None
}
57 changes: 56 additions & 1 deletion app/routes/task_routes.py
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
from flask import Blueprint
from flask import Blueprint, request, make_response
from app.models.task import Task
from app import db

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

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

db.session.add(new_task)
db.session.commit()

message = {
"id": new_task.id,
"title": new_task.title,
"description": new_task.description,
"completed at": new_task.completed_at,
}

return message, 201


@tasks_bp.get("/<tasks>")
def get_saved_tasks(task_id):
query = db.select(Task)

query = query.order_by(Task.id)
tasks = db.session.scalars(query)

message = [
{
"id": task.id,
"title": task.title,
"description": task.description,
"completed at": task.completed_at,

}
for task in tasks]

return message, 200

@tasks_bp.get("")
def no_saved_tasks():
tasks_response = []
for task in tasks:
tasks_response.append(
{
"id": task.id,
"title": task.title,
"description": task.description,
"completed at": task.completed_at
}
)
return tasks_response
22 changes: 11 additions & 11 deletions tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_get_tasks_no_saved_tasks(client):
# Act
response = client.get("/tasks")
Expand All @@ -14,7 +14,7 @@ def test_get_tasks_no_saved_tasks(client):
assert response_body == []


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_get_tasks_one_saved_tasks(client, one_task):
# Act
response = client.get("/tasks")
Expand All @@ -33,7 +33,7 @@ def test_get_tasks_one_saved_tasks(client, one_task):
]


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_get_task(client, one_task):
# Act
response = client.get("/tasks/1")
Expand All @@ -52,7 +52,7 @@ def test_get_task(client, one_task):
}


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_get_task_not_found(client):
# Act
response = client.get("/tasks/1")
Expand All @@ -67,7 +67,7 @@ def test_get_task_not_found(client):
# *****************************************************************


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_create_task(client):
# Act
response = client.post("/tasks", json={
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_create_task(client):
assert new_task.completed_at == None


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_update_task(client, one_task):
# Act
response = client.put("/tasks/1", json={
Expand All @@ -117,7 +117,7 @@ def test_update_task(client, one_task):



@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_update_task_not_found(client):
# Act
response = client.put("/tasks/1", json={
Expand All @@ -135,7 +135,7 @@ def test_update_task_not_found(client):
# *****************************************************************


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_delete_task(client, one_task):
# Act
response = client.delete("/tasks/1")
Expand All @@ -146,7 +146,7 @@ def test_delete_task(client, one_task):
query = db.select(Task).where(Task.id == 1)
assert db.session.scalar(query) == None

@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_delete_task_not_found(client):
# Act
response = client.delete("/tasks/1")
Expand All @@ -163,7 +163,7 @@ def test_delete_task_not_found(client):
assert db.session.scalars(db.select(Task)).all() == []


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_create_task_must_contain_title(client):
# Act
response = client.post("/tasks", json={
Expand All @@ -180,7 +180,7 @@ def test_create_task_must_contain_title(client):
assert db.session.scalars(db.select(Task)).all() == []


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_create_task_must_contain_description(client):
# Act
response = client.post("/tasks", json={
Expand Down