public RedirectToRouteResult Delete(int id) { using (var context = new foodiesEntities1()) { // Delete from meme where memeid = id // Fetch the entity to delete, and remove it from the DBSet var thePostToDelete = context.Posts.FirstOrDefault(m => m.PostID == id); if (thePostToDelete != null) { // Fetch Associated Comments, so we can delete them! var comments = thePostToDelete.Comments.ToList(); foreach (var comment in comments) { context.Comments.Remove(comment); } // Actually Delete All Comments context.SaveChanges(); // Delete the meme itself context.Posts.Remove(thePostToDelete); // REMEMBER this! context.SaveChanges(); } } return(RedirectToAction("Gallery")); //return View("MemeList"); }
public new ActionResult Profile() { string email = User.Identity.GetUserName(); if (email.Equals("")) { return(View("../Account/Login")); } var userId = User.Identity.GetUserId(); var userEmail = UserManager.GetEmail(userId); var model = new UserModel(); using (var context = new foodiesEntities1()) { // Fetch existing entity var user = context.DaPrUsers.FirstOrDefault(u => u.UserEmail == userEmail); model.UserName = user.UserName; model.UserBio = user.UserBio; model.ProfilePicURL = user.ProfilePicURL; } return(View(model)); }
public ActionResult PostDetail(int id) { Post post = null; using (var context = new foodiesEntities1()) { post = context.Posts.FirstOrDefault(m => m.PostID == id); } if (post == null) { return(null); } // Map from Meme Entity to View Model var model = new PostModel() { PostID = post.PostID, PostURL = post.PostURL, PostTitle = post.PostTitle, IsVideo = post.IsVideo, }; return(View(model)); }
public ActionResult Profile(HttpPostedFileBase file, UserModel model) { var userId = User.Identity.GetUserId(); var userEmail = UserManager.GetEmail(userId); try { if (file.ContentLength > 0) { string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName); file.SaveAs(_path); model.ProfilePicURL = _FileName; using (var context = new foodiesEntities1()) { var userToUpdate = context.DaPrUsers.FirstOrDefault(m => m.UserEmail == userEmail); userToUpdate.ProfilePicURL = "../UploadedFiles/" + _FileName; userToUpdate.UserBio = model.UserBio; context.SaveChanges(); }; } ViewBag.Message = "File Uploaded Successfully!!"; return(View(model)); } catch { ViewBag.Message = "File upload failed!!"; return(View(model)); } }
public ActionResult Update(int id) { var model = new PostModel(); using (var context = new foodiesEntities1()) { var post = context.Posts.FirstOrDefault(m => m.PostID == id); model.PostTitle = post.PostTitle; model.PostURL = post.PostURL; } return(View(model)); }
public ActionResult Post(HttpPostedFileBase file, PostModel model) { var userId = User.Identity.GetUserId(); var userEmail = UserManager.GetEmail(userId); try { if (file.ContentLength > 0) { string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName); file.SaveAs(_path); using (var context = new foodiesEntities1()) { var user = context.DaPrUsers.FirstOrDefault(m => m.UserEmail == userEmail); var postToMake = new Post(); postToMake.PostTitle = model.PostTitle; postToMake.PostURL = "../UploadedFiles/" + _FileName; if (postToMake.PostURL.EndsWith("mp4") || postToMake.PostURL.EndsWith("gif")) { postToMake.IsVideo = true; } else { postToMake.IsVideo = false; } postToMake.UserID = user.UserID; context.Posts.Add(postToMake); context.SaveChanges(); }; } return(RedirectToAction("Gallery")); } catch { ViewBag.Message = "File upload failed!!"; return(View()); } }
public async Task <ActionResult> Index(HttpPostedFileBase file, UserModel model, ManageMessageId?message) { var userId = User.Identity.GetUserId(); var model2 = new IndexViewModel { HasPassword = HasPassword(), PhoneNumber = await UserManager.GetPhoneNumberAsync(userId), TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId), Logins = await UserManager.GetLoginsAsync(userId), BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId) }; var userEmail = UserManager.GetEmail(userId); try { if (file.ContentLength > 0) { string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName); file.SaveAs(_path); model.ProfilePicURL = _FileName; using (var context = new foodiesEntities1()) { var userToUpdate = context.DaPrUsers.FirstOrDefault(m => m.UserEmail == userEmail); userToUpdate.ProfilePicURL = "../UploadedFiles/" + _FileName; context.SaveChanges(); }; } ViewBag.Message = "File Uploaded Successfully!!"; return(View(model2)); } catch { ViewBag.Message = "File upload failed!!"; return(View(model2)); } }
public ActionResult Update(int id, PostModel model) { using (var context = new foodiesEntities1()) { // Fetch existing entity var thePostToUpdate = context.Posts.FirstOrDefault(m => m.PostID == id); // Update entity based on model thePostToUpdate.PostTitle = model.PostTitle; // Save Changes context.SaveChanges(); } var model2 = new UserPostsModel(); List <Post> postList = null; // Use EF to get MemeModels from the Database // Unit of Work using (var context = new foodiesEntities1()) { // Fetch the Meme Entities! postList = context.Posts.Select(m => m).ToList(); } if (postList != null) { // Convert Meme Entities to Meme Models foreach (var post in postList) { model2.UserPosts.Add(new PostModel() { PostID = post.PostID, PostTitle = post.PostTitle, PostURL = post.PostURL }); } } return(RedirectToAction("Gallery")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); using (var context = new foodiesEntities1()) { var newUser = new DaPrUser(); newUser.UserName = model.Username; newUser.UserEmail = model.Email; context.DaPrUsers.Add(newUser); context.SaveChanges(); } if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Gallery(PostModel model) { var userId = User.Identity.GetUserId(); var userEmail = UserManager.GetEmail(userId); var model2 = new UserPostsModel(); List <Post> postList = null; // Use EF to get MemeModels from the Database // Unit of Work using (var context = new foodiesEntities1()) { var user = context.DaPrUsers.FirstOrDefault(m => m.UserEmail == userEmail); // Fetch the Meme Entities! postList = context.Posts.Where(m => m.UserID == user.UserID).ToList(); } if (postList != null) { // Convert Meme Entities to Meme Models foreach (var post in postList) { model2.UserPosts.Add(new PostModel() { PostID = post.PostID, PostTitle = post.PostTitle, PostURL = post.PostURL, IsVideo = post.IsVideo }); } } return(View(model2)); }