service #2
83
lib/features/services/data/services_repository.dart
Normal file
83
lib/features/services/data/services_repository.dart
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
|
import '../models/service_model.dart';
|
||||||
|
// Importa gli altri modelli se sono in file separati
|
||||||
|
|
||||||
|
class ServicesRepository {
|
||||||
|
final _supabase = Supabase.instance.client;
|
||||||
|
|
||||||
|
// --- RECUPERO TUTTI I SERVIZI ---
|
||||||
|
Future<List<ServiceModel>> fetchAllServices() async {
|
||||||
|
try {
|
||||||
|
// La stringa di selezione tira giù il padre e TUTTI i record correlati dalle tabelle figlie
|
||||||
|
final response = await _supabase
|
||||||
|
.from('service')
|
||||||
|
.select('''
|
||||||
|
*,
|
||||||
|
energy_service(*),
|
||||||
|
fin_service(*),
|
||||||
|
entertainment_service(*)
|
||||||
|
''')
|
||||||
|
.order('created_at', ascending: false);
|
||||||
|
|
||||||
|
return (response as List)
|
||||||
|
.map((map) => ServiceModel.fromMap(map))
|
||||||
|
.toList();
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Errore nel recupero servizi: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- SALVATAGGIO COMPLETO (A CASCATA) ---
|
||||||
|
Future<void> saveFullService(ServiceModel service) async {
|
||||||
|
try {
|
||||||
|
// 1. Inserimento Padre
|
||||||
|
final serviceData = await _supabase
|
||||||
|
.from('service')
|
||||||
|
.insert(service.toMap())
|
||||||
|
.select()
|
||||||
|
.single();
|
||||||
|
|
||||||
|
final String newId = serviceData['id'];
|
||||||
|
|
||||||
|
// 2. Inserimento Energy (se presenti)
|
||||||
|
if (service.energyServices.isNotEmpty) {
|
||||||
|
final List<Map<String, dynamic>> energyToInsert = [];
|
||||||
|
for (var item in service.energyServices) {
|
||||||
|
energyToInsert.add(item.copyWith(serviceId: newId).toMap());
|
||||||
|
}
|
||||||
|
await _supabase.from('energy_service').insert(energyToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Inserimento Finanziamenti (se presenti)
|
||||||
|
if (service.finServices.isNotEmpty) {
|
||||||
|
final List<Map<String, dynamic>> finToInsert = [];
|
||||||
|
for (var item in service.finServices) {
|
||||||
|
finToInsert.add(item.copyWith(serviceId: newId).toMap());
|
||||||
|
}
|
||||||
|
await _supabase.from('fin_service').insert(finToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Inserimento Entertainment (se presenti)
|
||||||
|
if (service.entertainmentServices.isNotEmpty) {
|
||||||
|
final List<Map<String, dynamic>> entToInsert = [];
|
||||||
|
for (var item in service.entertainmentServices) {
|
||||||
|
entToInsert.add(item.copyWith(serviceId: newId).toMap());
|
||||||
|
}
|
||||||
|
await _supabase.from('entertainment_service').insert(entToInsert);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Errore durante il salvataggio: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- ELIMINAZIONE ---
|
||||||
|
// Grazie ai "ON DELETE CASCADE" che hai messo nell'SQL,
|
||||||
|
// cancellando il padre Supabase pialla automaticamente i figli. Top!
|
||||||
|
Future<void> deleteService(String id) async {
|
||||||
|
try {
|
||||||
|
await _supabase.from('service').delete().eq('id', id);
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Errore durante l\'eliminazione: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
final DateTime? createdAt;
|
final DateTime? createdAt;
|
||||||
final EnergyType type;
|
final EnergyType type;
|
||||||
final DateTime expiration;
|
final DateTime expiration;
|
||||||
final String gestoreId;
|
final String providerId;
|
||||||
final String? serviceId;
|
final String? serviceId;
|
||||||
|
|
||||||
const EnergyServiceModel({
|
const EnergyServiceModel({
|
||||||
@@ -15,7 +15,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
this.createdAt,
|
this.createdAt,
|
||||||
required this.type,
|
required this.type,
|
||||||
required this.expiration,
|
required this.expiration,
|
||||||
required this.gestoreId,
|
required this.providerId,
|
||||||
this.serviceId,
|
this.serviceId,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
DateTime? createdAt,
|
DateTime? createdAt,
|
||||||
EnergyType? type,
|
EnergyType? type,
|
||||||
DateTime? expiration,
|
DateTime? expiration,
|
||||||
String? gestoreId,
|
String? providerId,
|
||||||
String? serviceId,
|
String? serviceId,
|
||||||
}) {
|
}) {
|
||||||
return EnergyServiceModel(
|
return EnergyServiceModel(
|
||||||
@@ -32,7 +32,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
createdAt: createdAt ?? this.createdAt,
|
createdAt: createdAt ?? this.createdAt,
|
||||||
type: type ?? this.type,
|
type: type ?? this.type,
|
||||||
expiration: expiration ?? this.expiration,
|
expiration: expiration ?? this.expiration,
|
||||||
gestoreId: gestoreId ?? this.gestoreId,
|
providerId: providerId ?? this.providerId,
|
||||||
serviceId: serviceId ?? this.serviceId,
|
serviceId: serviceId ?? this.serviceId,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
createdAt,
|
createdAt,
|
||||||
type,
|
type,
|
||||||
expiration,
|
expiration,
|
||||||
gestoreId,
|
providerId,
|
||||||
serviceId,
|
serviceId,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
: null,
|
: null,
|
||||||
type: map['type'] == 'gas' ? EnergyType.gas : EnergyType.luce,
|
type: map['type'] == 'gas' ? EnergyType.gas : EnergyType.luce,
|
||||||
expiration: DateTime.parse(map['expiration']),
|
expiration: DateTime.parse(map['expiration']),
|
||||||
gestoreId: map['gestore_id'],
|
providerId: map['provider_id'],
|
||||||
serviceId: map['service_id'],
|
serviceId: map['service_id'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,7 @@ class EnergyServiceModel extends Equatable {
|
|||||||
if (id != null) 'id': id,
|
if (id != null) 'id': id,
|
||||||
'type': type.name, // .name trasforma l'enum in 'luce' o 'gas'
|
'type': type.name, // .name trasforma l'enum in 'luce' o 'gas'
|
||||||
'expiration': expiration.toIso8601String(),
|
'expiration': expiration.toIso8601String(),
|
||||||
'gestore_id': gestoreId,
|
'provider_id': providerId,
|
||||||
'service_id': serviceId,
|
'service_id': serviceId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
final bool constrained; // Vincolato?
|
final bool constrained; // Vincolato?
|
||||||
final DateTime constrainExpiration;
|
final DateTime constrainExpiration;
|
||||||
final String? serviceId;
|
final String? serviceId;
|
||||||
|
final String? providerId;
|
||||||
|
|
||||||
const EntertainmentServiceModel({
|
const EntertainmentServiceModel({
|
||||||
this.id,
|
this.id,
|
||||||
@@ -15,6 +16,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
required this.constrained,
|
required this.constrained,
|
||||||
required this.constrainExpiration,
|
required this.constrainExpiration,
|
||||||
this.serviceId,
|
this.serviceId,
|
||||||
|
this.providerId,
|
||||||
});
|
});
|
||||||
|
|
||||||
EntertainmentServiceModel copyWith({
|
EntertainmentServiceModel copyWith({
|
||||||
@@ -24,6 +26,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
bool? constrained,
|
bool? constrained,
|
||||||
DateTime? constrainExpiration,
|
DateTime? constrainExpiration,
|
||||||
String? serviceId,
|
String? serviceId,
|
||||||
|
String? providerId,
|
||||||
}) {
|
}) {
|
||||||
return EntertainmentServiceModel(
|
return EntertainmentServiceModel(
|
||||||
id: id ?? this.id,
|
id: id ?? this.id,
|
||||||
@@ -32,6 +35,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
constrained: constrained ?? this.constrained,
|
constrained: constrained ?? this.constrained,
|
||||||
constrainExpiration: constrainExpiration ?? this.constrainExpiration,
|
constrainExpiration: constrainExpiration ?? this.constrainExpiration,
|
||||||
serviceId: serviceId ?? this.serviceId,
|
serviceId: serviceId ?? this.serviceId,
|
||||||
|
providerId: providerId ?? this.providerId,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +47,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
constrained,
|
constrained,
|
||||||
constrainExpiration,
|
constrainExpiration,
|
||||||
serviceId,
|
serviceId,
|
||||||
|
providerId,
|
||||||
];
|
];
|
||||||
|
|
||||||
factory EntertainmentServiceModel.fromMap(Map<String, dynamic> map) {
|
factory EntertainmentServiceModel.fromMap(Map<String, dynamic> map) {
|
||||||
@@ -55,6 +60,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
constrained: map['constrained'] ?? false,
|
constrained: map['constrained'] ?? false,
|
||||||
constrainExpiration: DateTime.parse(map['constrain_expiration']),
|
constrainExpiration: DateTime.parse(map['constrain_expiration']),
|
||||||
serviceId: map['service_id'],
|
serviceId: map['service_id'],
|
||||||
|
providerId: map['provider_id'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +71,7 @@ class EntertainmentServiceModel extends Equatable {
|
|||||||
'constrained': constrained,
|
'constrained': constrained,
|
||||||
'constrain_expiration': constrainExpiration.toIso8601String(),
|
'constrain_expiration': constrainExpiration.toIso8601String(),
|
||||||
'service_id': serviceId,
|
'service_id': serviceId,
|
||||||
|
'provider_id': providerId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class FinServiceModel extends Equatable {
|
|||||||
final DateTime expiration;
|
final DateTime expiration;
|
||||||
final String? serviceId;
|
final String? serviceId;
|
||||||
final String? modelId; // FK verso model (es. iPhone, Samsung, ecc.)
|
final String? modelId; // FK verso model (es. iPhone, Samsung, ecc.)
|
||||||
|
final String? providerId;
|
||||||
|
|
||||||
const FinServiceModel({
|
const FinServiceModel({
|
||||||
this.id,
|
this.id,
|
||||||
@@ -13,6 +14,7 @@ class FinServiceModel extends Equatable {
|
|||||||
required this.expiration,
|
required this.expiration,
|
||||||
this.serviceId,
|
this.serviceId,
|
||||||
this.modelId,
|
this.modelId,
|
||||||
|
this.providerId,
|
||||||
});
|
});
|
||||||
|
|
||||||
FinServiceModel copyWith({
|
FinServiceModel copyWith({
|
||||||
@@ -21,6 +23,7 @@ class FinServiceModel extends Equatable {
|
|||||||
DateTime? expiration,
|
DateTime? expiration,
|
||||||
String? serviceId,
|
String? serviceId,
|
||||||
String? modelId,
|
String? modelId,
|
||||||
|
String? providerId,
|
||||||
}) {
|
}) {
|
||||||
return FinServiceModel(
|
return FinServiceModel(
|
||||||
id: id ?? this.id,
|
id: id ?? this.id,
|
||||||
@@ -28,6 +31,7 @@ class FinServiceModel extends Equatable {
|
|||||||
expiration: expiration ?? this.expiration,
|
expiration: expiration ?? this.expiration,
|
||||||
serviceId: serviceId ?? this.serviceId,
|
serviceId: serviceId ?? this.serviceId,
|
||||||
modelId: modelId ?? this.modelId,
|
modelId: modelId ?? this.modelId,
|
||||||
|
providerId: providerId ?? this.providerId,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +47,7 @@ class FinServiceModel extends Equatable {
|
|||||||
expiration: DateTime.parse(map['expiration']),
|
expiration: DateTime.parse(map['expiration']),
|
||||||
serviceId: map['service_id'],
|
serviceId: map['service_id'],
|
||||||
modelId: map['model_id'],
|
modelId: map['model_id'],
|
||||||
|
providerId: map['provider_id'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +57,7 @@ class FinServiceModel extends Equatable {
|
|||||||
'expiration': expiration.toIso8601String(),
|
'expiration': expiration.toIso8601String(),
|
||||||
'service_id': serviceId,
|
'service_id': serviceId,
|
||||||
'model_id': modelId,
|
'model_id': modelId,
|
||||||
|
'provider_id': providerId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user