Untitled

 avatar
unknown
plain_text
a month ago
2.3 kB
1
Indexable
using Airline_Reservation_System.Models;
using System;
using System.ComponentModel;
using System.IO;
using System.Text.Json;
using System.Windows.Input;

namespace Airline_Reservation_System.ViewModels
{
    public class RegistrationViewModel : INotifyPropertyChanged
    {
        private string _name;
        private string _email;
        private string _password;

        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }

        public string Email
        {
            get => _email;
            set
            {
                _email = value;
                OnPropertyChanged(nameof(Email));
            }
        }

        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                OnPropertyChanged(nameof(Password));
            }
        }

        public ICommand SignupCommand { get; }

        public RegistrationViewModel()
        {
            SignupCommand = new RelayCommand(SaveRegistrationData, CanExecuteSignup);
        }

        private bool CanExecuteSignup(object parameter)
        {
            return !string.IsNullOrEmpty(Name) &&
                   !string.IsNullOrEmpty(Email) &&
                   !string.IsNullOrEmpty(Password);
        }

        private void SaveRegistrationData(object parameter)
        {
            var user = new UserRegistrationModel
            {
                Name = Name,
                Email = Email,
                Password = Password
            };

            string jsonData = System.Text.Json.JsonSerializer.Serialize(user, new JsonSerializerOptions { WriteIndented = true });

            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UserData.json");
            File.WriteAllText(filePath, jsonData);

            // Reset fields
            Name = string.Empty;
            Email = string.Empty;
            Password = string.Empty;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Leave a Comment