/// <summary>
        /// Opens specified XML file,
        /// sets all data to DataGridView and
        /// path to file into form header.
        /// </summary>
        /// <param name="sender">Object which raised the event (button).</param>
        /// <param name="e">Event data.</param>
        private void OpenXml_Click(object sender, EventArgs e)
        {
            if (openXMLFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string readXml = File.ReadAllText(openXMLFileDialog.FileName);

                    using (TextReader reader = new StringReader(readXml))
                    {
                        try
                        {
                            BookstoreModel bookstoreModel = (BookstoreModel)xmlSerializer.Deserialize(reader);
                            SetDataToDataGridView(bookstoreModel);
                            this.Text = Text + $" {openXMLFileDialog.FileName}";
                        }
                        catch (InvalidOperationException ex)
                        {
                            MessageBox.Show($"В файле есть ошибка! \n\nПолный текст ошибки: \n\n{ex.InnerException.Message}",
                                            "Ошибка в файле!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                catch (SecurityException ex)
                {
                    MessageBox.Show($"Ошибка безопасности.\n\nПолный текст ошибки: {ex.Message}\n\n");
                }
            }
        }
        /// <summary>
        /// Assigns all data from root XML element to
        /// BookStoreDataGridView
        /// </summary>
        /// <param name="bookstore">Root XML element</param>
        private void SetDataToDataGridView(BookstoreModel bookstore)
        {
            BookstoreDataGridView.Rows.Clear();

            foreach (BookModel book in bookstore.Books)
            {
                BookstoreDataGridView.Rows.Add(book.Title.Text, book.Year, book.Title.Lang, string.Join(";", book.Authors), book.Category, book.Price);
            }
        }
        /// <summary>
        /// Parses and maps BookstoreDataGridView into BookstoreModel
        /// for further use.
        /// TODO: remove row index (use column names instead)
        /// </summary>
        /// <returns>Root object of XML document</returns>
        private BookstoreModel GetBookstoreModelFromDataGridView()
        {
            BookstoreModel bookStoreModel = new BookstoreModel
            {
                Books = new List <BookModel>()
            };

            // if price or year is text assign default value
            foreach (DataGridViewRow dr in BookstoreDataGridView.Rows)
            {
                decimal price = default;
                try
                {
                    price = decimal.Parse(dr.Cells[5].Value.ToString());
                }
                catch
                {
                    MessageBox.Show($"Неверный формат цены!",
                                    "Ошибка формата цены", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                int year = default;
                try
                {
                    year = int.Parse(dr.Cells[1].Value.ToString());
                }
                catch
                {
                    MessageBox.Show($"Неверный формат года выпуска!",
                                    "Ошибка формата года выпуска", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                bookStoreModel.Books.Add(new BookModel
                {
                    Title = new TitleModel
                    {
                        Text = dr.Cells[0].Value.ToString(),
                        Lang = dr.Cells[2].Value?.ToString()
                    },
                    Year     = year,
                    Authors  = dr.Cells[3].Value.ToString().Split(';').ToList(),
                    Category = dr.Cells[4].Value.ToString(),
                    Price    = price
                });
            }

            return(bookStoreModel);
        }
 public AuthorsController(BookstoreModel db)
 {
     this.db = db;
 }
Example #5
0
 public PublishersController(BookstoreModel db)
 {
     this.db = db;
 }
 public void updatebook([FromBody] BookstoreModel data)
 {
     bookstroe.ReplaceOne(it => it.Id == data.Id, data);
 }
 public void insertbook([FromBody] BookstoreModel data)
 {
     /* data.Id = Guid.NewGuid().ToString(); */
     bookstroe.InsertOne(data);
 }
 public CategoriesController(BookstoreModel db)
 {
     this.db = db;
 }