public IHttpActionResult Follow(FollowingDto followingDto) { var userId = User.Identity.GetUserId(); var following = new Following { FollowerId = userId, FolloweeId = followingDto.FolloweeId }; if (_dbContext.Followings.Any(f => f.FollowerId == userId && f.FolloweeId == followingDto.FolloweeId)) { _dbContext.Entry(following).State = System.Data.Entity.EntityState.Deleted; _dbContext.SaveChanges(); return(Json(new { isFollow = false, followeeId = followingDto.FolloweeId })); } _dbContext.Followings.Add(following); _dbContext.SaveChanges(); return(Json(new { isFollow = true, followeeId = followingDto.FolloweeId })); }
public IHttpActionResult Follow(FollowingDto followingDto) { var userId = User.Identity.GetUserId(); if (_dbContext.Followings.Any(f => f.FollowerId == userId && f.FolloweeId == followingDto.FolloweeId)) { return(BadRequest("Following already exists!")); } var following = new Following { FollowerId = userId, FolloweeId = followingDto.FolloweeId }; _dbContext.Followings.Add(following); _dbContext.SaveChanges(); return(Ok()); }
public IHttpActionResult Follow(FollowingDto followingDto) { var userId = User.Identity.GetUserId(); if (_dbContext.Followings.Any(f => f.FolloweeId == userId && f.FolloweeId == followingDto.FolloweeId)) { return(BadRequest("Following already exists!")); } var following = new Following { FollowerId = userId, FolloweeId = followingDto.FolloweeId }; _dbContext.Followings.Add(following); _dbContext.SaveChanges(); following = _dbContext.Followings .Where(x => x.FolloweeId == followingDto.FolloweeId && x.FollowerId == userId) .Include(x => x.Followee) .Include(x => x.Follower).SingleOrDefault(); var followingNotification = new FollowingNotification() { Id = 0, Logger = following.Follower.Name + " following " + following.Followee.Name }; _dbContext.FollowingNotifications.Add(followingNotification); _dbContext.SaveChanges(); return(Ok()); }