private async void BtnSaveVacancyCategory_Click(object sender, RoutedEventArgs e) { Mouse.OverrideCursor = Cursors.Wait; string vacancyCategory = tbxVacancyCategory.Text; string vacancyLink = tbxVacancyLink.Text; if (string.IsNullOrEmpty(vacancyCategory) || string.IsNullOrEmpty(vacancyLink)) { MessageBox.Show("Заполните все поля"); Mouse.OverrideCursor = null; return; } try { using (VacancyModelContainer db = new VacancyModelContainer()) { if (db.Categories.Any(a => a.Name == vacancyCategory)) { MessageBox.Show("Такая категория вакансий в базе уже существует"); return; } } XDocument xDoc = XDocument.Load(vacancyLink); using (VacancyModelContainer db = new VacancyModelContainer()) { Category cat = new Category() { Name = vacancyCategory }; db.Categories.Add(cat); foreach (XElement xElem in xDoc.Element("rss").Element("channel").Elements("item")) { Vacancy vac = new Vacancy(); vac.Title = xElem.Element("title").Value; vac.Link = xElem.Element("link").Value; vac.Description = xElem.Element("description").Value; vac.PubDate = Convert.ToDateTime(xElem.Element("pubDate").Value); vac.Author = xElem.Element("author").Value; vac.Category = cat; db.Vacancies.Add(vac); } await db.SaveChangesAsync(); } Mouse.OverrideCursor = null; MessageBox.Show("Вакансии сохранены"); tbxVacancyCategory.Text = ""; tbxVacancyLink.Text = ""; } catch (Exception ex) { Mouse.OverrideCursor = null; MessageBox.Show(ex.Message); } }
private void LoadStatistic() { using (VacancyModelContainer db = new VacancyModelContainer()) { lblNumberOfRecords.Content = db.Vacancies.Count(); lblNumberOfRecords2.Content = db.Categories.Count(); } }
private void BtnClearCategories_Click(object sender, RoutedEventArgs e) { try { using (VacancyModelContainer db = new VacancyModelContainer()) { db.Database.ExecuteSqlCommand("TRUNCATE TABLE Categories"); lblNumberOfRecords2.Content = db.Categories.Count(); } MessageBox.Show("Таблица Categories очищена"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void BtnSearchVacancy_Click(object sender, RoutedEventArgs e) { Mouse.OverrideCursor = Cursors.Wait; string searchValue = tbxSearchValue.Text; if (string.IsNullOrEmpty(searchValue)) { Mouse.OverrideCursor = null; MessageBox.Show("Введите искомое значение"); return; } bool includeCategory = (bool)cbxCategory.IsChecked; bool includeDateOfPublish = (bool)cbxDateOfPublish.IsChecked; bool includeEmail = (bool)cbxEmail.IsChecked; bool includeDescription = (bool)cbxDescription.IsChecked; try { List <Vacancy> result = new List <Vacancy>(); using (VacancyModelContainer db = new VacancyModelContainer()) { if (includeCategory) { var categoryId = from cat in db.Categories where cat.Name.Contains(searchValue) select cat.CategoryId; (from catId in categoryId from vac in db.Vacancies where catId == vac.CategoryId select vac).ToList().ForEach(f => result.Add(f)); } if (includeDateOfPublish) { (from vac in db.Vacancies where vac.PubDate.ToString().Contains(searchValue) select vac).ToList().ForEach(f => result.Add(f)); } if (includeEmail) { (from vac in db.Vacancies where vac.Author.Contains(searchValue) select vac).ToList().ForEach(f => result.Add(f)); } if (includeDescription) { (from vac in db.Vacancies where vac.Description.Contains(searchValue) select vac).ToList().ForEach(f => result.Add(f)); } } if (result.Count == 0) { MessageBox.Show("Вакансий не найдено"); } lvSearchResult.ItemsSource = result; Mouse.OverrideCursor = null; } catch (Exception ex) { Mouse.OverrideCursor = null; MessageBox.Show(ex.Message); } }