Files
flux/lib/features/company/data/company_repository.dart

39 lines
1.0 KiB
Dart
Raw Permalink Normal View History

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')
.insert(company.toMap())
.select()
.single();
return CompanyModel.fromMap(response);
} on PostgrestException catch (e) {
throw e.message;
} catch (e) {
throw e.toString();
}
}
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();
return response != null ? CompanyModel.fromMap(response) : null;
} catch (e) {
return null;
}
}
}