Пример #1
0
        /// <summary>
        /// From csv file, retrieve a bookmanifest to add to a list do bookmanifest later
        /// </summary>
        /// <param name="csvFilePath"></param>
        /// <returns></returns>
        public static List <BookManifest> CsvToManifestObject(String csvFilePath, List <BookManifest> booksManifests)
        {
            BookManifest bookManifest = new BookManifest();

            CsvParserOptions         csvParserOptions = new CsvParserOptions(true, ',');
            CsvReaderOptions         csvReaderOptions = new CsvReaderOptions(new[] { Environment.NewLine });
            CsvBookManifestMapping   csvMapper        = new CsvBookManifestMapping();
            CsvParser <BookManifest> csvParser        = new CsvParser <BookManifest>(csvParserOptions, csvMapper);
            var result = csvParser.ReadFromFile(csvFilePath, Encoding.ASCII).ToList();

            foreach (var item in result)
            {
                bookManifest = new BookManifest();

                bookManifest.Title       = item.Result.Title;
                bookManifest.Description = item.Result.Description;
                bookManifest.ISBN        = item.Result.ISBN;
                bookManifest.Author      = item.Result.Author;
                bookManifest.Genre       = item.Result.Genre;
                bookManifest.Pages       = item.Result.Pages;
                bookManifest.AgeRange    = item.Result.AgeRange;
                bookManifest.Price       = item.Result.Price;
                bookManifest.Quantity    = item.Result.Quantity;

                BookManifestOperations.CreateOrUpdateEntry(booksManifests, bookManifest);
            }

            return(booksManifests);
        }
Пример #2
0
        public static string ToJsonObjectSingle(BookManifest bookManifest)
        {
            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(BookManifest));
            MemoryStream msObj            = new MemoryStream();

            js.WriteObject(msObj, bookManifest);
            msObj.Position = 0;
            StreamReader sr = new StreamReader(msObj);

            string json = sr.ReadToEnd();

            return(json);
        }
Пример #3
0
        public static List <BookManifest> TabDelimitedToBookManifest(String filePath, List <BookManifest> booksManifests)
        {
            BookManifest bookManifest = new BookManifest();

            DataTable    datatable    = new DataTable();
            StreamReader streamreader = new StreamReader(filePath);

            char[]   delimiter     = new char[] { '\t' };
            string[] columnheaders = streamreader.ReadLine().Split(delimiter);
            foreach (string columnheader in columnheaders)
            {
                datatable.Columns.Add(columnheader); // I've added the column headers here.
            }

            while (streamreader.Peek() > 0)
            {
                DataRow datarow = datatable.NewRow();
                datarow.ItemArray = streamreader.ReadLine().Split(delimiter);
                datatable.Rows.Add(datarow);
            }

            foreach (DataRow row in datatable.Rows)
            {
                //Console.WriteLine(""----Row No: " + datatable.Rows.IndexOf(row) + "----"");

                bookManifest = new BookManifest();

                bookManifest.Title       = row.ItemArray[0].ToString();
                bookManifest.Description = row.ItemArray[1].ToString();
                bookManifest.ISBN        = Convert.ToInt64(row.ItemArray[2].ToString());
                bookManifest.Author      = row.ItemArray[3].ToString();
                bookManifest.Genre       = row.ItemArray[4].ToString();
                bookManifest.Pages       = Convert.ToInt32(row.ItemArray[5].ToString());
                bookManifest.AgeRange    = row.ItemArray[6].ToString();
                bookManifest.Price       = row.ItemArray[7].ToString();
                bookManifest.Quantity    = Convert.ToInt32(row.ItemArray[8].ToString());


                BookManifestOperations.CreateOrUpdateEntry(booksManifests, bookManifest);

                //booksManifests.Add(bookManifest);
            }

            return(booksManifests);
        }
Пример #4
0
        public static void CreateOrUpdateEntry(List <BookManifest> booksManifests, BookManifest bookManifest)
        {
            //int convertedISBN = 0;
            //int.TryParse(bookManifest.ISBN, out convertedISBN);

            if (!BookManifestOperations.IsIsbnExistInList(booksManifests, bookManifest.ISBN))
            {
                booksManifests.Add(bookManifest);
            }
            else
            {
                var          dict = booksManifests.ToDictionary(x => x.ISBN);
                BookManifest found;
                if (dict.TryGetValue(bookManifest.ISBN, out found))
                {
                    found.Quantity += bookManifest.Quantity;
                }
            }
        }