diff --git a/app/__init__.py b/app/__init__.py index 3c581ceeb..d5a88d8a9 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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): @@ -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 diff --git a/app/models/__init__.py b/app/models/__init__.py index e69de29bb..4e768b56d 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/app/models/task.py b/app/models/task.py index 5d99666a4..f5c68879e 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -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 + } \ No newline at end of file diff --git a/app/routes/task_routes.py b/app/routes/task_routes.py index 3aae38d49..229be268f 100644 --- a/app/routes/task_routes.py +++ b/app/routes/task_routes.py @@ -1 +1,56 @@ -from flask import Blueprint \ No newline at end of file +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("/") +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 diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index 55475db79..9f465a166 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -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") @@ -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") @@ -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") @@ -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") @@ -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={ @@ -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={ @@ -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={ @@ -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") @@ -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") @@ -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={ @@ -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={