products data

This commit is contained in:
2026-04-12 19:21:54 +02:00
parent bdf928cca3
commit b8caff7636
9 changed files with 419 additions and 28 deletions

View File

@@ -0,0 +1,86 @@
import 'package:get_it/get_it.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/brand_model.dart';
import '../models/model_model.dart';
class ProductRepository {
final SupabaseClient _client = GetIt.I<SupabaseClient>();
// --- BRAND ---
/// Recupera tutti i brand dell'azienda
Future<List<BrandModel>> getBrands(String companyId) async {
try {
final response = await _client
.from('brand')
.select()
.eq('company_id', companyId)
.eq('is_active', true)
.order('name');
return (response as List).map((b) => BrandModel.fromJson(b)).toList();
} catch (e) {
throw 'Errore nel recupero dei Brand';
}
}
/// Crea o aggiorna un brand
Future<BrandModel> upsertBrand(BrandModel brand) async {
try {
final response = await _client
.from('brand')
.upsert(brand.toJson())
.select()
.single();
return BrandModel.fromJson(response);
} catch (e) {
throw 'Errore nel salvataggio del Brand';
}
}
// --- MODEL ---
/// Recupera i modelli di un brand specifico
Future<List<ModelModel>> getModelsByBrand(String brandId) async {
try {
final response = await _client
.from('model')
.select()
.eq('brand_id', brandId)
.eq('is_active', true)
.order('name');
return (response as List).map((m) => ModelModel.fromJson(m)).toList();
} catch (e) {
throw 'Errore nel recupero dei modelli';
}
}
/// Crea o aggiorna un modello
/// NOTA: name_with_brand verrà gestito dal trigger SQL che hai lanciato!
Future<ModelModel> upsertModel(ModelModel model) async {
try {
final response = await _client
.from('model')
.upsert(model.toJson())
.select()
.single();
return ModelModel.fromJson(response);
} catch (e) {
throw 'Errore nel salvataggio del modello';
}
}
// --- DELETE (LOGICA) ---
/// 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);
} catch (e) {
throw 'Errore durante la modifica dello stato';
}
}
}