Example #1
0
        public ActionResult Create(Post post)
        {
            post.UserName = User.Identity.Name;

            if(post.Updated == null)
                post.Updated = post.Updated.ToUniversalTime();

            if (post.Updated.Kind == DateTimeKind.Local)
                post.Updated = post.Updated.ToUniversalTime();

            if (ModelState.IsValid)
            {
                foreach (var tag in post.Tags.ToList())
                {
                    var dbTag = _db.Tags.Where(t => t.Name.ToLower() == tag.Name.ToLower()).SingleOrDefault();
                    {
                        post.Tags.Remove(tag);
                        post.Tags.Add(dbTag);
                    }
                }

                _db.Posts.Add(post);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(post);
        }
Example #2
0
        public void TagStringTest()
        {
            Post target = new Post(); // TODO: Initialize to an appropriate value
            string actual;
            target.TagString = "these, are;my|  tags";

            Assert.AreEqual(target.Tags.Count, 4);
            Assert.AreEqual(target.Tags[0], "these");
            Assert.AreEqual(target.Tags[0], "are");
            Assert.AreEqual(target.Tags[0], "my");
            Assert.AreEqual(target.Tags[0], "tags");
        }
Example #3
0
        public ActionResult Edit(Post post)
        {
            if (ModelState.IsValid)
            {
                if(post.Updated == null)
                    post.Updated = DateTime.UtcNow;

                if (post.Updated.Kind == DateTimeKind.Local)
                    post.Updated = post.Updated.ToUniversalTime();

                var databasePost = _db.Posts.Where(p => p.PostId == post.PostId).Include(p => p.Tags).SingleOrDefault();
                _db.Entry(databasePost).CurrentValues.SetValues(post);

                //append new tags
                foreach (var updatedTag in post.Tags)
                {
                    var dbTag = databasePost.Tags.Where(t => t.Name == updatedTag.Name).SingleOrDefault();

                    if (dbTag == null)
                    {
                        //New Tag not associated with post
                        dbTag = _db.Tags.Where(t => t.Name == updatedTag.Name).SingleOrDefault();

                        if (dbTag == null)
                            dbTag = new Tag() { Name = updatedTag.Name };

                        databasePost.Tags.Add(dbTag);
                    }
                }

                //remove tags
                foreach (var dbTag in databasePost.Tags.ToList())
                {
                    var updatedTag = post.Tags.Where(t => t.Name == dbTag.Name).SingleOrDefault();

                    if (updatedTag == null)
                        databasePost.Tags.Remove(dbTag);
                }

                _db.Entry(databasePost).State = EntityState.Modified;
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(post);
        }