diff --git a/app.py b/app.py index 11e2013..b2da9fa 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,13 @@ db = init_db(app) +@app.route('/health', methods=['GET']) +def health(): + return jsonify( + { + "Work" + } + ), 200 @app.route('/') def home() -> str: @@ -26,11 +33,14 @@ def index() -> Response: @app.route('/animal', methods=['POST']) def add_animal() -> tuple[Response, int]: data = AnimalCreate(**request.get_json()) + new_animal = Animal( animal_type=data.animal_type, name=data.name, - birth_date=data.birth_date + breed=data.breed, + birth_date=data.birth_date, ) + db.session.add(new_animal) db.session.commit() return jsonify( @@ -50,6 +60,7 @@ def update_animal(pk: int) -> Union[Response, tuple[Response, int]]: animal.animal_type = data.animal_type animal.name = data.name + animal.breed = data.breed animal.birth_date = data.birth_date db.session.commit() return jsonify( @@ -92,3 +103,4 @@ def initialize_app(): if __name__ == '__main__': initialize_app() app.run(debug=True) + diff --git a/database.py b/database.py index 7c8817d..54188d6 100644 --- a/database.py +++ b/database.py @@ -1,7 +1,7 @@ -from flask_sqlalchemy import SQLAlchemy +import flask_sqlalchemy from flask_migrate import Migrate -db = SQLAlchemy() +db = flask_sqlalchemy.SQLAlchemy() migrate = Migrate() diff --git a/migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py b/migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py new file mode 100644 index 0000000..a35af21 --- /dev/null +++ b/migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py @@ -0,0 +1,32 @@ +"""Add breed column to animal + +Revision ID: f2aafb7fe882 +Revises: e08fc0218f8b +Create Date: 2025-06-07 16:04:35.525625 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f2aafb7fe882' +down_revision = 'e08fc0218f8b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('animal', schema=None) as batch_op: + batch_op.add_column(sa.Column('breed', sa.String(), nullable=False)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('animal', schema=None) as batch_op: + batch_op.drop_column('breed') + + # ### end Alembic commands ### diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..9584cde 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -1,10 +1,11 @@ from datetime import date -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, computed_field class AnimalCreate(BaseModel): animal_type: str name: str + breed: str birth_date: date @@ -14,4 +15,10 @@ class AnimalResponse(BaseModel): id: int animal_type: str name: str + breed: str birth_date: date + + @computed_field + #@property + def age(self) -> int: + return date.today().year - self.birth_date.year diff --git a/models/sqlalchemy/__init__.py b/models/sqlalchemy/__init__.py index 4fee0e2..722174c 100644 --- a/models/sqlalchemy/__init__.py +++ b/models/sqlalchemy/__init__.py @@ -5,4 +5,6 @@ class Animal(db.Model): id = db.Column(db.Integer, primary_key=True) animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) + breed = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + #age = db.Column(db.Integer, nullable=False) diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..2ecb98c 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -8,4 +8,6 @@ class Animal(db.Model): id = db.Column(db.Integer, primary_key=True) animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) + breed = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + #age = db.Column(db.Integer, nullable=True) diff --git a/templates/home.html b/templates/home.html index 80b942e..3c9130d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -29,6 +29,10 @@

Animals in the Shelter

+
+ + +
@@ -45,7 +49,9 @@

List of animals:

Animal Type Name + Animal Breed Birth Date + Age Actions @@ -76,10 +82,18 @@
+
+ + +
+
+ + +