Skip to content

Commit 8e935e8

Browse files
committed
Refactor API endpoint URLs to use relative paths and remove unused code in DogList and DogDetails components; simplify app.py by removing CORS and unnecessary request handling.
1 parent 5ecf1b3 commit 8e935e8

File tree

4 files changed

+12
-71
lines changed

4 files changed

+12
-71
lines changed

client/astro.config.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import node from '@astrojs/node';
88
// https://astro.build/config
99
export default defineConfig({
1010
vite: {
11-
plugins: [tailwindcss(), svelte()]
11+
plugins: [tailwindcss(), svelte()],
12+
server: {
13+
proxy: {
14+
'/api': {
15+
target: 'http://localhost:5100',
16+
changeOrigin: true,
17+
}
18+
}
19+
}
1220
},
1321

1422
adapter: node({

client/src/components/DogDetails.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// Otherwise fetch data using dogId
3131
if (dogId) {
3232
try {
33-
const response = await fetch(`http://localhost:5100/api/dogs/${dogId}`);
33+
const response = await fetch(`/api/dogs/${dogId}`);
3434
if (response.ok) {
3535
dogData = await response.json();
3636
} else {

client/src/components/DogList.svelte

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,14 @@
77
breed: string;
88
}
99
10-
interface Breed {
11-
id: number;
12-
name: string;
13-
}
14-
1510
export let dogs: Dog[] = [];
1611
let loading = true;
1712
let error: string | null = null;
18-
let breeds: Breed[] = [];
19-
let selectedBreed = '';
20-
let onlyAvailable = false;
2113
2214
async function fetchDogs() {
2315
loading = true;
2416
try {
25-
let url = 'http://localhost:5100/api/dogs?';
26-
if (selectedBreed) url += `breed=${encodeURIComponent(selectedBreed)}&`;
27-
if (onlyAvailable) url += 'status=available&';
28-
29-
const response = await fetch(url);
17+
const response = await fetch('/api/dogs');
3018
if(response.ok) {
3119
dogs = await response.json();
3220
} else {
@@ -39,55 +27,14 @@
3927
}
4028
}
4129
42-
async function fetchBreeds() {
43-
try {
44-
const response = await fetch('http://localhost:5100/api/breeds');
45-
if(response.ok) {
46-
breeds = await response.json();
47-
}
48-
} catch (err) {
49-
console.error('Failed to fetch breeds:', err);
50-
}
51-
}
52-
53-
$: {
54-
if (selectedBreed !== undefined || onlyAvailable !== undefined) {
55-
fetchDogs();
56-
}
57-
}
58-
5930
onMount(() => {
60-
fetchBreeds();
6131
fetchDogs();
6232
});
6333
</script>
6434

6535
<div>
6636
<h2 class="text-2xl font-medium mb-6 text-slate-100">Available Dogs</h2>
6737

68-
<!-- Filter controls -->
69-
<div class="mb-6 flex gap-4 items-center bg-slate-800/60 backdrop-blur-sm rounded-xl p-4 border border-slate-700/50">
70-
<div class="flex-1">
71-
<select
72-
bind:value={selectedBreed}
73-
class="w-full bg-slate-700 text-slate-100 rounded-lg px-3 py-2 border border-slate-600 focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
74-
>
75-
<option value="">All Breeds</option>
76-
{#each breeds as breed}
77-
<option value={breed.name}>{breed.name}</option>
78-
{/each}
79-
</select>
80-
</div>
81-
<label class="flex items-center gap-2 text-slate-100">
82-
<input
83-
type="checkbox"
84-
bind:checked={onlyAvailable}
85-
class="form-checkbox h-5 w-5 text-blue-500 rounded border-slate-600 bg-slate-700 focus:ring-blue-500"
86-
>
87-
Available Only
88-
</label>
89-
</div>
90-
9138
{#if loading}
9239
<!-- loading animation -->
9340
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">

server/app.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# filepath: server/app.py
22
import os
3-
from flask import Flask, jsonify, request
3+
from flask import Flask, jsonify
44
from models import init_db, db, Dog, Breed
5-
from flask_cors import CORS
65

76
# Get the server directory path
87
base_dir = os.path.abspath(os.path.dirname(__file__))
@@ -11,30 +10,17 @@
1110
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(base_dir, "dogshelter.db")}'
1211
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1312

14-
# Enable CORS for all routes with specific origin
15-
CORS(app, resources={r"/*": {"origins": "*"}})
16-
1713
# Initialize the database with the app
1814
init_db(app)
1915

2016
@app.route('/api/dogs', methods=['GET'])
2117
def get_dogs():
22-
# Start with base query
2318
query = db.session.query(
2419
Dog.id,
2520
Dog.name,
2621
Breed.name.label('breed')
2722
).join(Breed, Dog.breed_id == Breed.id)
2823

29-
# Apply filters based on query parameters
30-
breed = request.args.get('breed')
31-
if breed:
32-
query = query.filter(Breed.name == breed)
33-
34-
status = request.args.get('status')
35-
if status == 'available':
36-
query = query.filter(Dog.status == 'AVAILABLE')
37-
3824
dogs_query = query.all()
3925

4026
# Convert the result to a list of dictionaries

0 commit comments

Comments
 (0)