|
| 1 | +from fastapi import Depends, HTTPException, status |
| 2 | +from motor.motor_asyncio import AsyncIOMotorDatabase |
| 3 | + |
| 4 | +from app.database import get_db |
| 5 | +from app.schemas import Settings |
| 6 | + |
| 7 | + |
| 8 | +def get_setting_repository(db: AsyncIOMotorDatabase = Depends(get_db)): |
| 9 | + """ |
| 10 | + Restituisce il repository della collection settings. |
| 11 | + """ |
| 12 | + return SettingRepository(db) |
| 13 | + |
| 14 | + |
| 15 | +class SettingRepository: |
| 16 | + def __init__(self, database): |
| 17 | + self.database = database |
| 18 | + self.collection = database.get_collection("settings") |
| 19 | + |
| 20 | + print("SettingRepository initialization") |
| 21 | + |
| 22 | + insert_payload = { |
| 23 | + "color_primary": "#5e5c64", |
| 24 | + "color_primary_hover": "#44424a", |
| 25 | + "color_primary_text": "white", |
| 26 | + "message_history": 100, |
| 27 | + } |
| 28 | + self.collection.update_one( |
| 29 | + {"_id": "main"}, |
| 30 | + { |
| 31 | + "$setOnInsert": insert_payload, |
| 32 | + }, |
| 33 | + upsert=True, |
| 34 | + ) |
| 35 | + |
| 36 | + async def get_settings(self): |
| 37 | + """ |
| 38 | + Restituisce le impostazioni dell'applicazione. |
| 39 | + """ |
| 40 | + return await self.collection.find_one({"_id": "main"}) |
| 41 | + |
| 42 | + async def update_settings(self, settings: Settings): |
| 43 | + """ |
| 44 | + Aggiorna le impostazioni dell'applicazione. |
| 45 | + """ |
| 46 | + try: |
| 47 | + current_settings = await self.get_settings() |
| 48 | + |
| 49 | + if not current_settings: |
| 50 | + raise HTTPException( |
| 51 | + status_code=status.HTTP_404_NOT_FOUND, |
| 52 | + detail="Settings not found", |
| 53 | + ) |
| 54 | + |
| 55 | + # Controlla se le impostazioni sono state modificate |
| 56 | + if ( |
| 57 | + current_settings.get("color_primary") == settings.color_primary |
| 58 | + and current_settings.get("color_primary_hover") |
| 59 | + == settings.color_primary_hover |
| 60 | + and current_settings.get("color_primary_text") |
| 61 | + == settings.color_primary_text |
| 62 | + and current_settings.get("message_history") == settings.message_history |
| 63 | + ): |
| 64 | + print("Settings data is already up to date.") |
| 65 | + raise HTTPException( |
| 66 | + status_code=status.HTTP_304_NOT_MODIFIED, |
| 67 | + detail="Settings data is already up to date.", |
| 68 | + ) |
| 69 | + |
| 70 | + # Prepara il payload di aggiornamento |
| 71 | + update_payload = { |
| 72 | + "color_primary": ( |
| 73 | + settings.color_primary |
| 74 | + if settings.color_primary |
| 75 | + else current_settings.get("color_primary") |
| 76 | + ), |
| 77 | + "color_primary_hover": ( |
| 78 | + settings.color_primary_hover |
| 79 | + if settings.color_primary_hover |
| 80 | + else current_settings.get("color_primary_hover") |
| 81 | + ), |
| 82 | + "color_primary_text": ( |
| 83 | + settings.color_primary_text |
| 84 | + if settings.color_primary_text |
| 85 | + else current_settings.get("color_primary_text") |
| 86 | + ), |
| 87 | + "message_history": ( |
| 88 | + settings.message_history |
| 89 | + if settings.message_history |
| 90 | + else current_settings.get("message_history") |
| 91 | + ), |
| 92 | + } |
| 93 | + |
| 94 | + result = await self.collection.update_one( |
| 95 | + {"_id": "main"}, |
| 96 | + { |
| 97 | + "$set": update_payload, |
| 98 | + }, |
| 99 | + ) |
| 100 | + |
| 101 | + # Controlla se l'aggiornamento ha avuto effetto |
| 102 | + if result.matched_count == 0: |
| 103 | + raise HTTPException( |
| 104 | + status_code=status.HTTP_404_NOT_FOUND, |
| 105 | + detail="Settings not found during update attempt", |
| 106 | + ) |
| 107 | + except Exception as e: |
| 108 | + print(f"Error updating settings: {e}") |
| 109 | + raise Exception(f"Error updating settings: {e}") |
0 commit comments