From 569d16e5717a3e7ef7dc7be52115abc9768a2506 Mon Sep 17 00:00:00 2001 From: lobroda Date: Sat, 14 Oct 2023 17:56:15 +0100 Subject: [PATCH] ADD : Ajout d'un contact (update view after created contact ToDo) --- LivreLand/View/ContactsView.xaml | 43 +++++++++---------- LivreLand/ViewModel/ContactsVM.cs | 32 ++++++++++++++ ViewModels/ManagerVM.cs | 70 ++++++++++++++++++++++++------- 3 files changed, 107 insertions(+), 38 deletions(-) diff --git a/LivreLand/View/ContactsView.xaml b/LivreLand/View/ContactsView.xaml index 7766c12..a9d53e4 100644 --- a/LivreLand/View/ContactsView.xaml +++ b/LivreLand/View/ContactsView.xaml @@ -26,7 +26,7 @@ - + @@ -44,33 +44,30 @@ Grid.Column="1"/> - - - - - - - - - + + AddContactDataForm()); + } + + #endregion + + #region Methods + + private void AddContactDataForm() + { + DataFormIsVisible = true; } #endregion diff --git a/ViewModels/ManagerVM.cs b/ViewModels/ManagerVM.cs index ad1bd68..22ec2f5 100644 --- a/ViewModels/ManagerVM.cs +++ b/ViewModels/ManagerVM.cs @@ -2,6 +2,7 @@ using PersonalMVVMToolkit; using System.Collections.ObjectModel; using System.Linq; +using System.Text.RegularExpressions; using System.Windows.Input; namespace ViewModels @@ -28,6 +29,8 @@ namespace ViewModels private AuthorVM selectedAuthor; private ContactVM selectedContact; + private string givenFirstName; + private string givenLastName; #endregion #region Properties @@ -121,6 +124,32 @@ namespace ViewModels } } + public string GivenFirstName + { + get { return givenFirstName; } + set + { + if (givenFirstName != value) + { + givenFirstName = value; + OnPropertyChanged(nameof(GivenFirstName)); + } + } + } + + public string GivenLastName + { + get { return givenLastName; } + set + { + if (givenLastName != value) + { + givenLastName = value; + OnPropertyChanged(nameof(GivenLastName)); + } + } + } + public string SearchTitle { get; private set; } public int Index @@ -222,7 +251,7 @@ namespace ViewModels GetPastBorrowingsCommand = new RelayCommand(() => GetPastBorrowings()); LendBookCommand = new RelayCommand((contactVM) => LendBook(contactVM)); GetContactsCommand = new RelayCommand(() => GetContacts()); - AddContactCommand = new RelayCommand((contactVM) => AddContact(contactVM)); + AddContactCommand = new RelayCommand(() => AddContact()); //GetBooksByTitleCommand = new RelayCommand(() => AllBooks = model.GetBooksByTitle(SearchTitle, Index, Count).Result.books.Select(book => new BookVM(book))); } @@ -540,22 +569,33 @@ namespace ViewModels OnPropertyChanged(nameof(AllContacts)); } - private async Task AddContact(ContactVM contactVM) + private async Task AddContact() { - Model.Contact contact = new Model.Contact(); - var resultContacts = await Model.GetContacts(Index, Count); - var allContacts = resultContacts.contacts; - foreach (var c in allContacts) - { - if (c.Id == contactVM.Id) - { - contact = c; - } - } - if (contact != null) + var result = await Model.GetContacts(Index, Count); + IEnumerable someContacts = result.contacts; + + int lastSequence = someContacts + .Where(c => Regex.IsMatch(c.Id, @"^/contacts/\d+$")) + .Select(c => int.Parse(Regex.Match(c.Id, @"\d+").Value)) + .DefaultIfEmpty(0) + .Max(); + + int newSequence = lastSequence + 1; + + string newId = $"/contacts/{newSequence:D2}"; + + var newContact = new Model.Contact { - await Model.AddContact(contact); - } + Id = newId, + FirstName = GivenFirstName, + LastName = GivenLastName + }; + + GivenFirstName = null; + GivenLastName = null; + + await Model.AddContact(newContact); + OnPropertyChanged(nameof(AllContacts)); } #endregion