//TODO: confirmation messages etc. (maybe use events, tuples with boolean return value or premethods which check input)
        //ADD CONTACT METHOD
        public bool AddContact(ContactBookLogic contactbooklogic, SQLConnection sql, long countLocations, string name, Location location, long phoneNumber, string mailAddress, string gender)
        {
            int LocationID = (int)sql.GetLocationID(location);

            Contact contact = new Contact
            {
                Name        = name,
                LocationID  = LocationID,
                PhoneNumber = phoneNumber,
                MailAddress = mailAddress,
                Gender      = gender
            };

            List <Contact> contactslist = sql.OutputContactTableToList();
            var            conIsDupe    = false;

            foreach (var con1 in contactslist)
            {
                if ((contact.Name == con1.Name) && (contact.LocationID == con1.LocationID) && (contact.PhoneNumber == con1.PhoneNumber) && (contact.MailAddress == con1.MailAddress) && (contact.Gender == con1.Gender))
                {
                    conIsDupe = true;
                }
            }

            if (!conIsDupe)
            {
                var CommandText = $"INSERT INTO contacts(Name, LocationID, PhoneNumber, MailAddress, Gender) VALUES('{contact.Name}', '{LocationID}', '{contact.PhoneNumber}', '{contact.MailAddress}', '{contact.Gender}');";
                sql.ExecuteNonQuery(CommandText);
            }
            return(conIsDupe);
        }
Esempio n. 2
0
        public void ReadAndAddContactFromCsvLine(string csvLine, SQLConnection sql)
        {
            string[] parts        = csvLine.Split(',');
            string   namePart     = InputChecker.CsvEmptyInputCheck(parts[0]);
            string   addressPart  = InputChecker.CsvEmptyInputCheck(parts[1]);
            string   cityNamePart = InputChecker.CsvEmptyInputCheck(parts[2]);

            long.TryParse(parts[3], out long a);
            long   phoneNumberPart = a;
            string mailAddressPart = InputChecker.CsvMailFormatCheck(parts[4]);
            string genderPart      = InputChecker.CsvGenderCheck(parts[5]);

            if (phoneNumberPart != 0 && genderPart != "wronginput" && namePart != "wronginput" && addressPart != "wronginput" && cityNamePart != "wronginput")
            {
                Location location = new Location()
                {
                    Address  = addressPart,
                    CityName = cityNamePart,
                };

                List <Location> locationslist = sql.OutputLocationTableToList();
                var             locIsDupe     = false;
                var             CommandText   = "";

                foreach (var loc1 in locationslist)
                {
                    if ((location.Address == loc1.Address) && (location.CityName == loc1.CityName))
                    {
                        locIsDupe = true;
                    }
                }

                if (!locIsDupe)
                {
                    CommandText = $"INSERT INTO locations(Address, CityName) VALUES ('{location.Address}', '{location.CityName}')";
                    sql.ExecuteNonQuery(CommandText);
                }
                //    TODOL: Message INFO: Location is duplicate and will not be added

                long LocationID = sql.GetLocationID(location);


                var contact = new Contact
                {
                    Name        = namePart,
                    LocationID  = (int)LocationID,
                    PhoneNumber = phoneNumberPart,
                    MailAddress = mailAddressPart,
                    Gender      = genderPart
                };

                List <Contact> contactslist = sql.OutputContactTableToList();
                var            conIsDupe    = false;

                foreach (var con1 in contactslist)
                {
                    if ((contact.Name == con1.Name) && (contact.LocationID == con1.LocationID) && (contact.PhoneNumber == con1.PhoneNumber) && (contact.MailAddress == con1.MailAddress) && (contact.Gender == con1.Gender))
                    {
                        conIsDupe = true;
                    }
                }

                if (!conIsDupe)
                {
                    CommandText = $"INSERT INTO contacts(Name, LocationID, PhoneNumber, MailAddress, Gender) VALUES('{contact.Name}', '{contact.LocationID}', '{contact.PhoneNumber}', '{contact.MailAddress}', '{contact.Gender}');";
                    sql.ExecuteNonQuery(CommandText);
                }
                // TODOL: Message INFO: Contact on {csvLine} is duplicate and will not be added
            }
            //   TODOL: message WARNING: Wrong input on line: {csvLine}
        }