2026-05-08 12:28:14 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
2026-04-09 11:30:57 +02:00
|
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
|
import '../models/company_model.dart';
|
|
|
|
|
|
|
|
|
|
class CompanyRepository {
|
|
|
|
|
final SupabaseClient _supabase = Supabase.instance.client;
|
|
|
|
|
|
|
|
|
|
Future<CompanyModel> createCompany(CompanyModel company) async {
|
|
|
|
|
try {
|
|
|
|
|
// .select().single() trasforma la risposta nell'oggetto appena inserito
|
|
|
|
|
final response = await _supabase
|
|
|
|
|
.from('company')
|
2026-04-22 11:06:02 +02:00
|
|
|
.insert(company.toMap())
|
2026-04-09 11:30:57 +02:00
|
|
|
.select()
|
|
|
|
|
.single();
|
|
|
|
|
|
2026-04-22 11:06:02 +02:00
|
|
|
return CompanyModel.fromMap(response);
|
2026-04-09 11:30:57 +02:00
|
|
|
} on PostgrestException catch (e) {
|
|
|
|
|
throw e.message;
|
|
|
|
|
} catch (e) {
|
2026-05-04 15:36:42 +02:00
|
|
|
throw e.toString();
|
2026-04-09 11:30:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 12:28:14 +02:00
|
|
|
Future<CompanyModel> updateCompany(CompanyModel company) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await _supabase
|
|
|
|
|
.from('company')
|
|
|
|
|
.update(company.toMap())
|
|
|
|
|
.eq('id', company.id!)
|
|
|
|
|
.select()
|
|
|
|
|
.single();
|
|
|
|
|
|
|
|
|
|
return CompanyModel.fromMap(response);
|
|
|
|
|
} on PostgrestException catch (e) {
|
|
|
|
|
throw e.message;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw e.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> uploadCompanyLogo({
|
|
|
|
|
required String companyId,
|
|
|
|
|
required Uint8List fileBytes,
|
|
|
|
|
required String fileName,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
// 1. Prepariamo il path.
|
|
|
|
|
// Organizziamo per companyId e aggiungiamo un timestamp per evitare cache del browser
|
|
|
|
|
// quando l'utente cambia logo più volte.
|
|
|
|
|
final extension = fileName.split('.').last;
|
|
|
|
|
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
|
final filePath = '$companyId/logo_$timestamp.$extension';
|
|
|
|
|
|
|
|
|
|
// 2. Caricamento fisico dei bytes
|
|
|
|
|
// Usiamo uploadBinary che è perfetto per Uint8List
|
|
|
|
|
await _supabase.storage
|
|
|
|
|
.from('company_logos')
|
|
|
|
|
.uploadBinary(
|
|
|
|
|
filePath,
|
|
|
|
|
fileBytes,
|
|
|
|
|
fileOptions: const FileOptions(
|
|
|
|
|
cacheControl: '3600',
|
|
|
|
|
upsert:
|
|
|
|
|
true, // Se esiste già un file con lo stesso nome, lo sovrascrive
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 3. Otteniamo l'URL pubblico.
|
|
|
|
|
// Nota: il bucket 'company_logos' deve essere impostato come PUBLIC su Supabase
|
|
|
|
|
final String publicUrl = _supabase.storage
|
|
|
|
|
.from('company_logos')
|
|
|
|
|
.getPublicUrl(filePath);
|
|
|
|
|
|
|
|
|
|
return publicUrl;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception("Errore durante l'upload del logo: $e");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 11:30:57 +02:00
|
|
|
Future<CompanyModel?> getCompany() async {
|
|
|
|
|
try {
|
|
|
|
|
final userId = _supabase.auth.currentUser?.id;
|
|
|
|
|
final response = await _supabase
|
|
|
|
|
.from('company')
|
|
|
|
|
.select()
|
|
|
|
|
.eq('user_id', userId as Object)
|
|
|
|
|
.maybeSingle();
|
|
|
|
|
|
2026-04-22 11:06:02 +02:00
|
|
|
return response != null ? CompanyModel.fromMap(response) : null;
|
2026-04-09 11:30:57 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|