Exemplo n.º 1
0
        protected void LinkButtonSaveThread_Click(object sender, EventArgs e)
        {
            try
            {
                BgTatkoEntities context = new BgTatkoEntities();
                BgTatkoForum.Models.Thread thread = new BgTatkoForum.Models.Thread();
                thread.Content = this.TextBoxContent.Text;
                thread.Title = this.TextBoxTitle.Text;
                thread.DateCreated = DateTime.Now;
                thread.CategoryId = this.DropDownListCategory.SelectedIndex + 1;
                var user = context.Users.Where(u => u.UserName == this.User.Identity.Name).FirstOrDefault();
                thread.Category = context.Categories.Find(thread.CategoryId);
                thread.User = user;
                thread.UserId = user.Id;
                context.Threads.Add(thread);
                user.Threads.Add(thread);
                context.SaveChanges();

                var titleTokens = this.TextBoxTitle.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var textBoxTokens = this.TextBoxTags.Text.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                var tokens = textBoxTokens.Union(titleTokens);

                foreach (var token in tokens)
                {
                    var existingTag = context.Tags.FirstOrDefault(t => t.Name == token.ToLower());

                    if (existingTag != null)
                    {
                        existingTag.Threads.Add(thread);
                        context.SaveChanges();
                    }
                    else
                    {
                        var newTag = new Tag() { Name = token.ToLower() };
                        context.Tags.Add(newTag);
                        context.SaveChanges();
                        newTag.Threads.Add(thread);
                        context.SaveChanges();
                    }
                }

                this.Response.Redirect("Thread/Thread?threadId=" + thread.ThreadId, false);
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Exemplo n.º 2
0
 // The id parameter name should match the DataKeyNames value set on the control
 public void GridViewUsers_DeleteItem(string id)
 {
     BgTatkoEntities context = new BgTatkoEntities();
     User item = context.Users.Find(id);
     context.Users.Remove(item);
     context.SaveChanges();
 }
Exemplo n.º 3
0
        protected void LinkButtonDeleteThread_Command(object sender, CommandEventArgs e)
        {
            try
            {
                BgTatkoEntities context = new BgTatkoEntities();
                int threadId = Convert.ToInt32(e.CommandArgument);
                var thread = context.Threads.Where(t => t.ThreadId == threadId).FirstOrDefault();
                context.Posts.RemoveRange(thread.Posts);
                context.ThreadVotes.RemoveRange(thread.ThreadVotes);
                context.Threads.Remove(thread);
                context.SaveChanges();
                this.GridViewEditThreads.DataBind();
                ErrorSuccessNotifier.AddSuccessMessage("Thread deleted");
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            this.FormViewDeleteThread.Visible = false;
        }
Exemplo n.º 4
0
        protected void LinkButtonSaveComment_Click(object sender, EventArgs e)
        {
            var content = this.TextBoxCommentContent.Text;
            var postId = Convert.ToInt32(this.HiddenFieldSlectedPostId.Value);

            BgTatkoEntities context = new BgTatkoEntities();
            Post post = context.Posts.Find(postId);

            try
            {

                User user = context.Users.First(u => u.UserName == this.User.Identity.Name);
                Comment comment = new Comment { Content = content, DateCreated = DateTime.Now, User = user };
                post.Comments.Add(comment);
                context.SaveChanges();
                Response.Redirect(Request.Url.ToString());
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Exemplo n.º 5
0
        protected void LinkButtonSavePost_Click(object sender, CommandEventArgs e)
        {
            var content = this.TextBoxPostContent.Text;
            var threadId = Convert.ToInt32(e.CommandArgument.ToString());

            BgTatkoEntities context = new BgTatkoEntities();
            var thread = context.Threads.Find(threadId);

            try
            {

                User user = context.Users.First(u => u.UserName == this.User.Identity.Name);
                Post post = new Post { Content = content, User = user };

                thread.Posts.Add(post);
                context.SaveChanges();
                Response.Redirect("~/Thread?threadId=" + threadId);
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Exemplo n.º 6
0
        private void Vote_Command(CommandEventArgs e, int value)
        {
            try
            {
                var ids = e.CommandArgument.ToString().Split(',');
                int threadId = Convert.ToInt32(ids[0]);
                string userId = ids[1];
                BgTatkoEntities context = new BgTatkoEntities();

                var vote = context.ThreadVotes.FirstOrDefault(v => v.ThreadId == threadId && v.UserId == userId);
                if (vote == null)
                {
                    vote = new ThreadVote()
                    {
                        UserId = userId,
                        ThreadId = threadId,
                        Value = value
                    };
                    context.ThreadVotes.Add(vote);
                    context.SaveChanges();
                    this.GridThreads.DataBind();
                }
                else
                {
                    ErrorSuccessNotifier.AddInfoMessage("Users can vote once per Thread");
                }
            }
            catch (Exception)
            {
                ErrorSuccessNotifier.AddWarningMessage("User must be logged in to vote");
            }
        }
Exemplo n.º 7
0
 // The id parameter name should match the DataKeyNames value set on the control
 public void GridViewUsers_UpdateItem(string id)
 {
     try
     {
         BgTatkoEntities context = new BgTatkoEntities();
         User item = context.Users.Find(id);
         // Load the item here, e.g. item = MyDataLayer.Find(id);
         if (item == null)
         {
             // The item wasn't found
             ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
             return;
         }
         TryUpdateModel(item);
         if (ModelState.IsValid)
         {
             // Save changes here, e.g. MyDataLayer.SaveChanges();
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }