diff --git a/app.py b/app.py index 11e2013..f2b16e4 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,9 @@ db = init_db(app) +@app.route('/health') +def health(): + return Response(status=200) @app.route('/') def home() -> str: @@ -27,9 +30,11 @@ def index() -> Response: def add_animal() -> tuple[Response, int]: data = AnimalCreate(**request.get_json()) new_animal = Animal( - animal_type=data.animal_type, + type=data.type, name=data.name, - birth_date=data.birth_date + birth_date=data.birth_date, + breed=data.breed, + image_url=data.image_url, ) db.session.add(new_animal) db.session.commit() @@ -45,12 +50,15 @@ def add_animal() -> tuple[Response, int]: def update_animal(pk: int) -> Union[Response, tuple[Response, int]]: data = AnimalCreate(**request.get_json()) animal = Animal.query.get(pk) + if not animal: return jsonify({"message": "Animal not found"}), 404 - animal.animal_type = data.animal_type + animal.type = data.type animal.name = data.name animal.birth_date = data.birth_date + animal.breed = data.breed + animal.image_url = data.image_url db.session.commit() return jsonify( { diff --git a/migrations/versions/e08fc0218f8b_.py b/migrations/versions/e08fc0218f8b_.py index 9a2228e..72960d7 100644 --- a/migrations/versions/e08fc0218f8b_.py +++ b/migrations/versions/e08fc0218f8b_.py @@ -20,8 +20,10 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('animal', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('animal_type', sa.String(), nullable=False), + sa.Column('type', sa.String(), nullable=False), sa.Column('name', sa.String(), nullable=False), + sa.Column('breed', sa.String(), nullable=False), + sa.Column('image_url', sa.String(), nullable=False), sa.Column('birth_date', sa.Date(), nullable=False), sa.PrimaryKeyConstraint('id') ) diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..1648ad6 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -1,17 +1,35 @@ from datetime import date -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, computed_field class AnimalCreate(BaseModel): - animal_type: str + type: str name: str birth_date: date + breed: str + image_url: str + @computed_field + @property + def age(self) -> str: + today = date.today() + years = today.year - self.birth_date.year + months = today.month - self.birth_date.month + days_diff = today.day - self.birth_date.day -class AnimalResponse(BaseModel): + if days_diff < 0: + months -= 1 + + if months < 0: + years -= 1 + months += 12 + + if years <= 0: + return f"{months} month{'s' if months != 1 else ''}" + return f"{years} year{'s' if years != 1 else ''} {months} month{'s' if months != 1 else ''}" + + +class AnimalResponse(AnimalCreate): model_config = ConfigDict(from_attributes=True) id: int - animal_type: str - name: str - birth_date: date diff --git a/models/sqlalchemy/__init__.py b/models/sqlalchemy/__init__.py index 4fee0e2..314f619 100644 --- a/models/sqlalchemy/__init__.py +++ b/models/sqlalchemy/__init__.py @@ -3,6 +3,6 @@ class Animal(db.Model): id = db.Column(db.Integer, primary_key=True) - animal_type = db.Column(db.String, nullable=False) + type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..015e79b 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -6,6 +6,8 @@ class Animal(db.Model): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) - animal_type = db.Column(db.String, nullable=False) + type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + breed = db.Column(db.String, nullable=False) + image_url = db.Column(db.String, nullable=False) diff --git a/templates/home.html b/templates/home.html index 80b942e..c28045e 100644 --- a/templates/home.html +++ b/templates/home.html @@ -22,13 +22,21 @@

Animals in the Shelter

- - + +
+
+ + +
+
+ + +
@@ -45,6 +53,8 @@

List of animals:

Animal Type Name + Breed + Image Url Birth Date Actions @@ -76,6 +86,14 @@
+
+ + +
+
+ + +
@@ -97,8 +115,10 @@ // This assumes you have a backend route to get details of a single animal. $.getJSON(`/animal/${id}`, function(data) { $('#editAnimalId').val(data.animal.id); - $('#editAnimalType').val(data.animal.animal_type); + $('#editAnimalType').val(data.animal.type); $('#editAnimalName').val(data.animal.name); + $('#editAnimalBreed').val(data.animal.breed); + $('#editAnimalImageUrl').val(data.animal.image_url); $('#editAnimalBirthDate').val(data.animal.birth_date); $('#editAnimalModal').modal('show'); }); @@ -106,9 +126,11 @@ function submitEditAnimal() { const updatedAnimal = { - animal_type: $('#editAnimalType').val(), + type: $('#editAnimalType').val(), name: $('#editAnimalName').val(), - birth_date: $('#editAnimalBirthDate').val() + birth_date: $('#editAnimalBirthDate').val(), + breed: $('#editAnimalBreed').val(), + image_url: $('#editAnimalImageUrl').val(), }; const animalId = $('#editAnimalId').val(); @@ -131,12 +153,13 @@ $.getJSON('/animals', function(data) { $('#animalsList').empty(); data.animals.forEach(animal => { - console.log(animal) $('#animalsList').append(` - ${animal.animal_type} + ${animal.type} ${animal.name} - ${animal.birth_date} + ${animal.breed} + + ${animal.age} @@ -151,8 +174,10 @@ $('#addAnimalForm').on('submit', function(e) { e.preventDefault(); const newAnimal = { - animal_type: $('#animal_type').val(), + type: $('#type').val(), name: $('#name').val(), + breed: $('#breed').val(), + image_url: $('#image_url').val(), birth_date: $('#birth_date').val() }; $.ajax({