CoffeeAtHome/lib/examples/csv_crud_example.dart
2026-03-29 08:13:38 -07:00

179 lines
5.2 KiB
Dart

// Example: How to use CRUD operations with CSV data
import '../services/csv_data_service.dart';
import '../models/bean.dart';
import '../models/machine.dart';
class CsvCrudExample {
final CsvDataService _csvService = CsvDataService();
// Example 1: Add a custom bean to the catalog
Future<void> addCustomBean() async {
final customBean = Bean(
id: 'custom_bean_1',
name: 'My Special Blend',
origin: 'Custom',
farm: 'Home Roastery',
producer: 'Me',
varietal: 'Mixed',
altitude: 1200,
processingMethod: 'Washed',
harvestSeason: '2025',
flavorNotes: [TastingNotes.chocolate, TastingNotes.nutty],
acidity: Acidity.medium,
body: Body.medium,
sweetness: 7,
roastLevel: RoastLevel.medium,
cupScore: 88.0,
price: 25.0,
availability: Availability.available,
certifications: ['Custom Roasted'],
roaster: 'Home Roaster',
roastDate: DateTime.now(),
bestByDate: DateTime.now().add(Duration(days: 30)),
brewingMethods: ['Espresso', 'Pour Over'],
isOwned: false,
quantity: 0.0,
notes: 'My own custom blend',
);
await _csvService.saveBean(customBean);
print('Custom bean added to catalog!');
}
// Example 2: List all beans (CSV + custom)
Future<void> listAllBeans() async {
final allBeans = await _csvService.getBeans();
print('Total beans in catalog: ${allBeans.length}');
// Separate CSV and custom beans
final csvBeans = await _csvService.getCsvBeans();
final customBeans = _csvService.getCustomBeans();
print('CSV beans: ${csvBeans.length}');
print('Custom beans: ${customBeans.length}');
// List custom beans
for (final bean in customBeans) {
print('Custom: ${bean.name} - ${bean.roaster}');
}
}
// Example 3: Try to delete a CSV bean (will fail)
Future<void> tryDeleteCsvBean() async {
try {
await _csvService.deleteBean('bean_1'); // Assuming this is a CSV bean
} catch (e) {
print('Expected error: $e');
}
}
// Example 4: Delete a custom bean (will succeed)
Future<void> deleteCustomBean() async {
try {
await _csvService.deleteBean('custom_bean_1');
print('Custom bean deleted successfully!');
} catch (e) {
print('Error deleting custom bean: $e');
}
}
// Example 5: Check if item is from CSV or custom
Future<void> checkItemSource() async {
final allBeans = await _csvService.getBeans();
for (final bean in allBeans.take(5)) {
final isFromCsv = _csvService.isCsvBean(bean.id);
print('${bean.name}: ${isFromCsv ? "CSV" : "Custom"}');
}
}
// Example 6: Add custom machine
Future<void> addCustomMachine() async {
final customMachine = Machine(
id: 'custom_machine_1',
manufacturer: 'Custom',
model: 'DIY Espresso Machine',
year: 2025,
type: MachineType.espresso,
steamWand: true,
details: 'Built from scratch with love',
isOwned: false,
rating: 5.0,
popularity: 1,
portafilters: [
Portafilter(
id: 'custom_pf_1',
size: '58mm',
material: 'Stainless Steel',
)
],
specifications: {
'Boiler': 'Single',
'Pressure': '9 bar',
'Water Tank': '2L',
},
);
await _csvService.saveMachine(customMachine);
print('Custom machine added to catalog!');
}
// Example 7: Update existing custom item
Future<void> updateCustomBean() async {
final customBeans = _csvService.getCustomBeans();
if (customBeans.isNotEmpty) {
final existingBean = customBeans.first;
final beanToUpdate = Bean(
id: existingBean.id,
name: existingBean.name,
origin: existingBean.origin,
farm: existingBean.farm,
producer: existingBean.producer,
varietal: existingBean.varietal,
altitude: existingBean.altitude,
processingMethod: existingBean.processingMethod,
harvestSeason: existingBean.harvestSeason,
flavorNotes: existingBean.flavorNotes,
acidity: existingBean.acidity,
body: existingBean.body,
sweetness: existingBean.sweetness,
roastLevel: existingBean.roastLevel,
cupScore: existingBean.cupScore,
price: 30.0, // Update price
availability: existingBean.availability,
certifications: existingBean.certifications,
roaster: existingBean.roaster,
roastDate: existingBean.roastDate,
bestByDate: existingBean.bestByDate,
brewingMethods: existingBean.brewingMethods,
isOwned: existingBean.isOwned,
quantity: existingBean.quantity,
notes: 'Updated notes: Even better now!', // Update notes
);
await _csvService.saveBean(beanToUpdate);
print('Custom bean updated!');
}
}
}
// Usage example
void main() async {
final example = CsvCrudExample();
// Add custom items
await example.addCustomBean();
await example.addCustomMachine();
// List and check items
await example.listAllBeans();
await example.checkItemSource();
// Try operations
await example.tryDeleteCsvBean(); // Will fail
await example.updateCustomBean(); // Will succeed
await example.deleteCustomBean(); // Will succeed
}