Initial commit - app-padrao-1.0
This commit is contained in:
43
lib/features/services/data/models/service_model.dart
Normal file
43
lib/features/services/data/models/service_model.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'service_model.g.dart';
|
||||
|
||||
@HiveType(typeId: 6) // TypeId 0=User, 1=Haircut, 2=Product, 3=TransactionType/4=Transaction, 5=Settings
|
||||
class ServiceModel {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
@HiveField(1)
|
||||
final String name; // Ex: Corte Máquina, Barba
|
||||
|
||||
@HiveField(2)
|
||||
final double price; // Preço padrão
|
||||
|
||||
@HiveField(3)
|
||||
final int durationMinutes; // Duração estimada (opcional, bom ter)
|
||||
|
||||
@HiveField(4)
|
||||
final String userId; // Para segregar por usuário
|
||||
|
||||
ServiceModel({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.price,
|
||||
this.durationMinutes = 30,
|
||||
required this.userId,
|
||||
});
|
||||
|
||||
ServiceModel copyWith({
|
||||
String? name,
|
||||
double? price,
|
||||
int? durationMinutes,
|
||||
}) {
|
||||
return ServiceModel(
|
||||
id: id,
|
||||
name: name ?? this.name,
|
||||
price: price ?? this.price,
|
||||
durationMinutes: durationMinutes ?? this.durationMinutes,
|
||||
userId: userId,
|
||||
);
|
||||
}
|
||||
}
|
||||
53
lib/features/services/data/models/service_model.g.dart
Normal file
53
lib/features/services/data/models/service_model.g.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'service_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class ServiceModelAdapter extends TypeAdapter<ServiceModel> {
|
||||
@override
|
||||
final int typeId = 6;
|
||||
|
||||
@override
|
||||
ServiceModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return ServiceModel(
|
||||
id: fields[0] as String,
|
||||
name: fields[1] as String,
|
||||
price: fields[2] as double,
|
||||
durationMinutes: fields[3] as int,
|
||||
userId: fields[4] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, ServiceModel obj) {
|
||||
writer
|
||||
..writeByte(5)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.name)
|
||||
..writeByte(2)
|
||||
..write(obj.price)
|
||||
..writeByte(3)
|
||||
..write(obj.durationMinutes)
|
||||
..writeByte(4)
|
||||
..write(obj.userId);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is ServiceModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:barber_app/core/database/database_service.dart';
|
||||
import 'package:barber_app/features/services/data/models/service_model.dart';
|
||||
|
||||
class ServiceRepository {
|
||||
final _uuid = const Uuid();
|
||||
|
||||
String? get _currentUserId => DatabaseService.getCurrentUserId();
|
||||
|
||||
// Criar Serviço
|
||||
Future<ServiceModel?> createService({
|
||||
required String name,
|
||||
required double price,
|
||||
int durationMinutes = 30,
|
||||
}) async {
|
||||
if (_currentUserId == null) return null;
|
||||
|
||||
final service = ServiceModel(
|
||||
id: _uuid.v4(),
|
||||
name: name,
|
||||
price: price,
|
||||
durationMinutes: durationMinutes,
|
||||
userId: _currentUserId!,
|
||||
);
|
||||
|
||||
// ATENÇÃO: Precisamos adicionar servicesBoxInstance no DatabaseService
|
||||
await DatabaseService.servicesBoxInstance.put(service.id, service);
|
||||
return service;
|
||||
}
|
||||
|
||||
// Editar
|
||||
Future<void> updateService(ServiceModel service) async {
|
||||
await DatabaseService.servicesBoxInstance.put(service.id, service);
|
||||
}
|
||||
|
||||
// Deletar
|
||||
Future<void> deleteService(String id) async {
|
||||
await DatabaseService.servicesBoxInstance.delete(id);
|
||||
}
|
||||
|
||||
// Listar Todos
|
||||
List<ServiceModel> getAllServices() {
|
||||
if (_currentUserId == null) return [];
|
||||
|
||||
return DatabaseService.servicesBoxInstance.values
|
||||
.where((s) => s.userId == _currentUserId)
|
||||
.toList()
|
||||
..sort((a, b) => a.name.compareTo(b.name));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user