public async Task <IActionResult> MakeFollowRelationship([FromRoute] string username, [FromQuery(Name = "f")] string followedUser) { if (username == followedUser) { return(BadRequest(new { Err = "Cannot follow yourself!" })); } string jwtUsername = User.FindFirstValue(ClaimTypes.Name); if (jwtUsername != username) { return(BadRequest(new { Err = "You cannot make follows on others' accounts!" })); } var user = await DbContext.Users.SingleOrDefaultAsync(u => u.Username == username); var userToBeFollowed = await DbContext.Users.SingleOrDefaultAsync(u => u.Username == followedUser); if (userToBeFollowed is null) { return(BadRequest(new { Err = "Followed user does not exists!" })); } // make follows Follow retrievedFollow = await DbContext.Follows .Where(f => f.FollowerId == user.Id && f.UserId == userToBeFollowed.Id) .FirstOrDefaultAsync(); if (retrievedFollow is null) { retrievedFollow = new Follow() { FollowerId = user.Id, UserId = userToBeFollowed.Id, IsActive = true }; DbContext.Add(retrievedFollow); } else { if (retrievedFollow.IsActive.Value) { return(BadRequest(new { Err = "Already followed!" })); } DbContext.Entry(retrievedFollow).CurrentValues.SetValues(new { IsActive = true }); } await DbContext.SaveChangesAsync(); return(Ok(new { retrievedFollow.Follower.Id, retrievedFollow.Follower.Username, Type = "following", Link = $"/users/{retrievedFollow.Follower.Username}" })); }
public async Task <IActionResult> CreateCommentForPost([FromRoute(Name = "id")] int postId, [FromBody] Comment comment) { if (postId != comment.PostId) { return(BadRequest(new { Err = "You are creating a comment for the wrong post!" })); } // verify the current user int _userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)); comment.UserId = _userId; comment.Timestamp = DateTimeOffset.UtcNow; DbContext.Add(comment); await DbContext.SaveChangesAsync(); await DbContext.Entry(comment) .Reference(c => c.User) .Query() .Include(u => u.AvatarPhoto) .LoadAsync(); var url = Url.Action("GetCommentById", "Comment", new { id = comment.Id }); return(Created(url, ResponseModelFactory.Create(comment))); }
public async Task <IActionResult> UpdateAvatar([FromRoute] string username, [FromForm] IFormFile newAvatar) { string jwtUsername = User.FindFirstValue(ClaimTypes.Name); if (jwtUsername != username) { return(BadRequest(new { Err = "You cannot update others' avatars!" })); } // get avatar record var avatarRecord = await DbContext.AvatarPhotos .Include(a => a.User) .Where(a => a.User.Username == username) .FirstOrDefaultAsync(); // upload photo try { string avatarPath = await PhotoUtils.UploadPhotoAsync(newAvatar, AvatarServingPath); DbContext.Entry(avatarRecord).CurrentValues.SetValues(new { FileName = avatarPath }); await DbContext.SaveChangesAsync(); return(Ok()); } catch (InvalidDataException) { return(BadRequest(new { Err = "Photo is invalid!" })); } }
public async Task <IActionResult> CreateLikeReactionForPost([FromRoute(Name = "id")] int postId) { int userId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value); // check if any exists var retrievedLike = await DbContext.Likes.Include(l => l.User).ThenInclude(u => u.AvatarPhoto) .Include(l => l.LikedPostNavigation) .FirstOrDefaultAsync(l => l.UserId == userId && l.LikedPost == postId); if (retrievedLike is null) { retrievedLike = new Like() { LikedPost = postId, UserId = userId, IsActive = true }; DbContext.Add(retrievedLike); } else { if (retrievedLike.IsActive.Value) { return(Ok(ResponseModelFactory.Create(retrievedLike))); } } DbContext.Entry(retrievedLike).CurrentValues.SetValues(new { IsActive = true }); await DbContext.SaveChangesAsync(); await DbContext.Entry(retrievedLike).GetDatabaseValuesAsync(); await DbContext.Entry(retrievedLike) .Reference(l => l.User) .Query() .Include(u => u.AvatarPhoto) .LoadAsync(); return(Ok(ResponseModelFactory.Create(retrievedLike))); }