Refactor service management: streamline service form, enhance state management, and improve loading logic
This commit is contained in:
@@ -55,8 +55,7 @@ class ServicesRepository {
|
||||
// --- SALVATAGGIO COMPLETO (PRIMA PADRE, POI FIGLI) ---
|
||||
Future<void> saveFullService(ServiceModel service) async {
|
||||
try {
|
||||
// 1. Inseriamo il record principale
|
||||
// Se service.id è null, Supabase fa INSERT. Se c'è, fa UPDATE (grazie all'upsert o gestione manuale)
|
||||
// 1. Upsert del record principale
|
||||
final serviceData = await _supabase
|
||||
.from('service')
|
||||
.upsert(service.toMap())
|
||||
@@ -65,45 +64,65 @@ class ServicesRepository {
|
||||
|
||||
final String newId = serviceData['id'];
|
||||
|
||||
// 2. Pulizia vecchi record figli (necessaria se è una MODIFICA)
|
||||
// Se stiamo modificando, cancelliamo i vecchi per reinserire i nuovi (più semplice)
|
||||
// 2. MODIFICA: Pulizia atomica dei figli
|
||||
// Se stiamo modificando (id != null), resettiamo le tabelle collegate
|
||||
if (service.id != null) {
|
||||
await _supabase.from('energy_service').delete().eq('service_id', newId);
|
||||
await _supabase.from('fin_service').delete().eq('service_id', newId);
|
||||
await _supabase
|
||||
.from('entertainment_service')
|
||||
.delete()
|
||||
.eq('service_id', newId);
|
||||
await Future.wait([
|
||||
_supabase.from('energy_service').delete().eq('service_id', newId),
|
||||
_supabase.from('fin_service').delete().eq('service_id', newId),
|
||||
_supabase
|
||||
.from('entertainment_service')
|
||||
.delete()
|
||||
.eq('service_id', newId),
|
||||
// Aggiungi qui eventuali altre tabelle pivot o file
|
||||
]);
|
||||
}
|
||||
|
||||
// 3. Inserimento EnergyServices
|
||||
// 3. Inserimento dei moduli in parallelo per velocità
|
||||
final List<Future> insertTasks = [];
|
||||
|
||||
if (service.energyServices.isNotEmpty) {
|
||||
final List<Map<String, dynamic>> toInsert = [];
|
||||
for (var item in service.energyServices) {
|
||||
toInsert.add(item.copyWith(serviceId: newId).toMap());
|
||||
}
|
||||
await _supabase.from('energy_service').insert(toInsert);
|
||||
insertTasks.add(
|
||||
_supabase
|
||||
.from('energy_service')
|
||||
.insert(
|
||||
service.energyServices
|
||||
.map((item) => item.copyWith(serviceId: newId).toMap())
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 4. Inserimento FinServices
|
||||
if (service.finServices.isNotEmpty) {
|
||||
final List<Map<String, dynamic>> toInsert = [];
|
||||
for (var item in service.finServices) {
|
||||
toInsert.add(item.copyWith(serviceId: newId).toMap());
|
||||
}
|
||||
await _supabase.from('fin_service').insert(toInsert);
|
||||
insertTasks.add(
|
||||
_supabase
|
||||
.from('fin_service')
|
||||
.insert(
|
||||
service.finServices
|
||||
.map((item) => item.copyWith(serviceId: newId).toMap())
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 5. Inserimento EntertainmentServices
|
||||
if (service.entertainmentServices.isNotEmpty) {
|
||||
final List<Map<String, dynamic>> toInsert = [];
|
||||
for (var item in service.entertainmentServices) {
|
||||
toInsert.add(item.copyWith(serviceId: newId).toMap());
|
||||
}
|
||||
await _supabase.from('entertainment_service').insert(toInsert);
|
||||
insertTasks.add(
|
||||
_supabase
|
||||
.from('entertainment_service')
|
||||
.insert(
|
||||
service.entertainmentServices
|
||||
.map((item) => item.copyWith(serviceId: newId).toMap())
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (insertTasks.isNotEmpty) {
|
||||
await Future.wait(insertTasks);
|
||||
}
|
||||
} catch (e) {
|
||||
throw Exception('Errore durante il salvataggio: $e');
|
||||
// Qui potresti aggiungere una logica di "rollback manuale" se necessario
|
||||
throw Exception('Errore durante il salvataggio corazzato: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user