Loading app/controller/service.py +70 −32 Original line number Diff line number Diff line Loading @@ -15,6 +15,7 @@ from model.service.service import ( isServiceTranslationExists, updateService, updateServiceLogo, getLastPosition ) from model.language.language import isLanguageCodeExists Loading Loading @@ -72,6 +73,15 @@ def create_service(): try: data = request.get_json() translations = data.get("translations") position = None if "position" in data and isinstance(data.get("position"), int): position = data.get("position") elif "position" in data: return ( jsonify({"error": "if you provide position, it must be an integer"}), 400, ) if (not translations): return ( Loading @@ -96,6 +106,16 @@ def create_service(): 400, ) if "title" not in serviceTranslation: return ( jsonify( { "error": "you must provide a title for each translations" } ), 400, ) if "language_code" not in serviceTranslation: return ( jsonify( Loading Loading @@ -127,7 +147,12 @@ def create_service(): 400, ) if position is None: position = getLastPosition() + 1 print(position, translations ) service_id = insertService( position, translations ) Loading Loading @@ -162,17 +187,29 @@ def update_service(id): data = request.get_json() translations = data.get("translations") if (not translations): position = None if "position" in data and isinstance(data.get("position"), int): position = data.get("position") elif "position" in data: return ( jsonify({"error": "if you provide position, it must be an integer"}), 400, ) if (not translations and not position): return ( jsonify( { "error": "you must provide translations" "error": "you must provide translations or position" } ), 400, ) translationToUpdate: list = [] if translations: for serviceTranslation in translations: if "language_code" not in serviceTranslation: return ( Loading Loading @@ -213,6 +250,7 @@ def update_service(id): updatedService = updateService( id, position, translations ) Loading app/model/service/service.py +109 −39 Original line number Diff line number Diff line Loading @@ -26,10 +26,12 @@ def getServicesFull(): SELECT S.id, logo, position, json_agg( json_build_object( 'language_code', L.language_code, 'content', ST.content 'content', ST.content, 'title', ST.title ) ) AS translations FROM service S Loading Loading @@ -60,10 +62,12 @@ def getService(ServiceId: int) -> Any | Literal[-1] | Literal[False]: SELECT S.id, logo, position, json_agg( json_build_object( 'language_code', L.language_code, 'content', ST.content 'content', ST.content, 'title', ST.title ) ) AS translations FROM service S Loading @@ -89,25 +93,31 @@ def getService(ServiceId: int) -> Any | Literal[-1] | Literal[False]: return False def insertService( def insertService(position: int | None, translations: list[dict[str, str]], ) -> bool | int: InsertServiceQuery = """INSERT INTO service (logo) VALUES (NULL) RETURNING id;""" print("*******************************service create *******************************") try: with db_cursor() as (conn, cur): conn.execute("BEGIN") try: cur.execute(InsertServiceQuery) print(position, translations) cur.execute( "SELECT public.insertService(%s);", (position,), ) print("after exec") conn.commit() print("after commint") service_id = cur.fetchone()[0] print(service_id) for ServiceTranslation in translations: if not insertServiceTranslation( service_id, ServiceTranslation["language_code"], ServiceTranslation["content"] ServiceTranslation["content"], ServiceTranslation["title"] ): raise Exception( f"Database error while inserting service translation for {ServiceTranslation['language_code']}" Loading @@ -121,6 +131,15 @@ def insertService( raise e except Exception as e: error_details = traceback.format_exc() # Log detailed error message current_app.logger.error( f""" Database error while updating service translation: - Exception: {type(e).__name__} - {str(e)} - Traceback: {error_details}""" ) current_app.logger.error(f"Database error while inserting service: {e}") return False Loading @@ -128,13 +147,15 @@ def insertService( def insertServiceTranslation( serviceID: int, language_code: str, content: str content: str, title: str, ) -> bool | int: InsertServiceQuery = """INSERT INTO service_translation (service_id, language_id, content) VALUES (%s, (SELECT id FROM language WHERE language_code= %s ), %s);""" InsertServiceQuery = """INSERT INTO service_translation (service_id, language_id, content, title) VALUES (%s, (SELECT id FROM language WHERE language_code= %s ), %s, %s);""" content = None if content == "NULL" else content title = None if title == "NULL" else title try: with db_cursor() as (conn, cur): Loading @@ -144,6 +165,7 @@ def insertServiceTranslation( serviceID, language_code, content, title, ), ) conn.commit() Loading @@ -159,6 +181,7 @@ def updateServiceTranslation( serviceID: int, language_code: str, content: str, title: str, ): try: with db_cursor() as (conn, cur): Loading @@ -173,6 +196,13 @@ def updateServiceTranslation( update_fields.append("content = %s") update_values.append(content) if title is not None: if title == "NULL": update_fields.append("title = NULL") else: update_fields.append("title = %s") update_values.append(title) if update_fields: update_query = f"""UPDATE service_translation SET {', '.join(update_fields)} Loading @@ -194,9 +224,9 @@ def updateServiceTranslation( def deleteService(ServiceId: int) -> bool: print("deleteService with id: ", ServiceId ) # cascade delete on child tables records deleteServicesQuery = """DELETE FROM service WHERE id = %s""" deleteServicesQuery = """SELECT public.deleteService(%s)""" try: with db_cursor() as (conn, cur): Loading Loading @@ -228,22 +258,38 @@ def deleteServiceTranslation(ServiceId: int, language_code: str) -> bool: def updateService( id: int, position: int | None, translations: list[dict[str, str]], ): try: with db_cursor() as (conn, cur): conn.execute("BEGIN") try: if position is not None: cur.execute( "SELECT public.updateService(%s, %s)", (id, position), ) conn.commit() print("after exec update service") if translations: for serviceTranslation in translations: if "content" in serviceTranslation: if serviceTranslation["content"] is None: serviceTranslation["content"] = "NULL" if "title" in serviceTranslation: if serviceTranslation["title"] is None: serviceTranslation["title"] = "NULL" if isServiceTranslationExists( id, serviceTranslation["language_code"] ): if not updateServiceTranslation( id, serviceTranslation["language_code"], serviceTranslation["content"] serviceTranslation["content"], serviceTranslation["title"] ): raise Exception( f"Database error while updating service translation for {id} / {serviceTranslation['language_code']}" Loading @@ -251,15 +297,19 @@ def updateService( elif not insertServiceTranslation( id, serviceTranslation["language_code"], serviceTranslation["content"] serviceTranslation["content"], serviceTranslation["title"] ): raise Exception( f"Database error while inserting service translation for {id} / {serviceTranslation['language_code']}" ) conn.execute("COMMIT") return id # Returning the updated service ID return False # No update performed except Exception as e: conn.execute("ROLLBACK") raise e except Exception as e: # Capture full traceback for debugging Loading Loading @@ -314,3 +364,23 @@ def updateServiceLogo(serviceID: int, filename: str) -> bool: except Exception as e: current_app.logger.error(f"Database error while retrieving services: {e}") return False def getLastPosition() -> int: getLastPositionQuery = """SELECT MAX(s.position) FROM service s """ try: with db_cursor() as (conn, cur): cur.execute(getLastPositionQuery) lastPosition = cur.fetchone()[0] conn.commit() return lastPosition # Returning the updated keyword ID except Exception as e: current_app.logger.error( f"Database error while geting last service position: {e}" ) return False Loading
app/controller/service.py +70 −32 Original line number Diff line number Diff line Loading @@ -15,6 +15,7 @@ from model.service.service import ( isServiceTranslationExists, updateService, updateServiceLogo, getLastPosition ) from model.language.language import isLanguageCodeExists Loading Loading @@ -72,6 +73,15 @@ def create_service(): try: data = request.get_json() translations = data.get("translations") position = None if "position" in data and isinstance(data.get("position"), int): position = data.get("position") elif "position" in data: return ( jsonify({"error": "if you provide position, it must be an integer"}), 400, ) if (not translations): return ( Loading @@ -96,6 +106,16 @@ def create_service(): 400, ) if "title" not in serviceTranslation: return ( jsonify( { "error": "you must provide a title for each translations" } ), 400, ) if "language_code" not in serviceTranslation: return ( jsonify( Loading Loading @@ -127,7 +147,12 @@ def create_service(): 400, ) if position is None: position = getLastPosition() + 1 print(position, translations ) service_id = insertService( position, translations ) Loading Loading @@ -162,17 +187,29 @@ def update_service(id): data = request.get_json() translations = data.get("translations") if (not translations): position = None if "position" in data and isinstance(data.get("position"), int): position = data.get("position") elif "position" in data: return ( jsonify({"error": "if you provide position, it must be an integer"}), 400, ) if (not translations and not position): return ( jsonify( { "error": "you must provide translations" "error": "you must provide translations or position" } ), 400, ) translationToUpdate: list = [] if translations: for serviceTranslation in translations: if "language_code" not in serviceTranslation: return ( Loading Loading @@ -213,6 +250,7 @@ def update_service(id): updatedService = updateService( id, position, translations ) Loading
app/model/service/service.py +109 −39 Original line number Diff line number Diff line Loading @@ -26,10 +26,12 @@ def getServicesFull(): SELECT S.id, logo, position, json_agg( json_build_object( 'language_code', L.language_code, 'content', ST.content 'content', ST.content, 'title', ST.title ) ) AS translations FROM service S Loading Loading @@ -60,10 +62,12 @@ def getService(ServiceId: int) -> Any | Literal[-1] | Literal[False]: SELECT S.id, logo, position, json_agg( json_build_object( 'language_code', L.language_code, 'content', ST.content 'content', ST.content, 'title', ST.title ) ) AS translations FROM service S Loading @@ -89,25 +93,31 @@ def getService(ServiceId: int) -> Any | Literal[-1] | Literal[False]: return False def insertService( def insertService(position: int | None, translations: list[dict[str, str]], ) -> bool | int: InsertServiceQuery = """INSERT INTO service (logo) VALUES (NULL) RETURNING id;""" print("*******************************service create *******************************") try: with db_cursor() as (conn, cur): conn.execute("BEGIN") try: cur.execute(InsertServiceQuery) print(position, translations) cur.execute( "SELECT public.insertService(%s);", (position,), ) print("after exec") conn.commit() print("after commint") service_id = cur.fetchone()[0] print(service_id) for ServiceTranslation in translations: if not insertServiceTranslation( service_id, ServiceTranslation["language_code"], ServiceTranslation["content"] ServiceTranslation["content"], ServiceTranslation["title"] ): raise Exception( f"Database error while inserting service translation for {ServiceTranslation['language_code']}" Loading @@ -121,6 +131,15 @@ def insertService( raise e except Exception as e: error_details = traceback.format_exc() # Log detailed error message current_app.logger.error( f""" Database error while updating service translation: - Exception: {type(e).__name__} - {str(e)} - Traceback: {error_details}""" ) current_app.logger.error(f"Database error while inserting service: {e}") return False Loading @@ -128,13 +147,15 @@ def insertService( def insertServiceTranslation( serviceID: int, language_code: str, content: str content: str, title: str, ) -> bool | int: InsertServiceQuery = """INSERT INTO service_translation (service_id, language_id, content) VALUES (%s, (SELECT id FROM language WHERE language_code= %s ), %s);""" InsertServiceQuery = """INSERT INTO service_translation (service_id, language_id, content, title) VALUES (%s, (SELECT id FROM language WHERE language_code= %s ), %s, %s);""" content = None if content == "NULL" else content title = None if title == "NULL" else title try: with db_cursor() as (conn, cur): Loading @@ -144,6 +165,7 @@ def insertServiceTranslation( serviceID, language_code, content, title, ), ) conn.commit() Loading @@ -159,6 +181,7 @@ def updateServiceTranslation( serviceID: int, language_code: str, content: str, title: str, ): try: with db_cursor() as (conn, cur): Loading @@ -173,6 +196,13 @@ def updateServiceTranslation( update_fields.append("content = %s") update_values.append(content) if title is not None: if title == "NULL": update_fields.append("title = NULL") else: update_fields.append("title = %s") update_values.append(title) if update_fields: update_query = f"""UPDATE service_translation SET {', '.join(update_fields)} Loading @@ -194,9 +224,9 @@ def updateServiceTranslation( def deleteService(ServiceId: int) -> bool: print("deleteService with id: ", ServiceId ) # cascade delete on child tables records deleteServicesQuery = """DELETE FROM service WHERE id = %s""" deleteServicesQuery = """SELECT public.deleteService(%s)""" try: with db_cursor() as (conn, cur): Loading Loading @@ -228,22 +258,38 @@ def deleteServiceTranslation(ServiceId: int, language_code: str) -> bool: def updateService( id: int, position: int | None, translations: list[dict[str, str]], ): try: with db_cursor() as (conn, cur): conn.execute("BEGIN") try: if position is not None: cur.execute( "SELECT public.updateService(%s, %s)", (id, position), ) conn.commit() print("after exec update service") if translations: for serviceTranslation in translations: if "content" in serviceTranslation: if serviceTranslation["content"] is None: serviceTranslation["content"] = "NULL" if "title" in serviceTranslation: if serviceTranslation["title"] is None: serviceTranslation["title"] = "NULL" if isServiceTranslationExists( id, serviceTranslation["language_code"] ): if not updateServiceTranslation( id, serviceTranslation["language_code"], serviceTranslation["content"] serviceTranslation["content"], serviceTranslation["title"] ): raise Exception( f"Database error while updating service translation for {id} / {serviceTranslation['language_code']}" Loading @@ -251,15 +297,19 @@ def updateService( elif not insertServiceTranslation( id, serviceTranslation["language_code"], serviceTranslation["content"] serviceTranslation["content"], serviceTranslation["title"] ): raise Exception( f"Database error while inserting service translation for {id} / {serviceTranslation['language_code']}" ) conn.execute("COMMIT") return id # Returning the updated service ID return False # No update performed except Exception as e: conn.execute("ROLLBACK") raise e except Exception as e: # Capture full traceback for debugging Loading Loading @@ -314,3 +364,23 @@ def updateServiceLogo(serviceID: int, filename: str) -> bool: except Exception as e: current_app.logger.error(f"Database error while retrieving services: {e}") return False def getLastPosition() -> int: getLastPositionQuery = """SELECT MAX(s.position) FROM service s """ try: with db_cursor() as (conn, cur): cur.execute(getLastPositionQuery) lastPosition = cur.fetchone()[0] conn.commit() return lastPosition # Returning the updated keyword ID except Exception as e: current_app.logger.error( f"Database error while geting last service position: {e}" ) return False