Refactor provider and store models to use fromMap method; update associated stores handling in UI

This commit is contained in:
2026-04-17 11:36:15 +02:00
parent c1b6c9e7ac
commit 22a4f1dac4
9 changed files with 78 additions and 54 deletions

View File

@@ -31,7 +31,7 @@ class StoreCubit extends Cubit<StoreState> {
Future<void> loadStores() async {
emit(state.copyWith(status: StoreStatus.loading));
try {
final stores = await _repository.getStoresByCompany(
final stores = await _repository.fetchAllCompanyStores(
_sessionBloc.state.company!.id,
);
final Map<String, List<StaffMemberModel>> staffByStore = {};

View File

@@ -3,12 +3,12 @@ import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/store_model.dart';
class StoreRepository {
final SupabaseClient _client = GetIt.I.get<SupabaseClient>();
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
/// Crea un nuovo negozio associato alla compagnia dell'utente
Future<void> createStore(StoreModel store) async {
try {
await _client.from('store').insert(store.toJson());
await _supabase.from('store').insert(store.toMap());
} on PostgrestException catch (e) {
// Intercettiamo errori specifici del database
throw e.message;
@@ -18,19 +18,21 @@ class StoreRepository {
}
/// Recupera tutti i negozi di una determinata compagnia
Future<List<StoreModel>> getStoresByCompany(String companyId) async {
Future<List<StoreModel>> fetchAllCompanyStores(String companyId) async {
try {
final response = await _client
final response = await _supabase
.from('store')
.select()
.select('''
*,
providers_count:providers_in_stores(count),
staff_members_count:staff_in_stores(count)
''')
.eq('company_id', companyId)
.order('created_at');
.order('nome');
return (response as List)
.map((json) => StoreModel.fromJson(json))
.toList();
return (response as List).map((m) => StoreModel.fromMap(m)).toList();
} catch (e) {
throw 'Errore nel recupero dei negozi';
throw 'Errore nel recupero dei negozi: $e';
}
}
}

View File

@@ -11,6 +11,9 @@ class StoreModel extends Equatable {
final String cap;
final String comune;
final String provincia;
final int providersCount; // Numero di provider associati, utile per la lista
final int
staffMembersCount; // Numero di membri dello staff associati, utile per la lista
const StoreModel({
this.id,
@@ -23,6 +26,8 @@ class StoreModel extends Equatable {
required this.cap,
required this.comune,
required this.provincia,
this.providersCount = 0, // Default a 0 se non specificato
this.staffMembersCount = 0, // Default a 0 se non specificato
});
// Fondamentale per Equatable: definisce quali proprietà determinano l'uguaglianza
@@ -38,6 +43,8 @@ class StoreModel extends Equatable {
cap,
comune,
provincia,
providersCount,
staffMembersCount,
];
// Il mitico copyWith per creare nuove istanze modificando solo ciò che serve
@@ -52,6 +59,8 @@ class StoreModel extends Equatable {
String? cap,
String? comune,
String? provincia,
int? providersCount,
int? staffMembersCount,
}) {
return StoreModel(
id: id ?? this.id,
@@ -64,27 +73,38 @@ class StoreModel extends Equatable {
cap: cap ?? this.cap,
comune: comune ?? this.comune,
provincia: provincia ?? this.provincia,
providersCount: providersCount ?? this.providersCount,
staffMembersCount: staffMembersCount ?? this.staffMembersCount,
);
}
factory StoreModel.fromJson(Map<String, dynamic> json) {
factory StoreModel.fromMap(Map<String, dynamic> map) {
return StoreModel(
id: json['id'] as String,
nome: json['nome'],
companyId: json['company_id'] as String,
isActive: json['is_active'] ?? true,
isPaid: json['is_paid'] ?? false,
paymentExpiration: json['payment_expiration'] != null
? DateTime.parse(json['payment_expiration'])
id: map['id'] as String,
nome: map['nome'],
companyId: map['company_id'] as String,
isActive: map['is_active'] ?? true,
isPaid: map['is_paid'] ?? false,
paymentExpiration: map['payment_expiration'] != null
? DateTime.parse(map['payment_expiration'])
: null,
indirizzo: json['indirizzo'],
cap: json['cap'],
comune: json['comune'],
provincia: json['provincia'],
indirizzo: map['indirizzo'],
cap: map['cap'],
comune: map['comune'],
provincia: map['provincia'],
providersCount:
map['providers_count'] != null && map['providers_count'].isNotEmpty
? map['providers_count'][0]['count'] as int
: 0,
staffMembersCount:
map['staff_members_count'] != null &&
map['staff_members_count'].isNotEmpty
? map['staff_members_count'][0]['count'] as int
: 0,
);
}
Map<String, dynamic> toJson() {
Map<String, dynamic> toMap() {
return {
if (id != null) 'id': id,
'nome': nome,