Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreatePersonViewModel.cs @ 6f451cc1

Historique | Voir | Annoter | Télécharger (2,634 ko)

1
using GES_PAC.Model;
2
using System.Windows.Input;
3

    
4
namespace GES_PAC.ViewModel
5
{
6
    public class CreatePersonViewModel : BaseViewModel
7
    {
8
        #region Attributs
9
        private string _nom;
10
        private string _prenom;
11
        private string _email;
12
        private bool _isFormValid;
13
        #endregion
14

    
15
        #region Commandes
16
        public ICommand CreatePersonCommand { get; }
17
        #endregion
18

    
19
        #region Propriétés
20
        public string Nom
21
        {
22
            get => _nom;
23
            set
24
            {
25
                _nom = value;
26
                OnPropertyChanged();
27
                ValidateForm();
28
            }
29
        }
30
        
31
        public string Prenom
32
        {
33
            get => _prenom;
34
            set
35
            {
36
                _prenom = value;
37
                OnPropertyChanged();
38
                ValidateForm();
39
            }
40
        }
41
        
42
        public string Email
43
        {
44
            get => _email;
45
            set
46
            {
47
                _email = value;
48
                OnPropertyChanged();
49
                ValidateForm();
50
            }
51
        }
52
        
53
        public bool IsFormValid
54
        {
55
            get => _isFormValid;
56
            set
57
            {
58
                _isFormValid = value;
59
                OnPropertyChanged();
60
            }
61
        }
62

    
63
        public bool NomError { get; private set; } = true;
64
        public bool PrenomError { get; private set; } = true;
65
        public bool EmailError { get; private set; } = true;
66
        #endregion
67

    
68
        #region Constructeurs
69
        public CreatePersonViewModel()
70
        {
71
            CreatePersonCommand = new Command(async () => await CreatePerson());
72
        }
73
        #endregion
74

    
75
        #region Méthodes
76
        private async Task CreatePerson()
77
        {
78
            var newTechnician = new Personne(Nom, Prenom, Email);
79
            App.Db.Personne.Add(newTechnician);
80
            await App.Db.SaveChangesAsync();
81

    
82
            PersonViewModel.Instance.Persons.Add(newTechnician);
83
            await Shell.Current.GoToAsync("//Main");
84
        }
85

    
86
        private void ValidateForm()
87
        {
88
            NomError = string.IsNullOrWhiteSpace(Nom) || Nom.Length < 3;
89
            PrenomError = string.IsNullOrWhiteSpace(Prenom) || Prenom.Length < 3;
90
            EmailError = string.IsNullOrWhiteSpace(Email) || Email.Length < 10;
91

    
92
            OnPropertyChanged(nameof(NomError));
93
            OnPropertyChanged(nameof(PrenomError));
94
            OnPropertyChanged(nameof(EmailError));
95

    
96
            IsFormValid = !NomError && !PrenomError && !EmailError;
97

    
98
            (CreatePersonCommand as Command)?.ChangeCanExecute();
99
        }
100
        #endregion
101
    }
102
}