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
10 changes: 8 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
def home() -> str:
return render_template('home.html')

@app.route('/health')
def health_check():
return '', 200

@app.route('/animals', methods=['GET'])
def index() -> Response:
Expand All @@ -28,8 +31,10 @@ def add_animal() -> tuple[Response, int]:
data = AnimalCreate(**request.get_json())
new_animal = Animal(
animal_type=data.animal_type,
animal_breed=data.animal_breed,
name=data.name,
birth_date=data.birth_date
birth_date=data.birth_date,
photo_url=data.photo_url,
)
db.session.add(new_animal)
db.session.commit()
Expand All @@ -40,7 +45,6 @@ def add_animal() -> tuple[Response, int]:
}
), 201


@app.route('/animal/<int:pk>', methods=['PUT'])
def update_animal(pk: int) -> Union[Response, tuple[Response, int]]:
data = AnimalCreate(**request.get_json())
Expand All @@ -49,8 +53,10 @@ def update_animal(pk: int) -> Union[Response, tuple[Response, int]]:
return jsonify({"message": "Animal not found"}), 404

animal.animal_type = data.animal_type
animal.breed = data.animal_breed,
animal.name = data.name
animal.birth_date = data.birth_date
animal.photo_url = data.photo_url
db.session.commit()
return jsonify(
{
Expand Down
2 changes: 2 additions & 0 deletions migrations/versions/e08fc0218f8b_.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def upgrade():
op.create_table('animal',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('animal_type', sa.String(), nullable=False),
sa.Column('animal_breed', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('photo_url', sa.String(), nullable=False),
sa.Column('birth_date', sa.Date(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
Expand Down
4 changes: 4 additions & 0 deletions models/pydantic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class AnimalCreate(BaseModel):
animal_type: str
animal_breed: str
name: str
photo_url: str
birth_date: date


Expand All @@ -13,5 +15,7 @@ class AnimalResponse(BaseModel):

id: int
animal_type: str
animal_breed: str
name: str
photo_url: str
birth_date: date
2 changes: 2 additions & 0 deletions models/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
class Animal(db.Model):
id = db.Column(db.Integer, primary_key=True)
animal_type = db.Column(db.String, nullable=False)
animal_breed = db.Column(db.String, nullable=False)
name = db.Column(db.String, nullable=False)
birth_date = db.Column(db.Date, nullable=False)
photo_url = db.Column(db.String, nullable=False)
2 changes: 2 additions & 0 deletions models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ class Animal(db.Model):

id = db.Column(db.Integer, primary_key=True)
animal_type = db.Column(db.String, nullable=False)
animal_breed = db.Column(db.String, nullable=False)
name = db.Column(db.String, nullable=False)
birth_date = db.Column(db.Date, nullable=False)
photo_url = db.Column(db.String, nullable=False)
43 changes: 39 additions & 4 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ <h2>Animals in the Shelter</h2>
<label for="animal_type">Animal Type:</label>
<input type="text" class="form-control" id="animal_type" placeholder="Animal Type" required>
</div>
<div class="form-group">
<label for="animal_breed">Animal Breed:</label>
<input type="text" class="form-control" id="animal_breed" placeholder="Animal Breed" required>
</div>
<div class="form-group">
<label for="name">Animal Name:</label>
<input type="text" class="form-control" id="name" placeholder="Animal Name" required>
Expand All @@ -33,6 +37,10 @@ <h2>Animals in the Shelter</h2>
<label for="birth_date">Birth Date:</label>
<input type="date" class="form-control" id="birth_date" required>
</div>
<div class="form-group">
<label for="photo_url">Photo URL:</label>
<input type="text" class="form-control" id="photo_url" placeholder="Photo URL">
</div>
<button type="submit" class="btn btn-primary">Add Animal</button>
</form>
</div>
Expand All @@ -44,8 +52,11 @@ <h4 class="mt-4">List of animals:</h4>
<thead>
<tr>
<th>Animal Type</th>
<th>Animal Breed</th>
<th>Name</th>
<th>Birth Date</th>
<th>Age</th>
<th>Photo</th>
<th>Actions</th>
</tr>
</thead>
Expand All @@ -72,6 +83,10 @@ <h5 class="modal-title">Edit Animal</h5>
<label for="editAnimalType">Animal Type:</label>
<input type="text" class="form-control" id="editAnimalType" required>
</div>
<div class="form-group">
<label for="editAnimalBreed">Animal Breed:</label>
<input type="text" class="form-control" id="editAnimalBreed" required>
</div>
<div class="form-group">
<label for="editAnimalName">Name:</label>
<input type="text" class="form-control" id="editAnimalName" required>
Expand All @@ -80,6 +95,10 @@ <h5 class="modal-title">Edit Animal</h5>
<label for="editAnimalBirthDate">Birth Date:</label>
<input type="date" class="form-control" id="editAnimalBirthDate" required>
</div>
<div class="form-group">
<label for="editAnimalPhotoUrl">Photo URL:</label>
<input type="text" class="form-control" id="editAnimalPhotoUrl" placeholder="Photo URL">
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -98,17 +117,21 @@ <h5 class="modal-title">Edit Animal</h5>
$.getJSON(`/animal/${id}`, function(data) {
$('#editAnimalId').val(data.animal.id);
$('#editAnimalType').val(data.animal.animal_type);
$('#editAnimalBreed').val(data.animal.animal_breed);
$('#editAnimalName').val(data.animal.name);
$('#editAnimalBirthDate').val(data.animal.birth_date);
$('#editAnimalPhotoUrl').val(data.animal.photo_url);
$('#editAnimalModal').modal('show');
});
}

function submitEditAnimal() {
const updatedAnimal = {
animal_type: $('#editAnimalType').val(),
animal_breed: $('#editAnimalBreed').val(),
name: $('#editAnimalName').val(),
birth_date: $('#editAnimalBirthDate').val()
birth_date: $('#editAnimalBirthDate').val(),
photo_url: $('#editAnimalPhotoUrl').val()
};

const animalId = $('#editAnimalId').val();
Expand All @@ -131,12 +154,15 @@ <h5 class="modal-title">Edit Animal</h5>
$.getJSON('/animals', function(data) {
$('#animalsList').empty();
data.animals.forEach(animal => {
console.log(animal)
const age = calculateAge(new Date(animal.birth_date));
$('#animalsList').append(`
<tr>
<td>${animal.animal_type}</td>
<td>${animal.animal_breed}</td>
<td>${animal.name}</td>
<td>${animal.birth_date}</td>
<td>${age}</td>
<td><img src="${animal.photo_url}" alt="Animal Photo" style="max-width: 100px;"></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 @@ -147,13 +173,23 @@ <h5 class="modal-title">Edit Animal</h5>
});
}

// Function to calculate age from birth date
function calculateAge(birthDate) {
const now = new Date();
const diff = now.getTime() - birthDate.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}

// Add new animal
$('#addAnimalForm').on('submit', function(e) {
e.preventDefault();
const newAnimal = {
animal_type: $('#animal_type').val(),
animal_breed: $('#animal_breed').val(),
name: $('#name').val(),
birth_date: $('#birth_date').val()
birth_date: $('#birth_date').val(),
photo_url: $('#photo_url').val()
};
$.ajax({
url: '/animal',
Expand Down Expand Up @@ -183,7 +219,6 @@ <h5 class="modal-title">Edit Animal</h5>
// Initial fetch
fetchAnimals();


</script>
</body>
</html>