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
25 changes: 23 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from settings import settings
from database import init_db
from models.sqlalchemy.models import Animal

import requests
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = settings.sqlalchemy_database_uri

Expand All @@ -29,7 +29,9 @@ def add_animal() -> tuple[Response, int]:
new_animal = Animal(
animal_type=data.animal_type,
name=data.name,
birth_date=data.birth_date
birth_date=data.birth_date,
breed=data.breed,
photo_url=data.photo_url
)
db.session.add(new_animal)
db.session.commit()
Expand All @@ -51,6 +53,8 @@ def update_animal(pk: int) -> Union[Response, tuple[Response, int]]:
animal.animal_type = data.animal_type
animal.name = data.name
animal.birth_date = data.birth_date
animal.breed = data.breed
animal.photo_url = data.photo_url
db.session.commit()
return jsonify(
{
Expand All @@ -72,6 +76,23 @@ def retrieve_animal(pk: int) -> Union[Response, tuple[Response, int]]:
}
)

@app.route('/health', methods=['GET'])
def health_check():
return jsonify({"status": "ok"}), 200

def get_dog_photo_url(breed: str) -> str:
response = requests.get(f"https://dog.ceo/api/breed/{breed}/images/random")
if response.status_code == 200:
return response.json().get('message')
return ""

def get_cat_photo_url() -> str:
response = requests.get("https://api.thecatapi.com/v1/images/search")
if response.status_code == 200:
data = response.json()
if data and isinstance(data, list):
return data[0].get('url', "")
return ""

@app.route('/animal/<int:pk>', methods=['DELETE'])
def delete_animal(pk: int) -> Union[Response, tuple[Response, int]]:
Expand Down
34 changes: 34 additions & 0 deletions migrations/versions/41d2772a113d_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""empty message

Revision ID: 41d2772a113d
Revises: e08fc0218f8b
Create Date: 2025-06-09 00:45:28.796915

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '41d2772a113d'
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=True))
batch_op.add_column(sa.Column('photo_url', sa.String(), nullable=True))

# ### 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('photo_url')
batch_op.drop_column('breed')

# ### end Alembic commands ###
19 changes: 18 additions & 1 deletion models/pydantic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class AnimalCreate(BaseModel):
animal_type: str
name: str
birth_date: date

breed : str
photo_url : str

class AnimalResponse(BaseModel):
model_config = ConfigDict(from_attributes=True)
Expand All @@ -15,3 +16,19 @@ class AnimalResponse(BaseModel):
animal_type: str
name: str
birth_date: date
breed : str
photo_url : str

@property
def age(self) -> int:
today = date.today()
age = today.year - self.birth_date.year

if (today.month, today.day) < (self.birth_date.month, self.birth_date.day):
age = age - 1
return age

def model_dump(self, *args, **kwargs):
data = super().model_dump(*args, **kwargs)
data['age'] = self.age
return data
2 changes: 2 additions & 0 deletions models/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ class Animal(db.Model):
animal_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=True)
photo_url = db.Column(db.String, nullable=True)
2 changes: 2 additions & 0 deletions models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ class Animal(db.Model):
animal_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=True)
photo_url = db.Column(db.String, nullable=True)
Loading