Ejemplo n.º 1
0
 public AuthorWindow(string i_au_id)
 {
     InitializeComponent();
     au_id = i_au_id;
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             author au = context.authors.Find(this.au_id);
             fNameField.Text = au.au_fname;
             lNameField.Text = au.au_lname;
             cityField.Text = au.city;
             stateField.Text = au.state;
             addressField.Text = au.address;
             zipField.Text = au.zip;
             phoneField.Text = au.phone;
             contractField.IsChecked = au.contract;
             this.Title = au.au_fname + " " + au.au_lname;
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 2
0
 private void authorAddBtnClick(object sender, RoutedEventArgs e)
 {
     AuthorWindow aw = new AuthorWindow();
     aw.ShowDialog();
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             authorDataGrid.ItemsSource = context.authors.ToList();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 3
0
 public MainWindow()
 {
     InitializeComponent();
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             publisherDataGrid.ItemsSource = context.publishers.ToList();
             authorDataGrid.ItemsSource = context.authors.ToList();
             titleDataGrid.ItemsSource = context.titles.ToList();
         }
     }
     catch(Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 4
0
 private void saveBtnClick(object sender, RoutedEventArgs e)
 {
     //Validate
     //Technically, the DB schema allows everything (except for the ID) to be null...
     bool IsOkay = true;
     if (nameField.Text == "") { MessageBox.Show("Input a name!"); IsOkay = false; }
     if (countryField.Text == "") { MessageBox.Show("Input a country!"); IsOkay = false; }
     if (cityField.Text == "") { MessageBox.Show("Input a city!"); IsOkay = false; }
     if (!IsOkay) return;
     //Save
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             publisher p = IsNew ? new publisher() : context.publishers.Find(this.pub_id);
             if(IsNew) p.pub_id = Guid.NewGuid().ToString().Substring(0, 4); //Coursemate's advice;
             p.pub_name = nameField.Text;
             p.country = countryField.Text;
             p.city = cityField.Text;
             p.state = stateField.Text;
             if (IsNew)
             {
                 p.pub_info = new pub_info();
                 p.pub_info.pr_info = infoField.Text;
                 p.pub_info.pub_id = p.pub_id;
                 p.pub_info.publisher = p;
                 context.publishers.Add(p); //Adding references with EF is so easy, I love it!
                 context.pub_info.Add(p.pub_info);
             } else
             {
                 p.pub_info.pr_info = infoField.Text;
             }
             context.SaveChanges();
             this.Close();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
     this.Close();
 }
Ejemplo n.º 5
0
 private void authorDeleteBtnClick(object sender, RoutedEventArgs e)
 {
     var choice = MessageBox.Show("Are you sure?", "Delete author", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No);
     if (choice == MessageBoxResult.Yes)
     {
         author selected = (author)authorDataGrid.SelectedItem;
         try
         {
             using (NET_MD2Entities context = new NET_MD2Entities())
             {
                 context.authors.Remove((from a in context.authors where a.au_id == selected.au_id select a).Single());
                 context.SaveChanges();
                 authorDataGrid.ItemsSource = context.authors.ToList();
             }
         }
         catch (Exception exc)
         {
             MessageBox.Show("Something went wrong! " + exc.Message);
         }
     }
 }
Ejemplo n.º 6
0
 public TitleWindow(string i_title_id)
 {
     InitializeComponent();
     title_id = i_title_id;
     populateFields();
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             title t = context.titles.Find(this.title_id);
             titleField.Text = t.title1;
             publishedField.Text = t.pubdate.ToString();
             notesField.Text = t.notes;
             this.Title = t.title1;
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 7
0
 private void saveBtnClick(object sender, RoutedEventArgs e)
 {
     //Validate
     bool IsOkay = true;
     if (fNameField.Text == "") { MessageBox.Show("Input a first name!"); IsOkay = false; }
     if (lNameField.Text == "") { MessageBox.Show("Input a last name!"); IsOkay = false; }
     if (phoneField.Text == "") { MessageBox.Show("Input a phone number!"); IsOkay = false; }
     if (zipField.Text.Length != 5) { MessageBox.Show("Zip must be 5 characters long!"); IsOkay = false; }
     else if (phoneField.Text.Length != 12) { MessageBox.Show("Phone number must be 12 digits long!"); IsOkay = false; } //Technically, characters are fine as well, the DB schema is weird
     if (!IsOkay) return;
     //Save
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             author a = IsNew ? new author() : context.authors.Find(this.au_id);
             if (IsNew)
             {
                 DateTime now = DateTime.Now;
                 string nowString = DateTime.Now.ToString("HHmmssfff"); //Would have used Guid, but the au_id constraint wants numbers only (I know I can delete it)
                 a.au_id = nowString.Substring(0, 3) + "-" + nowString.Substring(3, 2) + "-" + nowString.Substring(5, 4); //Coursemate's advice
             }
             a.au_fname = fNameField.Text;
             a.au_lname = lNameField.Text;
             a.city = cityField.Text;
             a.state = stateField.Text;
             a.address = addressField.Text;
             a.zip = zipField.Text;
             a.phone = phoneField.Text;
             a.contract = (bool)contractField.IsChecked;
             if (IsNew) { context.authors.Add(a); }
             context.SaveChanges();
             this.Close();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 8
0
 public PublisherWindow(string i_pub_id)
 {
     InitializeComponent();
     IsNew = false;
     pub_id = i_pub_id;
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             publisher pub = context.publishers.Find(this.pub_id);
             nameField.Text = pub.pub_name;
             countryField.Text = pub.country;
             cityField.Text = pub.city;
             if (pub.state != "  ") stateField.Text = pub.state; //For some reason NULL values in the DB end up as spaces here.
             infoField.Text = pub.pub_info.pr_info;
             this.Title = pub.pub_name;
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 9
0
 private void titleRefreshBtnClick(object sender, RoutedEventArgs e)
 {
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             titleDataGrid.ItemsSource = context.titles.ToList();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 10
0
 private void titleEditBtnClick(object sender, RoutedEventArgs e)
 {
     title selected = (title)titleDataGrid.SelectedItem;
     TitleWindow tw = new TitleWindow(selected.title_id);
     tw.ShowDialog();
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             titleDataGrid.ItemsSource = context.titles.ToList();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 11
0
 private void publisherRefreshBtnClick(object sender, RoutedEventArgs e)
 {
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             publisherDataGrid.ItemsSource = context.publishers.ToList(); //Refresh buttons are mostly meant for titles, if you update the publisher or author, the changes don't appear till you reload the list.
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 12
0
 private void publisherEditBtnClick(object sender, RoutedEventArgs e)
 {
     publisher selected = (publisher)publisherDataGrid.SelectedItem;
     PublisherWindow pw = new PublisherWindow(selected.pub_id);
     pw.ShowDialog();
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             publisherDataGrid.ItemsSource = context.publishers.ToList();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 13
0
 private void publisherAddBtnClick(object sender, RoutedEventArgs e)
 {
     PublisherWindow pw = new PublisherWindow();
     pw.ShowDialog();
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             publisherDataGrid.ItemsSource = context.publishers.ToList(); //This seems like a performance hazard, since I'm reloading the whole table after every edit.
             //But, as far as I know, that's the proper way to do it.
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
 }
Ejemplo n.º 14
0
        private void populateFields()
        {
            try
            {
                using (NET_MD2Entities context = new NET_MD2Entities())
                {
                    //Publishers
                    publisherField.ItemsSource = context.publishers.ToList();
                    title t = context.titles.Find(title_id);
                    if (!IsNew)
                    {
                        if (t.publisher == null) //The database allows null publishers, this is to enforce having a publisher
                        {
                            publisherField.SelectedIndex = 0;
                        }
                        else
                        {
                            publisherField.SelectedItem = t.publisher;
                        }
                    }
                    else
                    {
                        publisherField.SelectedIndex = 0;
                    }

                    //Authors
                    addAuthorList = context.authors.ToList();
                    addAuthorList.Sort();
                    if (!IsNew)
                    {
                        List<titleauthor> taList = (from ta in context.titleauthors
                                                    where ta.title_id == this.title_id
                                                    select ta).ToList();
                        foreach (titleauthor ta in taList) //Remove title authors from the list of authors we can add and add them to the box of authors for the title.
                        {
                            addAuthorList.Remove(ta.author);
                            authorList.Add(ta.author);
                        }
                    }
                    addAuthorBox.SelectedIndex = 0;
                    addAuthorBox.ItemsSource = addAuthorList;
                    authorBox.ItemsSource = authorList;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Something went wrong! " + exc.Message);
            }
        }
Ejemplo n.º 15
0
 private void saveBtnClick(object sender, RoutedEventArgs e)
 {
     //Validate
     bool IsOkay = true;
     if (titleField.Text == "") { MessageBox.Show("Input a title!"); IsOkay = false; }
     if (authorList.Count == 0) { MessageBox.Show("Title must have at least 1 author!"); IsOkay = false; }
     if (!IsOkay) return;
     //Save
     try
     {
         using (NET_MD2Entities context = new NET_MD2Entities())
         {
             title t = IsNew ? new title() : context.titles.Find(this.title_id);
             if (IsNew) t.title_id = Guid.NewGuid().ToString().Substring(0, 4); //Coursemate's advice
             t.title1 = titleField.Text;
             t.publisher = context.publishers.Find(((publisher)publisherField.SelectedItem).pub_id); //I can't just add the publisher instance from the list, since it's from a different context
             t.pub_id = t.publisher.pub_id;
             try { t.pubdate = DateTime.Parse(publishedField.Text); }
             catch(Exception exc)
             {
                 MessageBox.Show("Incorrect date format! " + exc.Message);
                 return;
             }
             t.notes = notesField.Text;
             t.type = IsNew ? "UNDECIDED" : t.type; //Even though the column in the DB has a default value, it somehow doesn't like it when it's not specified (possibly because t.type then is not initialized?)
             if (IsNew)
             {
                 title newTitle = context.titles.Add(t);
                 foreach (author a in authorBox.Items)
                 {
                     author au = context.authors.Find(a.au_id);
                     titleauthor ta = new titleauthor();
                     ta.au_id = a.au_id;
                     ta.title_id = this.title_id;
                     ta.title = newTitle;
                     ta.author = au;
                     newTitle.titleauthors.Add(ta);
                 }
             }
             else
             {
                 t.titleauthors.Clear(); //This is probably more efficient than comparing t.titleauthor to authorList (with the changed authors) and updating the changes
                 foreach (author a in authorList)
                 {
                     author au = context.authors.Find(a.au_id);
                     titleauthor newTa = new titleauthor();
                     newTa.au_id = a.au_id;
                     newTa.title_id = this.title_id;
                     newTa.author = au;
                     newTa.title = t;
                     t.titleauthors.Add(newTa);
                 }
             }
             context.SaveChanges();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Something went wrong! " + exc.Message);
     }
     this.Close();
 }