feat: Refactor API structure and implement core functionalities
- Updated requirements to include asyncpg, pyyaml, aiofiles, and httpx. - Modified schema.sql to add 'tipo_pasto' column in 'pasti' table. - Created .gitignore file to exclude unnecessary files and directories. - Added README.md for API implementation details and directory structure. - Introduced config.yaml for centralized configuration management. - Implemented core modules for database management, authentication, and exception handling. - Developed models for 'pietanze', 'pasti', and 'prenotazioni' with validation. - Created routes for CRUD operations on 'pietanze' with pagination and filtering. - Established logging and error handling mechanisms throughout the application. - Set up FastAPI application with CORS and health check endpoint.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import asyncpg
|
||||
import yaml
|
||||
from typing import Optional
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DatabaseManager:
|
||||
def __init__(self):
|
||||
self.pool: Optional[asyncpg.Pool] = None
|
||||
|
||||
async def initialize(self, config: dict):
|
||||
"""Initialize database connection pool"""
|
||||
try:
|
||||
self.pool = await asyncpg.create_pool(
|
||||
host=config['host'],
|
||||
port=config['port'],
|
||||
database=config['name'],
|
||||
user=config['user'],
|
||||
password=config['password'],
|
||||
min_size=config.get('pool_min_size', 5),
|
||||
max_size=config.get('pool_max_size', 20),
|
||||
max_queries=config.get('pool_max_queries', 50000),
|
||||
max_inactive_connection_lifetime=config.get('pool_max_inactive_connection_lifetime', 300.0)
|
||||
)
|
||||
logger.info("Database pool initialized successfully")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize database pool: {e}")
|
||||
raise
|
||||
|
||||
async def close(self):
|
||||
"""Close database connection pool"""
|
||||
if self.pool:
|
||||
await self.pool.close()
|
||||
logger.info("Database pool closed")
|
||||
|
||||
async def execute_query(self, query: str, *args):
|
||||
"""Execute a query and return results"""
|
||||
async with self.pool.acquire() as connection:
|
||||
return await connection.fetch(query, *args)
|
||||
|
||||
async def execute_one(self, query: str, *args):
|
||||
"""Execute a query and return single result"""
|
||||
async with self.pool.acquire() as connection:
|
||||
return await connection.fetchrow(query, *args)
|
||||
|
||||
async def execute_command(self, query: str, *args):
|
||||
"""Execute a command (INSERT, UPDATE, DELETE)"""
|
||||
async with self.pool.acquire() as connection:
|
||||
return await connection.execute(query, *args)
|
||||
|
||||
# Global database manager instance
|
||||
db_manager = DatabaseManager()
|
||||
|
||||
async def get_database():
|
||||
"""Dependency for getting database connection"""
|
||||
return db_manager
|
||||
Reference in New Issue
Block a user