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
14 changes: 13 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

db = init_db(app)

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

@app.route('/')
def home() -> str:
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -92,3 +103,4 @@ def initialize_app():
if __name__ == '__main__':
initialize_app()
app.run(debug=True)

4 changes: 2 additions & 2 deletions database.py
Original file line number Diff line number Diff line change
@@ -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()


Expand Down
32 changes: 32 additions & 0 deletions migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py
Original file line number Diff line number Diff line change
@@ -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 ###
9 changes: 8 additions & 1 deletion models/pydantic/models.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
2 changes: 2 additions & 0 deletions models/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ <h2>Animals in the Shelter</h2>
<label for="name">Animal Name:</label>
<input type="text" class="form-control" id="name" placeholder="Animal Name" required>
</div>
<div class="form-group">
<label for="breed">Animal Breed:</label>
<input type="text" class="form-control" id="breed" placeholder="Animal Breed" required>
</div>
<div class="form-group">
<label for="birth_date">Birth Date:</label>
<input type="date" class="form-control" id="birth_date" required>
Expand All @@ -45,7 +49,9 @@ <h4 class="mt-4">List of animals:</h4>
<tr>
<th>Animal Type</th>
<th>Name</th>
<th>Animal Breed</th>
<th>Birth Date</th>
<th>Age</th>
<th>Actions</th>
</tr>
</thead>
Expand Down Expand Up @@ -76,10 +82,18 @@ <h5 class="modal-title">Edit Animal</h5>
<label for="editAnimalName">Name:</label>
<input type="text" class="form-control" id="editAnimalName" required>
</div>
<div class="form-group">
<label for="editAnimalBreed">Breed:</label>
<input type="text" class="form-control" id="editAnimalBreed" required>
</div>
<div class="form-group">
<label for="editAnimalBirthDate">Birth Date:</label>
<input type="date" class="form-control" id="editAnimalBirthDate" required>
</div>
<div class="form-group">
<label for="editAnimalAge">Age:</label>
<input type="number" class="form-control" id="editAnimalAge" readonly>
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -99,7 +113,9 @@ <h5 class="modal-title">Edit Animal</h5>
$('#editAnimalId').val(data.animal.id);
$('#editAnimalType').val(data.animal.animal_type);
$('#editAnimalName').val(data.animal.name);
$('#editAnimalBreed').val(data.animal.breed);
$('#editAnimalBirthDate').val(data.animal.birth_date);
$('#editAnimalAge').val(data.animal.age);
$('#editAnimalModal').modal('show');
});
}
Expand All @@ -108,6 +124,8 @@ <h5 class="modal-title">Edit Animal</h5>
const updatedAnimal = {
animal_type: $('#editAnimalType').val(),
name: $('#editAnimalName').val(),
breed: $('#editAnimalBreed').val(),
age: $('#editAnimalAge').val(),
birth_date: $('#editAnimalBirthDate').val()
};

Expand Down Expand Up @@ -136,7 +154,9 @@ <h5 class="modal-title">Edit Animal</h5>
<tr>
<td>${animal.animal_type}</td>
<td>${animal.name}</td>
<td>${animal.breed}</td>
<td>${animal.birth_date}</td>
<td>${animal.age}</td>
<td>
<button class="btn btn-warning btn-sm" onclick="updateAnimal(${animal.id})">Update</button>
<button class="btn btn-danger btn-sm" onclick="deleteAnimal(${animal.id})">Delete</button>
Expand All @@ -153,6 +173,7 @@ <h5 class="modal-title">Edit Animal</h5>
const newAnimal = {
animal_type: $('#animal_type').val(),
name: $('#name').val(),
breed: $('#breed').val(),
birth_date: $('#birth_date').val()
};
$.ajax({
Expand Down