This commit is contained in:
2026-04-18 12:01:12 +02:00
parent a3509bca91
commit bbb9729ca4
3 changed files with 137 additions and 9 deletions

View File

@@ -102,4 +102,25 @@ class ProductCubit extends Cubit<ProductState> {
);
}
}
Future<void> searchModels(String query) async {
if (query.isEmpty) {
// Se cancella tutto, potresti voler ricaricare i modelli del brand selezionato
// o svuotare la lista. Scegliamo di svuotare per pulizia.
emit(state.copyWith(models: []));
return;
}
// Opzionale: emetti loading solo se vuoi mostrare una barretta nella UI
// emit(state.copyWith(status: ProductStatus.loading));
try {
final results = await _repository.searchModels(query);
emit(state.copyWith(status: ProductStatus.success, models: results));
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
}

View File

@@ -4,14 +4,14 @@ import '../models/brand_model.dart';
import '../models/model_model.dart';
class ProductRepository {
final SupabaseClient _client = GetIt.I<SupabaseClient>();
final SupabaseClient _supabase = GetIt.I<SupabaseClient>();
// --- BRAND ---
/// Recupera tutti i brand dell'azienda
Future<List<BrandModel>> getBrands(String companyId) async {
try {
final response = await _client
final response = await _supabase
.from('brand')
.select()
.eq('company_id', companyId)
@@ -27,7 +27,7 @@ class ProductRepository {
/// Crea o aggiorna un brand
Future<BrandModel> upsertBrand(BrandModel brand) async {
try {
final response = await _client
final response = await _supabase
.from('brand')
.upsert(brand.toJson())
.select()
@@ -44,7 +44,7 @@ class ProductRepository {
/// Recupera i modelli di un brand specifico
Future<List<ModelModel>> getModelsByBrand(String brandId) async {
try {
final response = await _client
final response = await _supabase
.from('model')
.select()
.eq('brand_id', brandId)
@@ -61,7 +61,7 @@ class ProductRepository {
/// NOTA: name_with_brand verrà gestito dal trigger SQL che hai lanciato!
Future<ModelModel> upsertModel(ModelModel model) async {
try {
final response = await _client
final response = await _supabase
.from('model')
.upsert(model.toJson())
.select()
@@ -78,9 +78,24 @@ class ProductRepository {
/// Disattiva un brand o un modello (Soft Delete per non rompere le FK delle operazioni passate)
Future<void> toggleActiveStatus(String table, String id, bool status) async {
try {
await _client.from(table).update({'is_active': status}).eq('id', id);
await _supabase.from(table).update({'is_active': status}).eq('id', id);
} catch (e) {
throw 'Errore durante la modifica dello stato';
}
}
Future<List<ModelModel>> searchModels(String query) async {
try {
final response = await _supabase
.from('model')
.select()
.ilike('name_with_brand', '%$query%') // Cerca ovunque nel nome
.eq('is_active', true)
.limit(10); // Non esageriamo con i risultati
return (response as List).map((m) => ModelModel.fromJson(m)).toList();
} catch (e) {
throw 'Errore durante la ricerca: $e';
}
}
}