Пример #1
0
        public bool Reader(OperationType type, ref AddressBooks addressBooks)
        {
            LogDetails           logDetails = new LogDetails();
            IDataSourceOperation dataSourceOperation;

            if (type == OperationType.Mock)
            {
                dataSourceOperation = new MockDataSourceOperation();
            }
            else
            {
                dataSourceOperation = new JsonOperation();
            }
            try
            {
                addressBooks = DictToListMapping.ListToDictionary(Reader(dataSourceOperation));
            }
            catch (Exception e)
            {
                logDetails.LogDebug("IO Error in Reading operation");
                logDetails.LogError(e.Message);
                return(false);
            }
            return(true);
        }
Пример #2
0
        public static void ReadFromSqlServer(ref AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping();

            using (SqlConnection connection = new SqlConnection(path))
            {
                SqlCommand command = new SqlCommand("select * from AddressBook", connection);
                connection.Open();
                SqlDataReader dataReader = command.ExecuteReader();
                while (dataReader.Read())
                {
                    dictToList.AddressBookName.Add(dataReader["AddressBookName"].ToString());
                    dictToList.ContactName.Add(dataReader["ContactName"].ToString());
                    dictToList.FirstName.Add(dataReader["FirstName"].ToString());
                    dictToList.LastName.Add(dataReader["LastName"].ToString());
                    dictToList.City.Add(dataReader["City"].ToString());
                    dictToList.State.Add(dataReader["State"].ToString());
                    dictToList.ZipCode.Add(dataReader["Zip"].ToString());
                    dictToList.PhoneNumber.Add(dataReader["Phone"].ToString());
                    dictToList.Email.Add(dataReader["Email"].ToString());
                    dictToList.DateAdded.Add((DateTime)dataReader["DateAdded"]);
                }
            }
            addressBooks = DictToListMapping.ListToDictionary(dictToList);
        }
Пример #3
0
        public bool WriteToDataSource(DictToListMapping dictToList)
        {
            RestClient    client      = new RestClient("http://localhost:3000");
            JObject       jObjectbody = new JObject();
            IRestResponse response    = new RestResponse();
            RestRequest   request;

            for (int i = 0; i < dictToList.AddressBookName.Count; i++)
            {
                jObjectbody.Add("AddressBookName", dictToList.AddressBookName[i]);
                jObjectbody.Add("ContactName", dictToList.ContactName[i]);
                jObjectbody.Add("FirstName", dictToList.FirstName[i]);
                jObjectbody.Add("LastName", dictToList.LastName[i]);
                jObjectbody.Add("City", dictToList.City[i]);
                jObjectbody.Add("State", dictToList.State[i]);
                jObjectbody.Add("Zip", dictToList.ZipCode[i]);
                jObjectbody.Add("PhoneNumber", dictToList.PhoneNumber[i]);
                jObjectbody.Add("Email", dictToList.Email[i]);
                jObjectbody.Add("DateAdded", dictToList.Email[i]);

                request = new RestRequest("/AddressBook", Method.POST);
                request.AddParameter("application/Json", jObjectbody, ParameterType.RequestBody);

                response = client.Execute(request);
            }

            return(response.StatusCode == System.Net.HttpStatusCode.Created);
        }
Пример #4
0
        /// <summary>
        /// Read from CSV file to store in address books
        /// </summary>
        /// <param name="addressBooks"></param>
        public static void ReadFromCsv(ref AddressBooks addressBooks)
        {
            DictToListMapping dictToListMapping = new DictToListMapping();

            var lines = File.ReadAllLines(path);
            var csv   = from line in lines
                        select(line.Split('\n')).ToArray();

            DictToListMapping dictToList = new DictToListMapping();
            int index = 0;

            foreach (var words in csv)
            {
                if (index == 0)
                {
                    index++;
                    continue;
                }
                var values = words[0].Split(',');

                dictToList.AddressBookName.Add(values[0]);
                dictToList.ContactName.Add(values[1]);
                dictToList.FirstName.Add(values[2]);
                dictToList.LastName.Add(values[3]);
                dictToList.City.Add(values[4]);
                dictToList.State.Add(values[5]);
                dictToList.ZipCode.Add(values[6]);
                dictToList.PhoneNumber.Add(values[7]);
                dictToList.Email.Add(values[8]);
            }

            addressBooks = DictToListMapping.ListToDictionary(dictToList);
        }
Пример #5
0
        public DictToListMapping ReadFromDataSource()
        {
            DictToListMapping dictToList = new DictToListMapping();
            string            jsonData   = File.ReadAllText(path);

            dictToList = JsonConvert.DeserializeObject <DictToListMapping>(jsonData);
            return(dictToList);
        }
Пример #6
0
        /// <summary>
        /// Write addressbooks to JSON file
        /// </summary>
        /// <param name="addressBooks"></param>
        public static void WriteToJson(AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));

            string jsonData = JsonConvert.SerializeObject(dictToList);

            File.WriteAllText(path, jsonData);
        }
Пример #7
0
        /// <summary>
        /// Read from JSON file to store in address books
        /// </summary>
        /// <param name="addressBooks"></param>
        public static void ReadFromJson(ref AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping();

            string jsonData = File.ReadAllText(path);

            dictToList = JsonConvert.DeserializeObject <DictToListMapping>(jsonData);

            addressBooks = DictToListMapping.ListToDictionary(dictToList);
        }
Пример #8
0
        public void ReadFromDataSource_ShouldPopulateAddressBookObject_WithDataFromJsonFile()
        {
            //Arrange
            DictToListMapping dictToList    = new DictToListMapping();
            JsonOperation     jsonOperation = new JsonOperation();

            //Act
            dictToList = jsonOperation.ReadFromDataSource();
            //Assert
            Assert.AreEqual("General", dictToList.AddressBookName[0]);
            Assert.AreEqual("Jyoti RanjanMishra", dictToList.ContactName[0]);
        }
Пример #9
0
        public static void WriteToSqlServer(AddressBooks addressBooks)
        {
            DeleteAllRows();

            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));

            DataSet   dataSet   = new DataSet();
            DataTable dataTable = new DataTable("AddressBook");

            dataTable.Columns.Add("AddressBookName", typeof(string));
            dataTable.Columns.Add("ContactName", typeof(string));
            dataTable.Columns.Add("FirstName", typeof(string));
            dataTable.Columns.Add("LastName", typeof(string));
            dataTable.Columns.Add("City", typeof(string));
            dataTable.Columns.Add("State", typeof(string));
            dataTable.Columns.Add("Zip", typeof(string));
            dataTable.Columns.Add("Phone", typeof(string));
            dataTable.Columns.Add("Email", typeof(string));
            dataTable.Columns.Add("DateAdded", typeof(DateTime));

            dataSet.Tables.Add(dataTable);
            dataSet.Tables[0].TableName = "AddressBook";
            for (int i = 0; i < dictToList.AddressBookName.Count; i++)
            {
                DataRow row = dataSet.Tables[0].NewRow();

                row[0] = dictToList.AddressBookName[i];
                row[1] = dictToList.ContactName[i];
                row[2] = dictToList.FirstName[i];
                row[3] = dictToList.LastName[i];
                row[4] = dictToList.City[i];
                row[5] = dictToList.State[i];
                row[6] = dictToList.ZipCode[i];
                row[7] = dictToList.PhoneNumber[i];
                row[8] = dictToList.Email[i];
                row[9] = dictToList.DateAdded[i];

                dataSet.Tables["AddressBook"].Rows.Add(row);
            }

            using (SqlConnection connection = new SqlConnection(path))
            {
                connection.Open();
                SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from AddressBook", connection);

                SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
                int rowsUpdated           = dataAdapter.Update(dataSet, "AddressBook");
                Console.WriteLine("Rows Affected " + rowsUpdated);
            }
        }
        public DictToListMapping ReadFromDataSource()
        {
            DictToListMapping dictToList = new DictToListMapping();

            dictToList.AddressBookName.Add("TestAddressBook");
            dictToList.ContactName.Add("TestContactName");
            dictToList.FirstName.Add("TestFirstName");
            dictToList.LastName.Add("TestLastName");
            dictToList.City.Add("TestCity");
            dictToList.State.Add("TestState");
            dictToList.ZipCode.Add("TestZip");
            dictToList.PhoneNumber.Add("TestPhone");
            dictToList.Email.Add("TestEmail");
            dictToList.DateAdded.Add(new DateTime(2020, 01, 01));
            return(dictToList);
        }
Пример #11
0
        public DictToListMapping ReadFromDataSource()
        {
            DictToListMapping dictToList = new DictToListMapping();

            dictToList.AddressBookName = JsonConvert.DeserializeObject <List <string> >(GetDataList("AddressBookName").Content);
            dictToList.ContactName     = JsonConvert.DeserializeObject <List <string> >(GetDataList("ContactName").Content);
            dictToList.FirstName       = JsonConvert.DeserializeObject <List <string> >(GetDataList("FirstName").Content);
            dictToList.LastName        = JsonConvert.DeserializeObject <List <string> >(GetDataList("LastName").Content);
            dictToList.City            = JsonConvert.DeserializeObject <List <string> >(GetDataList("City").Content);
            dictToList.State           = JsonConvert.DeserializeObject <List <string> >(GetDataList("State").Content);
            dictToList.ZipCode         = JsonConvert.DeserializeObject <List <string> >(GetDataList("ZipCode").Content);
            dictToList.PhoneNumber     = JsonConvert.DeserializeObject <List <string> >(GetDataList("PhoneNumber").Content);
            dictToList.Email           = JsonConvert.DeserializeObject <List <string> >(GetDataList("Email").Content);
            dictToList.DateAdded       = JsonConvert.DeserializeObject <List <DateTime> >(GetDataList("DateAdded").Content);

            return(dictToList);
        }
        public void ReadFromDataSource_ShouldPopulateAddressBookObject_WithDataFromJsonServerFile()
        {
            //Arrange
            DictToListMapping   dictToList = new DictToListMapping();
            JsonServerOperation jsonServer = new JsonServerOperation();

            //Act
            dictToList = jsonServer.ReadFromDataSource();
            //Assert
            Assert.AreEqual("General", dictToList.AddressBookName[0]);
            Assert.AreEqual("Jyoti RanjanMishra", dictToList.ContactName[0]);
            Assert.AreEqual("Jyoti Ranjan", dictToList.FirstName[0]);
            Assert.AreEqual("Mishra", dictToList.LastName[0]);
            Assert.AreEqual("Baripada", dictToList.City[0]);
            Assert.AreEqual("Odisha", dictToList.State[0]);
            Assert.AreEqual("970173", dictToList.ZipCode[0]);
            Assert.AreEqual("*****@*****.**", dictToList.Email[0]);
            Assert.AreEqual(new System.DateTime(2020, 11, 13), dictToList.DateAdded[0].Date);
        }
        public void WriteToDataSource_ShouldWriteToJsonServer_WithSpecifiedData()
        {
            //Arrange
            DictToListMapping dictToList = new DictToListMapping();

            dictToList.AddressBookName.Add("Testing");
            dictToList.ContactName.Add("Testing");
            dictToList.FirstName.Add("Testing");
            dictToList.LastName.Add("Testing");
            dictToList.City.Add("Testing");
            dictToList.State.Add("Testing");
            dictToList.ZipCode.Add("Testing");
            dictToList.PhoneNumber.Add("Testing");
            dictToList.Email.Add("Testing");
            dictToList.DateAdded.Add(new System.DateTime(2020, 01, 01));
            JsonServerOperation jsonServer = new JsonServerOperation();
            //Act
            bool status = jsonServer.WriteToDataSource(dictToList);

            //Assert
            Assert.IsTrue(status);
        }
Пример #14
0
        /// <summary>
        /// Write addressbooks to CSV file
        /// </summary>
        /// <param name="addressBooks"></param>
        public static void WriteToCsv(AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));
            List <string[]>   records    = new List <string[]>();

            string[] person;

            for (int i = 0; i < dictToList.AddressBookName.Count; i++)
            {
                person = new string[9];

                person[0] = dictToList.AddressBookName[i];
                person[1] = dictToList.ContactName[i];
                person[2] = dictToList.FirstName[i];
                person[3] = dictToList.LastName[i];
                person[4] = dictToList.City[i];
                person[5] = dictToList.State[i];
                person[6] = dictToList.ZipCode[i];
                person[7] = dictToList.PhoneNumber[i];
                person[8] = dictToList.Email[i];

                records.Add(person);
            }

            Type type = dictToList.GetType();

            PropertyInfo[] propertyInfo = type.GetProperties();

            using (var writer = new StreamWriter(path))
            {
                writer.WriteLine(string.Join(",", propertyInfo.Select(p => p.Name)));
                for (int i = 0; i < records.Count; i++)
                {
                    writer.WriteLine(string.Join(",", records[i]));
                }
            }
        }
 public bool WriteToDataSource(DictToListMapping dictToList)
 {
     return(false);
 }
Пример #16
0
 public bool WriteToDataSource(DictToListMapping dictToList)
 {
     throw new NotImplementedException();
 }