private void populateTopicSelector() { using (var context = new myStudyJournalEntities()) { var dbCollection = context.Posts; List <string> uniqueTags = new List <string>(); foreach (var post in dbCollection) { string[] tagArray = post.Tags.Split(','); foreach (var tag in tagArray) { if (!string.IsNullOrEmpty(tag)) { string currentTag = tag.Trim().ToUpper(); if (!uniqueTags.Contains(currentTag)) { uniqueTags.Add(currentTag); } } } } uniqueTags.Sort(); foreach (var item in uniqueTags) { topicSelector.Items.Add(item); } } }
public void saveEdit(int id) { using (var context = new myStudyJournalEntities()) { var postToEdit = context.Posts.Find(id); var post = new Post() { Id = id, Title = title.Text, Date = DateTime.Now.Date, Tags = tagsToString(), Body = body.Text }; context.Entry(postToEdit).CurrentValues.SetValues(post); context.SaveChanges(); } string tagsToString() { string[] tags = new string[listBox2.Items.Count]; listBox2.Items.CopyTo(tags, 0); string items = String.Join(",", tags); return(items); } }
public void populateTopicList() { //var uniqueItemList = new List<string>(); foreach (var tagArr in getTags()) { foreach (var item in tagArr) { Console.WriteLine(item.Trim().ToUpper()); if (!listBox1.Items.Contains(item.Trim().ToUpper()) && !string.IsNullOrEmpty(item)) { listBox1.Items.Add(item.Trim().ToUpper()); } } } List <string[]> getTags() { var tagCollection = new List <string[]>(); using (var context = new myStudyJournalEntities()) { var dbCollection = context.Posts.SqlQuery("SELECT * FROM dbo.Post").ToList <Post>(); foreach (var tagEntry in dbCollection) { tagCollection.Add(tagEntry.Tags.Split(',')); } } return(tagCollection); } }
private void Page_Loaded(object sender, RoutedEventArgs e) { using (var context = new myStudyJournalEntities()) { var dbSet = context.Posts; List <Post> posts = new List <Post>(dbSet); dataGrid.DataContext = posts; } }
private void TopicSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (topicSelector.SelectedItem != null) { string selectedTopic = topicSelector.SelectedItem.ToString(); using (var context = new myStudyJournalEntities()) { var quariedPosts = context.Posts.Where(x => x.Tags.Contains(selectedTopic)); List <Post> selectedPosts = new List <Post>(quariedPosts); dataGrid.DataContext = selectedPosts; } } }
private async void DeleteBtn_Click(object sender, RoutedEventArgs e) { var selected = dataGrid.SelectedItem.ToString(); var post = Post.parseString(selected); var res = MessageBox.Show($"Are you sure you Want to delete Post?", "Database Action", MessageBoxButton.YesNo); if (res == MessageBoxResult.Yes) { using (var context = new myStudyJournalEntities()) { var postToDelete = context.Posts.Find(post.Id); context.Posts.Remove(postToDelete); await context.SaveChangesAsync(); } MessageBox.Show($"Post ID: {post.Id} Deleted Successfully!", "Database Action"); this.viewFrame.Navigate(new DatabasePage(this.viewFrame)); } else { return; } }
public List <tagProperties> getStudyData() { using (var context = new myStudyJournalEntities()) { var dbCollection = context.Posts.SqlQuery("SELECT * FROM dbo.Post"); var tagData = new List <tagProperties>(); var tagCollection = new List <string>(); var uniqueTags = new List <string>(); foreach (var post in dbCollection) { string[] tagArray = post.Tags.Split(','); foreach (var tag in tagArray) { if (!string.IsNullOrEmpty(tag)) { string currentTag = tag.Trim().ToUpper(); if (!uniqueTags.Contains(currentTag)) { uniqueTags.Add(currentTag); } tagCollection.Add(currentTag); } } } uniqueTags.Sort(); foreach (var item in uniqueTags) { var likeTags = tagCollection.Where(x => x == item); int count = likeTags.Count(); tagData.Add(new tagProperties() { tagName = item, tagCount = count }); } return(tagData); } }
private async void onSubmitBtnClicked(object sender, RoutedEventArgs e) { Console.WriteLine(body.Text.Length); if (title.Text != "" && title.Text != "Enter a Title" && listBox2.Items.Count != 0 && body.Text != "") { using (var context = new myStudyJournalEntities()) { if (isEditing) { Post edit = new Post() { Id = this.postToEdit.Id, Date = DateTime.Parse(dateTime.Text), Title = title.Text, Tags = tagsToString(), Body = body.Text }; var confirmEdit = MessageBox.Show("Save edit?", "Database Action", MessageBoxButton.OKCancel); if (confirmEdit == MessageBoxResult.OK) { var selected = context.Posts.Find(this.postToEdit.Id); context.Entry(selected).CurrentValues.SetValues(edit); await context.SaveChangesAsync(); MessageBox.Show("edit saved", "Database Action"); } else { return; } } else { Post entry = new Post() { Date = DateTime.Parse(dateTime.Text), Title = title.Text, Tags = tagsToString(), Body = body.Text }; var confirmation = MessageBox.Show("Add post?", "Database Action", MessageBoxButton.OKCancel); if (confirmation == MessageBoxResult.OK) { context.Posts.Add(entry); await context.SaveChangesAsync(); MessageBox.Show("Post saved", "Database Action"); } else { return; } } } } else { MessageBox.Show("All fields must be entered to post.", "Error"); return; } string tagsToString() { string[] tags = new string[listBox2.Items.Count]; listBox2.Items.CopyTo(tags, 0); string items = String.Join(",", tags).ToUpper(); return(items); } }