sistemato assets, finito creazione company, inizio lavoro store
This commit is contained in:
31
lib/features/store/bloc/store_bloc.dart
Normal file
31
lib/features/store/bloc/store_bloc.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/features/store/data/store_repository.dart';
|
||||
import 'package:flux/features/store/models/store_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'store_events.dart';
|
||||
part 'store_state.dart';
|
||||
|
||||
class StoreBloc extends Bloc<StoreEvent, StoreState> {
|
||||
final StoreRepository _repository = GetIt.I<StoreRepository>();
|
||||
|
||||
StoreBloc() : super(const StoreState()) {
|
||||
on<CreateStoreRequested>(_onCreateStore);
|
||||
}
|
||||
|
||||
Future<void> _onCreateStore(
|
||||
CreateStoreRequested event,
|
||||
Emitter<StoreState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
try {
|
||||
await _repository.createStore(event.store);
|
||||
emit(state.copyWith(status: StoreStatus.success));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
lib/features/store/bloc/store_events.dart
Normal file
16
lib/features/store/bloc/store_events.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
part of 'store_bloc.dart';
|
||||
|
||||
abstract class StoreEvent extends Equatable {
|
||||
const StoreEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class CreateStoreRequested extends StoreEvent {
|
||||
final StoreModel store;
|
||||
const CreateStoreRequested({required this.store});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [store];
|
||||
}
|
||||
30
lib/features/store/bloc/store_state.dart
Normal file
30
lib/features/store/bloc/store_state.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
part of 'store_bloc.dart';
|
||||
|
||||
enum StoreStatus { initial, loading, success, failure }
|
||||
|
||||
class StoreState extends Equatable {
|
||||
final StoreStatus status;
|
||||
final StoreModel? store;
|
||||
final String? errorMessage;
|
||||
|
||||
const StoreState({
|
||||
this.status = StoreStatus.initial,
|
||||
this.store,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
StoreState copyWith({
|
||||
StoreStatus? status,
|
||||
StoreModel? store,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return StoreState(
|
||||
status: status ?? this.status,
|
||||
store: store ?? this.store,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, store, errorMessage];
|
||||
}
|
||||
36
lib/features/store/data/store_repository.dart
Normal file
36
lib/features/store/data/store_repository.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/store_model.dart';
|
||||
|
||||
class StoreRepository {
|
||||
final SupabaseClient _client = 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());
|
||||
} on PostgrestException catch (e) {
|
||||
// Intercettiamo errori specifici del database
|
||||
throw e.message;
|
||||
} catch (e) {
|
||||
throw 'Errore imprevisto durante la creazione del negozio: $e';
|
||||
}
|
||||
}
|
||||
|
||||
/// Recupera tutti i negozi di una determinata compagnia
|
||||
Future<List<StoreModel>> getStoresByCompany(String companyId) async {
|
||||
try {
|
||||
final response = await _client
|
||||
.from('store')
|
||||
.select()
|
||||
.eq('company_id', companyId)
|
||||
.order('created_at');
|
||||
|
||||
return (response as List)
|
||||
.map((json) => StoreModel.fromJson(json))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
throw 'Errore nel recupero dei negozi';
|
||||
}
|
||||
}
|
||||
}
|
||||
102
lib/features/store/models/store_model.dart
Normal file
102
lib/features/store/models/store_model.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class StoreModel extends Equatable {
|
||||
final String? id;
|
||||
final String nome;
|
||||
final String companyId;
|
||||
final bool isActive;
|
||||
final bool isPaid;
|
||||
final DateTime? paymentExpiration;
|
||||
final String indirizzo;
|
||||
final String cap;
|
||||
final String comune;
|
||||
final String provincia;
|
||||
|
||||
const StoreModel({
|
||||
this.id,
|
||||
required this.nome,
|
||||
required this.companyId,
|
||||
this.isActive = true,
|
||||
this.isPaid = false,
|
||||
this.paymentExpiration,
|
||||
required this.indirizzo,
|
||||
required this.cap,
|
||||
required this.comune,
|
||||
required this.provincia,
|
||||
});
|
||||
|
||||
// Fondamentale per Equatable: definisce quali proprietà determinano l'uguaglianza
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
nome,
|
||||
companyId,
|
||||
isActive,
|
||||
isPaid,
|
||||
paymentExpiration,
|
||||
indirizzo,
|
||||
cap,
|
||||
comune,
|
||||
provincia,
|
||||
];
|
||||
|
||||
// Il mitico copyWith per creare nuove istanze modificando solo ciò che serve
|
||||
StoreModel copyWith({
|
||||
String? id,
|
||||
String? nome,
|
||||
String? companyId,
|
||||
bool? isActive,
|
||||
bool? isPaid,
|
||||
DateTime? paymentExpiration,
|
||||
String? indirizzo,
|
||||
String? cap,
|
||||
String? comune,
|
||||
String? provincia,
|
||||
}) {
|
||||
return StoreModel(
|
||||
id: id ?? this.id,
|
||||
nome: nome ?? this.nome,
|
||||
companyId: companyId ?? this.companyId,
|
||||
isActive: isActive ?? this.isActive,
|
||||
isPaid: isPaid ?? this.isPaid,
|
||||
paymentExpiration: paymentExpiration ?? this.paymentExpiration,
|
||||
indirizzo: indirizzo ?? this.indirizzo,
|
||||
cap: cap ?? this.cap,
|
||||
comune: comune ?? this.comune,
|
||||
provincia: provincia ?? this.provincia,
|
||||
);
|
||||
}
|
||||
|
||||
factory StoreModel.fromJson(Map<String, dynamic> json) {
|
||||
return StoreModel(
|
||||
id: json['id'],
|
||||
nome: json['nome'],
|
||||
companyId: json['company_id'],
|
||||
isActive: json['is_active'] ?? true,
|
||||
isPaid: json['is_paid'] ?? false,
|
||||
paymentExpiration: json['payment_expiration'] != null
|
||||
? DateTime.parse(json['payment_expiration'])
|
||||
: null,
|
||||
indirizzo: json['indirizzo'],
|
||||
cap: json['cap'],
|
||||
comune: json['comune'],
|
||||
provincia: json['provincia'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'nome': nome,
|
||||
'company_id': companyId,
|
||||
'is_active': isActive,
|
||||
'is_paid': isPaid,
|
||||
if (paymentExpiration != null)
|
||||
'payment_expiration': paymentExpiration!.toIso8601String(),
|
||||
'indirizzo': indirizzo,
|
||||
'cap': cap,
|
||||
'comune': comune,
|
||||
'provincia': provincia,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user