public IHttpActionResult UserFollowers([FromUri] string AccessToken) { var UserId = TokenValidator.VerifyToken(AccessToken); var Followers = UserAuxiliar.Followers(UserId, SQLContext); return(Ok(Followers)); }
//TA MT FEIO HORROROSO DESCULPA private static List <dynamic> CreateAffinityProducts(int UserId, List <Product> Products, SQLContext SQLContext) { var ProductRecommendation = new List <dynamic>(); var TaskArray = new List <Task>(); foreach (var Product in Products) { var NewTask = Task.Run ( () => { double OwnerAffinity = 0; double SubjectAffinity = 0; double FollowsAffinity = 0; var OwnerId = Product.ProId; IEnumerable <int> SubjectsFollowedByOwner; IEnumerable <int> UsersFollowedByOwner; IEnumerable <int> OwnerFollowers; lock (SQLContext) { SubjectsFollowedByOwner = UserAuxiliar.FollowedSubjects((int)OwnerId, SQLContext).Select(x => x.Id); UsersFollowedByOwner = UserAuxiliar.Follows((int)OwnerId, SQLContext).Select(x => x.Id); OwnerFollowers = UserAuxiliar.Followers((int)OwnerId, SQLContext).Select(x => x.Id); } var Follows = UsersFollowedByOwner.Union(OwnerFollowers); Follow OwnerFollow; lock (SQLContext) { OwnerFollow = SQLContext.Follows.FirstOrDefault(x => x.FollowerId == UserId && x.FollowedId == OwnerId); } if (OwnerFollow != default) { OwnerAffinity += OwnerFollow.Affinity; } int RawSubjectAffinity = 0; lock (SQLContext) { var SameSubjects = SQLContext.UserSubjects.Where(x => x.UserId == UserId && SubjectsFollowedByOwner.Contains(x.SubjectId)); if (SameSubjects.Any()) { RawSubjectAffinity = SameSubjects.Sum(x => x.Affinity); } } if (SubjectsFollowedByOwner.Any()) { SubjectAffinity = RawSubjectAffinity / SubjectsFollowedByOwner.Count(); } IQueryable <Follow> EqualFollows; lock (SQLContext) { EqualFollows = SQLContext.Follows.Where(x => Follows.Contains(x.FollowedId) && x.FollowerId == UserId); if (EqualFollows.Any()) { FollowsAffinity = EqualFollows.Sum(x => x.Affinity) / EqualFollows.Count(); } } if (OwnerAffinity != 0 || FollowsAffinity != 0 || SubjectAffinity != 0) { ProductRecommendation.Add(new { Product, OwnerAffinity, SubjectAffinity, FollowsAffinity }); } } ); TaskArray.Add(NewTask); } Task.WaitAll(TaskArray.ToArray()); return(ProductRecommendation); }