You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.6 KiB
156 lines
4.6 KiB
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:path/path.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
import '../Models/produit.dart';
|
|
|
|
class ProductDatabase {
|
|
static final ProductDatabase instance = ProductDatabase._init();
|
|
late Database _database;
|
|
|
|
ProductDatabase._init() {
|
|
sqfliteFfiInit();
|
|
}
|
|
|
|
ProductDatabase();
|
|
|
|
Future<Database> get database async {
|
|
if (_database.isOpen) return _database;
|
|
_database = await _initDB('products2.db');
|
|
return _database;
|
|
}
|
|
|
|
Future<void> initDatabase() async {
|
|
_database = await _initDB('products2.db');
|
|
await _createDB(_database, 1);
|
|
}
|
|
|
|
Future<Database> _initDB(String filePath) async {
|
|
final documentsDirectory = await getApplicationDocumentsDirectory();
|
|
final path = join(documentsDirectory.path, filePath);
|
|
|
|
bool dbExists = await File(path).exists();
|
|
if (!dbExists) {
|
|
ByteData data = await rootBundle.load('assets/database/$filePath');
|
|
List<int> bytes =
|
|
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
|
|
await File(path).writeAsBytes(bytes);
|
|
}
|
|
|
|
return await databaseFactoryFfi.openDatabase(path);
|
|
}
|
|
|
|
Future<void> _createDB(Database db, int version) async {
|
|
// Récupère la liste des colonnes de la table "products"
|
|
final tables = await db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'");
|
|
final tableNames = tables.map((row) => row['name'] as String).toList();
|
|
|
|
// Si la table "products" n'existe pas encore, on la crée entièrement
|
|
if (!tableNames.contains('products')) {
|
|
await db.execute('''
|
|
CREATE TABLE products(
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT,
|
|
price REAL,
|
|
image TEXT,
|
|
category TEXT,
|
|
stock INTEGER,
|
|
description TEXT,
|
|
qrCode TEXT
|
|
reference TEXT
|
|
)
|
|
''');
|
|
print("Table 'products' créée avec toutes les colonnes.");
|
|
} else {
|
|
// Vérifie si les colonnes "description" et "qrCode" existent déjà
|
|
final columns = await db.rawQuery('PRAGMA table_info(products)');
|
|
final columnNames = columns.map((e) => e['name'] as String).toList();
|
|
|
|
// Ajoute la colonne "description" si elle n'existe pas
|
|
if (!columnNames.contains('description')) {
|
|
try {
|
|
await db.execute("ALTER TABLE products ADD COLUMN description TEXT");
|
|
print("Colonne 'description' ajoutée.");
|
|
} catch (e) {
|
|
print("Erreur ajout colonne description : $e");
|
|
}
|
|
}
|
|
|
|
// Ajoute la colonne "qrCode" si elle n'existe pas
|
|
if (!columnNames.contains('qrCode')) {
|
|
try {
|
|
await db.execute("ALTER TABLE products ADD COLUMN qrCode TEXT");
|
|
print("Colonne 'qrCode' ajoutée.");
|
|
} catch (e) {
|
|
print("Erreur ajout colonne qrCode : $e");
|
|
}
|
|
}
|
|
// Ajoute la colonne "reference" si elle n'existe pas
|
|
if (!columnNames.contains('reference')) {
|
|
try {
|
|
await db.execute("ALTER TABLE products ADD COLUMN reference TEXT");
|
|
print("Colonne 'reference' ajoutée.");
|
|
} catch (e) {
|
|
print("Erreur ajout colonne reference : $e");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Future<int> createProduct(Product product) async {
|
|
final db = await database;
|
|
return await db.insert('products', product.toMap());
|
|
}
|
|
|
|
Future<List<Product>> getProducts() async {
|
|
final db = await database;
|
|
final maps = await db.query('products');
|
|
return List.generate(maps.length, (i) {
|
|
return Product.fromMap(maps[i]);
|
|
});
|
|
}
|
|
|
|
Future<int> updateProduct(Product product) async {
|
|
final db = await database;
|
|
return await db.update(
|
|
'products',
|
|
product.toMap(),
|
|
where: 'id = ?',
|
|
whereArgs: [product.id],
|
|
);
|
|
}
|
|
|
|
Future<int> deleteProduct(int? id) async {
|
|
final db = await database;
|
|
return await db.delete(
|
|
'products',
|
|
where: 'id = ?',
|
|
whereArgs: [id],
|
|
);
|
|
}
|
|
|
|
Future<List<String>> getCategories() async {
|
|
final db = await database;
|
|
final result = await db.rawQuery('SELECT DISTINCT category FROM products');
|
|
return List.generate(
|
|
result.length, (index) => result[index]['category'] as String);
|
|
}
|
|
|
|
Future<List<Product>> getProductsByCategory(String category) async {
|
|
final db = await database;
|
|
final maps = await db
|
|
.query('products', where: 'category = ?', whereArgs: [category]);
|
|
return List.generate(maps.length, (i) {
|
|
return Product.fromMap(maps[i]);
|
|
});
|
|
}
|
|
|
|
Future<int> updateStock(int id, int stock) async {
|
|
final db = await database;
|
|
return await db
|
|
.rawUpdate('UPDATE products SET stock = ? WHERE id = ?', [stock, id]);
|
|
}
|
|
}
|
|
|