public async Task <IHttpActionResult> FollowTags(string tags, bool isUnfollowAllowed) { if (User.Identity.IsAuthenticated) { var userId = User.Identity.GetUserId(); string[] values = tags.Split(','); for (int i = 0; i < values.Length; i++) { values[i] = values[i].Trim(); string ss = values[i]; if (ss != "") { var tagExit = db.Tags.FirstOrDefault(x => x.name.Equals(ss, StringComparison.OrdinalIgnoreCase)); if (tagExit != null) { var alreadyFollowed = await db.FollowTags.FirstOrDefaultAsync(x => x.tagId.Equals(tagExit.Id) && x.followedBy.Equals(userId)); if (alreadyFollowed == null) { FollowTag follow = new FollowTag(); follow.followedBy = userId; follow.tagId = tagExit.Id; db.FollowTags.Add(follow); await db.SaveChangesAsync(); // return Ok("Successfully Followed"); } else { if (isUnfollowAllowed) { db.FollowTags.Remove(alreadyFollowed); await db.SaveChangesAsync(); // return Ok("Successfully Unfollowed"); } } } //return BadRequest("Tag does not exit"); } } return(Ok("Done")); } return(BadRequest("Not login")); }
public async Task <IHttpActionResult> Follow(int tagId) { var userId = User.Identity.GetUserId(); if (userId != null) { string s = ""; int c = 0; var data = new { status = s, count = c }; Tag q = await db.Tags.FindAsync(tagId); var followed = q.FollowTags.FirstOrDefault(x => x.followedBy == userId); if (followed != null) { db.FollowTags.Remove(followed); db.SaveChanges(); s = "Follow"; c = await db.FollowTags.CountAsync(x => x.tagId.Equals(tagId)); data = new { status = s, count = c }; return(Ok(data)); } FollowTag f = new FollowTag(); f.followedBy = userId; f.tagId = tagId; db.FollowTags.Add(f); db.SaveChanges(); s = "UnFollow"; c = await db.FollowTags.CountAsync(x => x.tagId.Equals(tagId)); data = new { status = s, count = c }; return(Ok(data)); } else { return(BadRequest("You are not login")); } }