Esempio n. 1
0
        /// <summary>Gets user information, validates and calls <see cref="homeController"/> to login the user.</summary>
        /// <exception cref="FormatException">Value must be an integer.</exception>
        public void Login()
        {
            int role = 0;

            do
            {
                Console.Clear();
                Console.Write("Enter 1 for donor and 2 for patient: ");
                try
                {
                    if (int.TryParse(Console.ReadLine(), out role) == false)
                    {
                        throw new FormatException("Value must be an integer.");
                    }
                }
                catch (FormatException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.Message);
                    Return();
                }
            } while (role != 1 && role != 2);

            Console.Write("Email: ");
            string email = Console.ReadLine();

            Console.Write("Password: "******"Invalid credentials.");
                    Console.ResetColor();
                }

                break;

            case 2:
                var patient = homeController.LoginAsPatient(email, password);
                if (patient != null)
                {
                    patientMenu = new PatientMenu(patient);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Invalid credentials.");
                    Console.ResetColor();
                }

                break;
            }
            Return();
        }
Esempio n. 2
0
        /// <summary>Gets user email and password, validates, and calls <see cref="homeController"/> to register user.</summary>
        /// <exception cref="FormatException">Value must be an integer.</exception>
        /// <exception cref="Exception">Invalid email</exception>
        /// <exception cref="Exception">Email already in use.</exception>
        public void Register()
        {
            int role = 0;

            do
            {
                Console.Clear();
                Console.Write("Enter 1 for donor and 2 for patient: ");
                try
                {
                    if (int.TryParse(Console.ReadLine(), out role) == false)
                    {
                        throw new FormatException("Value must be an integer.");
                    }
                }
                catch (FormatException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.Message);
                    Return();
                }
            } while (role != 1 && role != 2);

            Console.Write("Email: ");
            string email = Console.ReadLine();

            try
            {
                Console.ForegroundColor = ConsoleColor.Red;
                if (homeController.ValidateEmail(email) == false)
                {
                    throw new Exception("Invalid email");
                }
                if (homeController.EmailAlreadyExists(email, role))
                {
                    throw new Exception("Email already in use.");
                }
                Console.ResetColor();
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Return();
                return;
            }

            Console.Write("Password: "******"Password must be at least 6 symbols.");
                Return();
                return;
            }

            Console.Write("Repeat password: "******"Password mismatch.");
                Return();
                return;
            }

            password = homeController.HashPassword(password);

            switch (role)
            {
            case 1:
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                if (homeController.InvalidName(name))
                {
                    Console.WriteLine("Name cannot contain other symbols or be less than 2 letters.");
                    Return();
                    return;
                }

                Console.Write("Phone number: ");
                string phoneNumber = Console.ReadLine();
                if (homeController.InvalidPhoneNumber(phoneNumber))
                {
                    Console.WriteLine("Phone number should be 10 symbols and all numbers.");
                    Return();
                    return;
                }

                string status = "Available";

                Console.Write("Blood group: ");
                string bloodGroup = Console.ReadLine();
                if (homeController.InvalidBloodGroup(bloodGroup))
                {
                    Console.WriteLine("Blood group is invalid");
                    Return();
                    return;
                }

                if (homeController.RegisterAsDonor(email, password, name, phoneNumber, status, bloodGroup))
                {
                    Console.WriteLine("Register successful! Redirecting...");
                    donorMenu = new DonorMenu(homeController.LoginAsDonor(email, password));
                }
                else
                {
                    Console.WriteLine("Register failed. Please try again.");
                }

                break;
            }

            case 2:
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                if (homeController.InvalidName(name))
                {
                    Console.WriteLine("Name cannot contain other symbols or be less than 2 letters.");
                    Return();
                    return;
                }

                Console.Write("Phone number: ");
                string phoneNumber = Console.ReadLine();
                if (homeController.InvalidPhoneNumber(phoneNumber))
                {
                    Console.WriteLine("Phone number should be 10 symbols and all numbers.");
                    Return();
                    return;
                }

                Console.Write("Blood group: ");
                string bloodGroup = Console.ReadLine();
                if (homeController.InvalidBloodGroup(bloodGroup))
                {
                    Console.WriteLine("Enter a valid blood group.");
                    Return();
                    return;
                }

                Console.Write("Diagnose: ");
                string diagnose = Console.ReadLine();
                if (homeController.InvalidDiagnose(diagnose))
                {
                    Console.WriteLine("Diagnose cannot be less than 3 symbols and must be all letters.");
                    Return();
                    return;
                }

                if (homeController.RegisterAsPatient(email, password, name, phoneNumber, bloodGroup, diagnose))
                {
                    Console.WriteLine("Register successful! Redirecting...");
                    patientMenu = new PatientMenu(homeController.LoginAsPatient(email, password));
                }
                else
                {
                    Console.WriteLine("Register failed. Please try again.");
                }
                break;
            }
            }
            Return();
        }