public JsonResult ToggleLike(int?postId) { // Check if the user is signed in if (!User.Identity.IsAuthenticated) { Response.StatusCode = (int)HttpStatusCode.Forbidden; return(Json(new { Message = "You must be signed in to do that" })); } // if postId is null or if the post does not exist return error message if (postId == null || !postExists(postId)) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Json(new { Message = "Unable to find post" })); } // if the currently logged in user is not the owner of the post, return an error message if (UserOwnsPost(postId)) { Response.StatusCode = (int)HttpStatusCode.Forbidden; return(Json(new { Message = "You cannot like your own post" })); } // Getting all the likes that the current post has. var postLikes = _context.Likes.Where(l => l.PostId == postId); try { // If the user Has already liked the post, we unlike it. and vice versa if (postLikes.Any(l => l.UserId == _userManager.GetUserId(User) && l.PostId == postId)) { var likeToRemove = _context.Likes.Where(l => l.UserId == _userManager.GetUserId(User) && l.PostId == postId); _context.Likes.RemoveRange(likeToRemove); _context.SaveChanges(); Response.StatusCode = (int)HttpStatusCode.OK; return(Json(new { Message = "Unliked!" })); } else { Like like = new Like() { UserId = _userManager.GetUserId(User), Post = _context.Posts.Single(p => p.Id == postId) }; _context.Likes.Add(like); _context.SaveChanges(); Response.StatusCode = (int)HttpStatusCode.Created; return(Json(new { Message = "Liked!" })); } } catch (Exception e) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = e.Message })); } }
public JsonResult ToggleFollow(string userName) { // Check if the user is signed in if (!User.Identity.IsAuthenticated) { Response.StatusCode = (int)HttpStatusCode.Forbidden; return(Json(new { Message = "You must be signed in to do that" })); } // if userName is null or if the user does not exist return error message if (userName == null || !_context.Users.Any(u => u.UserName == userName)) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Json(new { Message = "Unable to find user" })); } // Getting all the Follows that the logged in user has. var userFollows = _context.Follows.Where(f => f.FollowerId == _userManager.GetUserId(User)); var followee = _userManager.FindByNameAsync(userName).Result; try { // If the logged in user is already following the followee, unfollow. and vice versa if (userFollows.Any(f => f.FolloweeId == followee.Id)) { var followToRemove = _context.Follows.Where(f => f.FolloweeId == followee.Id); _context.Follows.RemoveRange(followToRemove); _context.SaveChanges(); Response.StatusCode = (int)HttpStatusCode.OK; return(Json(new { Message = "Unfollowed!" })); } else { Follow follow = new Follow() { Follower = _userManager.GetUserAsync(User).Result, FolloweeId = followee.Id }; _context.Follows.Add(follow); _context.SaveChanges(); Response.StatusCode = (int)HttpStatusCode.Created; return(Json(new { Message = "Followed!" })); } } catch (Exception e) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = e.Message })); } }
public IActionResult UploadProfileImage(IList <IFormFile> files) { try { IFormFile uploadedImage = files.FirstOrDefault(); if (uploadedImage == null || uploadedImage.ContentType.ToLower().StartsWith("image/")) { MemoryStream ms = new MemoryStream(); uploadedImage.OpenReadStream().CopyTo(ms); System.Drawing.Image image = System.Drawing.Image.FromStream(ms); var imageId = Guid.NewGuid(); var user = _userManager.GetUserAsync(User).Result; Models.Image imageEntity = new Models.Image() { Id = imageId, Name = uploadedImage.Name, Data = ms.ToArray(), Width = image.Width, Height = image.Height, ContentType = uploadedImage.ContentType, User = _userManager.GetUserAsync(User).Result }; // TEMPORARY // for right now, the most recently uploaded image is the users profile image user.ProfileImageId = imageId; _context.Images.Add(imageEntity); _context.Users.Update(user); _context.SaveChanges(); } TempData["Status"] = "Your profile picture has been changed."; return(View()); } catch (Exception e) { TempData["Status"] = e.Message; return(View()); } }
public JsonResult UploadImage(IList <IFormFile> files) { try { IFormFile uploadedImage = files.FirstOrDefault(); if (uploadedImage == null || uploadedImage.ContentType.ToLower().StartsWith("image/")) { MemoryStream ms = new MemoryStream(); uploadedImage.OpenReadStream().CopyTo(ms); System.Drawing.Image image = System.Drawing.Image.FromStream(ms); var imageId = Guid.NewGuid(); var user = _userManager.GetUserAsync(User).Result; Models.Image imageEntity = new Models.Image() { Id = imageId, Name = uploadedImage.Name, Data = ms.ToArray(), Width = image.Width, Height = image.Height, ContentType = uploadedImage.ContentType, User = user }; _context.Images.Add(imageEntity); _context.SaveChanges(); } Response.StatusCode = (int)HttpStatusCode.Created; return(Json(new { Message = "Image Uploaded!!" })); } catch (Exception e) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = e.Message })); } }