Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }