public ActionResult Create(CreatePostViewModel postView) { string tags = postView.Tags; var post = Mapper.Map <CreatePostViewModel, Post>(postView); post.AuthorId = ((Student)Session["User"]).Id; db.Posts.Add(post); post.Created = DateTime.Now; if (post.Tags == null) { post.Tags = new List <Tags>(); } foreach (var tag in tags.Split(' ').Distinct()) { var buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); if (buf == null) { buf = new Tags() { Name = tag }; db.Tags.Add(buf); } post.Tags.Add(buf); } db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(CreateCommentViewModel _comment) { var comment = Mapper.Map <CreateCommentViewModel, Comment>(_comment); comment.AuthorId = ((Student)Session["User"]).Id; db.Comments.Add(comment); db.SaveChanges(); return(RedirectToAction("Details", "Posts", new { id = _comment.PostId })); }
public ActionResult Create(CreatePostViewModel _post) { string tags = _post.Tags; _post.Tags = null; var post = Mapper.Map <CreatePostViewModel, Post>(_post); post.AuthorId = ((Student)Session["User"]).Id; db.Posts.Add(post); db.SaveChanges(); post = db.Entry(post).Entity; foreach (var tag in tags.Split(' ').Distinct()) { var buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); if (buf == null) { db.Tags.Add(new Tags() { Name = tag }); db.SaveChanges(); buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); } post.Tags.Add(buf); } db.SaveChanges(); //foreach (var tag in tags.Split(' ').Distinct()) //{ // var buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); // db.TagsPosts.Add((buf == null) // ? new TagsPosts() { // Tags = new Tags() { Name = tag }, // IdPost = post.Id, // Post = post // } // : new TagsPosts() { // IdTag = buf.Id, // Tags = buf, // IdPost = post.Id, // Post = post // }); // db.SaveChanges(); //} return(RedirectToAction("Index")); }
public ActionResult Index(IndexStudentViewModel _student) { var user = db.Students .Where(a => a.FirstName == _student.FirstName && a.LastName == _student.LastName) .SingleOrDefault(); if (user == null) { var student = Mapper.Map <IndexStudentViewModel, Student>(_student); db.Students.Add(student); db.SaveChanges(); user = db.Students .Where(a => a.FirstName == _student.FirstName && a.LastName == _student.LastName) .SingleOrDefault(); } Session["User"] = user; return(RedirectToAction("Index", "Posts")); }