Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pdr21001 authored Apr 23, 2025
1 parent 5915be0 commit ef0303b
Showing 1 changed file with 13 additions and 104 deletions.
117 changes: 13 additions & 104 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from flasgger import Swagger
import sqlite3
import os
from backend.pets import add_pet, update_pet, delete_pet, search_pets, reset_pets

# Initialize Flask app
app = Flask(__name__)
Expand All @@ -15,6 +14,7 @@

# Helper function to get a database connection
def get_db():
"""Establish a connection to the SQLite database."""
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row # Allows row access by column name
return conn
Expand Down Expand Up @@ -55,40 +55,14 @@ def init_db():
INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', pets)

conn.commit()
conn.close()

# API Routes
@app.route('/api/pets', methods=['GET'])
def get_pets():
"""Retrieve all pets
---
responses:
200:
description: A list of pets
schema:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string
species:
type: string
breed:
type: string
age:
type: integer
personality:
type: string
image_path:
type: string
adoption_status:
type: string
"""
"""Retrieve all pets"""
try:
conn = get_db()
cursor = conn.cursor()
Expand All @@ -100,53 +74,18 @@ def get_pets():
conn.close()

return jsonify(pet_list), 200
except Exception as e:
except sqlite3.Error as e:
print(f"An error occurred: {e}")
return jsonify({'error': 'Internal Server Error'}), 500

@app.route('/', methods=['GET'])
def home():
"""Home route to welcome users to the API."""
return jsonify({'message': 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'})



@app.route('/api/pets/<int:pet_id>', methods=['GET'])
def get_pet(pet_id):
"""Retrieve a single pet by ID
---
parameters:
- name: pet_id
in: path
type: integer
required: true
description: The unique ID of the pet
responses:
200:
description: A pet
schema:
type: object
properties:
id:
type: integer
name:
type: string
species:
type: string
breed:
type: string
age:
type: integer
personality:
type: string
image_path:
type: string
adoption_status:
type: string
404:
description: Pet not found
500:
description: Internal Server Error
"""
"""Retrieve a single pet by ID"""
try:
conn = get_db()
cursor = conn.cursor()
Expand All @@ -161,43 +100,13 @@ def get_pet(pet_id):
conn.close()

return jsonify(pet_dict), 200
except Exception as e:
except sqlite3.Error as e:
print(f"An error occurred: {e}")
return jsonify({'error': 'Internal Server Error'}), 500

@app.route('/api/pets', methods=['POST'])
def add_pet():
"""Add a new pet
---
parameters:
- name: pet
in: body
required: true
schema:
type: object
properties:
name:
type: string
species:
type: string
breed:
type: string
age:
type: integer
personality:
type: string
image_path:
type: string
adoption_status:
type: string
responses:
201:
description: Pet created successfully
400:
description: Invalid pet data
500:
description: Internal Server Error
"""
"""Add a new pet"""
try:
pet_data = request.get_json()

Expand All @@ -209,26 +118,26 @@ def add_pet():

conn = get_db()
cursor = conn.cursor()

cursor.execute('''
INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (pet_data['name'], pet_data['species'], pet_data['breed'], pet_data['age'],
''', (pet_data['name'], pet_data['species'], pet_data['breed'], pet_data['age'],
pet_data['personality'], pet_data.get('image_path', '/images/default.jpg'), 'Available'))

conn.commit()
conn.close()

return jsonify({'message': 'Pet created successfully'}), 201
except Exception as e:
except sqlite3.Error as e:
print(f"An error occurred: {e}")
return jsonify({'error': 'Internal Server Error'}), 500

# Initialize database on first run
@app.before_first_request
def before_first_request():
"""Initialize the database before the first request."""
init_db()

if __name__ == "__main__":
app.run(debug=True, use_reloader=False)

0 comments on commit ef0303b

Please sign in to comment.