-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
168 lines (146 loc) · 5.85 KB
/
Copy pathdatabase.py
File metadata and controls
168 lines (146 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
MongoDB database configuration and connection management.
"""
import os
from dotenv import load_dotenv
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.errors import ConnectionFailure
load_dotenv()
class Database:
"""Database connection manager"""
client: AsyncIOMotorClient = None
db = None
db = Database()
async def connect_to_mongo():
"""Establish connection to MongoDB"""
mongodb_uri = os.getenv("MONGODB_URI", "mongodb://localhost:27017")
database_name = os.getenv("MONGODB_DATABASE", "petshop")
try:
db.client = AsyncIOMotorClient(mongodb_uri)
db.db = db.client[database_name]
# Test the connection
await db.client.admin.command("ping")
print(f"✓ Connected to MongoDB: {database_name}")
# Initialize collections with sample data if empty
await initialize_sample_data()
except ConnectionFailure as e:
print(f"✗ Failed to connect to MongoDB: {e}")
raise
async def close_mongo_connection():
"""Close MongoDB connection"""
if db.client:
db.client.close()
print("✓ Closed MongoDB connection")
async def get_database():
"""Get database instance"""
return db.db
async def initialize_sample_data():
"""Initialize the database with sample pet data if empty"""
pets_collection = db.db.pets
# Check if pets collection is empty
count = await pets_collection.count_documents({})
if count == 0:
sample_pets = [
{
"id": "pet001",
"name": "Golden Retriever Puppy",
"type": "dog",
"description": "Friendly and energetic Golden Retriever puppy, great with families and children. Well-socialized and vaccinated.",
"price": 1200.00,
"age_months": 3,
"available": True,
"image_url": "https://example.com/golden-retriever.jpg",
},
{
"id": "pet002",
"name": "British Shorthair Kitten",
"type": "cat",
"description": "Adorable British Shorthair kitten with beautiful blue-gray coat. Calm and affectionate temperament.",
"price": 800.00,
"age_months": 2,
"available": True,
"image_url": "https://example.com/british-shorthair.jpg",
},
{
"id": "pet003",
"name": "Beagle",
"type": "dog",
"description": "Playful Beagle with excellent hunting instincts. Friendly, curious, and great for active families.",
"price": 950.00,
"age_months": 6,
"available": True,
"image_url": "https://example.com/beagle.jpg",
},
{
"id": "pet004",
"name": "Siamese Cat",
"type": "cat",
"description": "Elegant Siamese cat with striking blue eyes. Vocal, intelligent, and social personality.",
"price": 650.00,
"age_months": 8,
"available": True,
"image_url": "https://example.com/siamese.jpg",
},
{
"id": "pet005",
"name": "Cockatiel",
"type": "bird",
"description": "Hand-tamed Cockatiel with yellow crest. Whistles and mimics sounds, very social bird.",
"price": 150.00,
"age_months": 4,
"available": True,
"image_url": "https://example.com/cockatiel.jpg",
},
{
"id": "pet006",
"name": "Betta Fish",
"type": "fish",
"description": "Beautiful Betta fish with vibrant blue and red coloring. Low maintenance and stunning to watch.",
"price": 25.00,
"age_months": 6,
"available": True,
"image_url": "https://example.com/betta.jpg",
},
{
"id": "pet007",
"name": "Holland Lop Rabbit",
"type": "rabbit",
"description": "Sweet Holland Lop rabbit with soft fur and floppy ears. Gentle and perfect for families.",
"price": 300.00,
"age_months": 5,
"available": True,
"image_url": "https://example.com/holland-lop.jpg",
},
{
"id": "pet008",
"name": "Syrian Hamster",
"type": "hamster",
"description": "Friendly Syrian hamster with golden brown fur. Active and entertaining, great starter pet.",
"price": 45.00,
"age_months": 2,
"available": True,
"image_url": "https://example.com/hamster.jpg",
},
{
"id": "pet009",
"name": "German Shepherd Puppy",
"type": "dog",
"description": "Intelligent German Shepherd puppy with excellent temperament. Loyal, trainable, and protective.",
"price": 1500.00,
"age_months": 4,
"available": True,
"image_url": "https://example.com/german-shepherd.jpg",
},
{
"id": "pet010",
"name": "Parakeet Pair",
"type": "bird",
"description": "Bonded pair of colorful parakeets (blue and green). Social, playful, and chatter together.",
"price": 80.00,
"age_months": 7,
"available": True,
"image_url": "https://example.com/parakeets.jpg",
},
]
await pets_collection.insert_many(sample_pets)
print(f"✓ Initialized database with {len(sample_pets)} sample pets")