From 5cb668aa933cbab4b30d0ba25c8e98f5d14d0c68 Mon Sep 17 00:00:00 2001 From: besbota Date: Tue, 28 Jan 2025 13:50:12 +0100 Subject: [PATCH] Add class Fournisseur and button for Modif / Delete / Add --- MauiApp1/App.xaml | 4 +- MauiApp1/App.xaml.cs | 19 +++-- MauiApp1/AppShell.xaml | 12 ++- MauiApp1/AppShell.xaml.cs | 7 +- MauiApp1/Data/Database.cs | 59 +++++++++++++ MauiApp1/MainPage.xaml | 21 +---- MauiApp1/MainPage.xaml.cs | 16 ++-- MauiApp1/MauiApp1.csproj | 2 + MauiApp1/MauiProgram.cs | 10 +++ MauiApp1/Model/Fournisseur.cs | 19 +++++ MauiApp1/Services/FournisseurDatabase.cs | 39 +++++++++ MauiApp1/ViewModels/FournisseurViewModel.cs | 75 +++++++++++++++++ MauiApp1/Views/FournisseurPage.xaml | 48 +++++++++++ MauiApp1/Views/FournisseurPage.xaml.cs | 93 +++++++++++++++++++++ 14 files changed, 387 insertions(+), 37 deletions(-) create mode 100644 MauiApp1/Data/Database.cs create mode 100644 MauiApp1/Model/Fournisseur.cs create mode 100644 MauiApp1/Services/FournisseurDatabase.cs create mode 100644 MauiApp1/ViewModels/FournisseurViewModel.cs create mode 100644 MauiApp1/Views/FournisseurPage.xaml create mode 100644 MauiApp1/Views/FournisseurPage.xaml.cs diff --git a/MauiApp1/App.xaml b/MauiApp1/App.xaml index 8fb5f38..7201dba 100644 --- a/MauiApp1/App.xaml +++ b/MauiApp1/App.xaml @@ -1,4 +1,4 @@ - + - + \ No newline at end of file diff --git a/MauiApp1/App.xaml.cs b/MauiApp1/App.xaml.cs index e2de305..a69f453 100644 --- a/MauiApp1/App.xaml.cs +++ b/MauiApp1/App.xaml.cs @@ -1,11 +1,20 @@ -namespace MauiApp1; +using Microsoft.Maui; +using Microsoft.Maui.Controls; +using MauiApp1.Data; -public partial class App : Application +namespace MauiApp1 { - public App() + public partial class App : Application { - InitializeComponent(); + private readonly Database _database; - MainPage = new AppShell(); + public App(Database database) + { + InitializeComponent(); + _database = database; + MainPage = new AppShell(); + } + + public static Database Database => Current.Handler.MauiContext.Services.GetService(); } } \ No newline at end of file diff --git a/MauiApp1/AppShell.xaml b/MauiApp1/AppShell.xaml index 5dd865e..bb5a1be 100644 --- a/MauiApp1/AppShell.xaml +++ b/MauiApp1/AppShell.xaml @@ -3,6 +3,7 @@ x:Class="MauiApp1.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" + xmlns:views="clr-namespace:MauiApp1.Views" xmlns:local="clr-namespace:MauiApp1" Shell.FlyoutBehavior="Disabled" Title="MauiApp1"> @@ -11,5 +12,14 @@ Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> + + + + + - + + + + + \ No newline at end of file diff --git a/MauiApp1/AppShell.xaml.cs b/MauiApp1/AppShell.xaml.cs index 2401c8f..74efde4 100644 --- a/MauiApp1/AppShell.xaml.cs +++ b/MauiApp1/AppShell.xaml.cs @@ -1,9 +1,14 @@ -namespace MauiApp1; +using MauiApp1.Views; + +namespace MauiApp1; public partial class AppShell : Shell { public AppShell() { InitializeComponent(); + + // Enregistrement des routes + Routing.RegisterRoute("fournisseurpage", typeof(FournisseurPage)); } } \ No newline at end of file diff --git a/MauiApp1/Data/Database.cs b/MauiApp1/Data/Database.cs new file mode 100644 index 0000000..9049348 --- /dev/null +++ b/MauiApp1/Data/Database.cs @@ -0,0 +1,59 @@ +using SQLite; +using MauiApp1.Model; + +namespace MauiApp1.Data +{ + public class Database + { + private readonly SQLiteAsyncConnection _database; + + public Database(string dbPath) + { + _database = new SQLiteAsyncConnection(dbPath); + _database.CreateTableAsync().Wait(); + } + + public async Task> GetFournisseursAsync() + { + try + { + return await _database.Table().ToListAsync(); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Erreur lors de la récupération des fournisseurs: {ex.Message}"); + throw; + } + } + + public async Task SaveFournisseurAsync(Fournisseur fournisseur) + { + try + { + if (fournisseur.Id != 0) + { + return await _database.UpdateAsync(fournisseur); + } + return await _database.InsertAsync(fournisseur); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Erreur lors de la sauvegarde du fournisseur: {ex.Message}"); + throw; + } + } + + public async Task DeleteFournisseurAsync(Fournisseur fournisseur) + { + try + { + return await _database.DeleteAsync(fournisseur); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Erreur lors de la suppression du fournisseur: {ex.Message}"); + throw; + } + } + } +} \ No newline at end of file diff --git a/MauiApp1/MainPage.xaml b/MauiApp1/MainPage.xaml index 4ff41e1..547a173 100644 --- a/MauiApp1/MainPage.xaml +++ b/MauiApp1/MainPage.xaml @@ -7,30 +7,15 @@ - -