public List <UserNotifications> GetUserNotifications(string userid) { List <UserNotifications> notifications = new List <UserNotifications>(); List <UserNotifications> notifications2 = new List <UserNotifications>(); using (var context = new ScoutUpDB()) { notifications = context.UserNotifications.Include(e => e.User) .Where(e => e.UserId == userid) .Where(r => r.UserNotificationsRead == false) .ToList(); foreach (var notify in notifications) { notifications2.Add(new UserNotifications { UserNotificationsID = notify.UserNotificationsID, UserId = notify.UserId, UserNotificationsMessage = notify.UserNotificationsMessage, UserNotificationsDate = notify.UserNotificationsDate, NotificationLink = notify.NotificationLink }); } } return(notifications2); }
public static IQueryable <Post> CompletePosts(this ScoutUpDB context) { return(context.Posts. Include(ph => ph.PostPhotos). Include(pl => pl.PostLikes). Include(pc => pc.PostComments)); }
public void UpdateNotification(List <UserNotifications> notifyList) { using (var context = new ScoutUpDB()) { foreach (var notify in notifyList) { notify.UserNotificationsRead = true; context.Entry(notify).State = EntityState.Modified; } context.SaveChanges(); } }
public static IQueryable <User> CompleteUsers(this ScoutUpDB context) { return(context.Users. Include(e => e.UserFollow). Include(p => p.UserPosts). Include("UserPosts.PostPhotos"). Include("UserPosts.PostLikes"). Include("UserPosts.PostComments"). Include(h => h.UserHobbies). Include(ph => ph.UserPhotos). Include(nt => nt.UserNotifications)); }
public OnlineUsersViewModel GetUserFromPostId(int postid) { var model = new OnlineUsersViewModel(); using (var context = new ScoutUpDB()) { model = context.Posts.Where(e => e.PostID == postid).Select(e => new OnlineUsersViewModel { UserId = e.UserId , UserProfilePhoto = e.User.UserProfilePhoto }).FirstOrDefault(); } return(model); }
public UserNotifications AddNotification(string userid, string message, string notifiyDirection, string notifyLink) { var notify = new UserNotifications() { UserId = userid, UserNotificationsMessage = message, UserNotificationsDate = DateTime.Now, UserNotificationsRead = false, NotificationLink = notifyLink }; using (var context = new ScoutUpDB()) { context.UserNotifications.Add(notify); context.SaveChanges(); } return(notify); }
public ActionResult LikePost(int? id) { if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var post = _db.PostById((int)id); var user = GetUserModel(); var isLiked = _db.PostLikes.Where(e => e.UserId == user.Id).FirstOrDefault(e => e.PostID == id); if (isLiked != null) { _db.PostLikes.Attach(isLiked); _db.Entry(isLiked).State = EntityState.Deleted; _db.SaveChanges(); //NotificationRepository repo=new NotificationRepository(); //repo.AddNotification(post.UserId,user.UserFirstName+ " " + user.UserSurname +" gönderini beğendi"); return Json(new { success = true,liked = false, likes = _db.PostById((int)id).PostLikes.Count }, JsonRequestBehavior.AllowGet); } var postLike = new PostLikes { UserId = user.Id, PostID = (int) id, IsLiked = true }; try { using (var context = new ScoutUpDB()) { context.PostLikes.Add(postLike); context.UsersLastMoves.Add(new UsersLastMoves { MoveDate = DateTime.Now, UserId = user.Id, UsersLastMoveText =" bir gönderiyi beğendi.", UsersMoveLink = "/users/index/" + post.UserId + "#post" + post.PostID }); context.SaveChanges(); return Json(new { success = true,liked=true,likes =context.PostById((int)id).PostLikes.Count },JsonRequestBehavior.AllowGet); } } catch (Exception ex) { return Json(new { success = false, error = true, message = ex.InnerException.ToString() },JsonRequestBehavior.AllowGet); } }
public List <UsersToFollow> FollowSuggest1(string userId, ScoutUpDB _db) { //user ın puan verdiği itemler ile diğer userların puan verdiği itemin kesişimi alınacak //birinin puan verip diğerinin vermediği ile ilgilenmiyoruz var usersRatedItems = _db.UserRatings.Where(e => e.UserId == userId). Select(e => e.CategoryItemID).ToArray(); var usersRatedItemsAndRatings = _db.UserRatings.Where(e => e.UserId == userId).Select(e => new UserAndRating { UserId = e.UserId, ItemId = e.CategoryItemID, UserRating = e.UserRating }).ToList(); var followlist = _db.UserFollow.Where(e => e.UserId == userId).Select(e => e.UserBeingFollowedUserId) .ToArray(); var allUserIds = _db.Users.Where(e => e.Id != userId).Where(e => !followlist.Contains(e.Id)).Select(e => e.Id); var allRatedItems = _db.UserRatings.Select(e => e.CategoryItemID).ToArray(); var ratedCount = new System.Collections.Generic.Dictionary <int, float>(); for (int i = 0; i < usersRatedItems.Count(); i++) { ratedCount.Add(usersRatedItems[i], (float)Math.Log((float)(allUserIds.Count() + 1)) / (float)allRatedItems.Count(e => e == usersRatedItems[i])); } var other = _db.UserRatings.Where(e => allUserIds.Contains(e.UserId)). Where(e => usersRatedItems.Contains(e.CategoryItemID)). Select(e => new UserAndRating { UserId = e.UserId, ItemId = e.CategoryItemID, UserRating = e.UserRating }); var otherUsersAndRatings = new Dictionary <string, List <UserAndRating> >(); foreach (var ids in allUserIds) { var list = new List <UserAndRating>(); foreach (var userAndRating in other) { if (ids == userAndRating.UserId) { list.Add(userAndRating); } } otherUsersAndRatings.Add(ids, list); } float userMean = (float)usersRatedItemsAndRatings.Sum(e => e.UserRating) / (float)usersRatedItemsAndRatings.Count; List <Similarity> similarties = new List <Similarity>(); var usersToFollow = new List <UsersToFollow>(); foreach (var otherUser in otherUsersAndRatings) { float otherUserMean = (float)otherUser.Value.Sum(e => e.UserRating) / (float)otherUser.Value.Count; float ustToplamCarpim = 0; float altToplamCarpim1 = 0; float altToplamCarpim2 = 0; float ortakPuanlanan = 0; foreach (var otherUserRating in otherUser.Value) { foreach (var usersRated in usersRatedItemsAndRatings) { if (usersRated.ItemId == otherUserRating.ItemId) { ustToplamCarpim += (float)ratedCount[usersRated.ItemId] * ((float)usersRated.UserRating - (float)userMean) * ((float)otherUserRating.UserRating - otherUserMean); altToplamCarpim1 += ratedCount[usersRated.ItemId] * ((float)(float)Math.Pow(((float)usersRated.UserRating - userMean), 2)); altToplamCarpim2 += ratedCount[usersRated.ItemId] * (float)(float)Math.Pow((float)(otherUserRating.UserRating - otherUserMean), 2); ortakPuanlanan++; break; } } } //NaN engellemek için 0.001 ile toplandı float sim = ((float)(ustToplamCarpim) / ((float)(Math.Sqrt(altToplamCarpim1)) * (float)Math.Sqrt(altToplamCarpim2))); sim = (float)(sim * ((float)(ortakPuanlanan) / usersRatedItems.Length)); similarties.Add(new Similarity { OtherUserID = otherUser.Key, UserId = userId, UsersSimilarity = sim, RatedByUsersCount = otherUser.Value.Count }); } var similartiesOrdered = similarties.OrderByDescending(e => e.UsersSimilarity); foreach (var similarity in similartiesOrdered) { var tempUser = _db.Users.Find(similarity.OtherUserID); var temp = new UsersToFollow(similarity.OtherUserID, tempUser.UserFirstName + " " + tempUser.UserSurname, tempUser.UserProfilePhoto); usersToFollow.Add(temp); } return(usersToFollow); }
public List <FollowSuggestViewModel> FollowSuggest(string userId, ScoutUpDB _db, bool nearby = false) { var user = _db.Users.Find(userId); //benzerlik listesi List <Similarity> similarties = new List <Similarity>(); //Geri dönecek takip listesi var usersToFollow = new List <FollowSuggestViewModel>(); //user ın puan verdiği itemler ile diğer userların puan verdiği itemin kesişimi alınacak //birinin puan verip diğerinin vermediği ile ilgilenmiyoruz var usersRatedItems = _db.UserRatings.Where(e => e.UserId == userId). Select(e => e.CategoryItemID).ToArray(); //Kullanıcının puanları öğelerin Listesi UserandRating classından oluşuyor bu liste.Kullanıcının puanladığı öğelerin diğer kullanıcılarla kesişimini alırken kullanılacak. //Puanlanan öğelere ve itemlere daha kolay ulaşım sağlar var usersRatedItemsAndRatings = _db.UserRatings.Where(e => e.UserId == userId).Select(e => new UserAndRating { UserId = e.UserId, ItemId = e.CategoryItemID, UserRating = e.UserRating }).ToList(); //Kullanıcın zaten takip ettikleriyle işimiz yok. var followlist = _db.UserFollow.Where(e => e.UserId == userId).Select(e => e.UserBeingFollowedUserId) .ToArray(); IQueryable <string> allUserIds; // followlananlar hariç tüm kullanıcıları çekiyoruz if (nearby) { allUserIds = _db.Users.Where(e => e.Id != userId).Where(e => !followlist.Contains(e.Id)).Where(e => e.UserCity == user.UserCity) .Select(e => e.Id); } else { allUserIds = _db.Users.Where(e => e.Id != userId).Where(e => !followlist.Contains(e.Id)) .Select(e => e.Id); } //tüm öğelerin ayrı ayrı rating ortalamasını hesaplar Up olarak geçiyor formülde var allRatingsDictionary = new Dictionary <int, float>(); foreach (var itemId in usersRatedItems) { var allRatedItemsRating = _db.UserRatings.Where(e => e.CategoryItemID == itemId) .Select(e => new UserAndRating { ItemId = e.CategoryItemID, UserRating = e.UserRating }).ToList(); allRatingsDictionary.Add(itemId, ((float)allRatedItemsRating.Sum(e => e.UserRating) / allRatedItemsRating.Count)); } //Diğer kullanıcıları çekiyoruz sadece kullanıcıyla aynı puanladığı itemler geliyor ????? var other = _db.UserRatings.Where(e => allUserIds.Contains(e.UserId)). //Where(e => usersRatedItems.Contains(e.CategoryItemID)). Select(e => new UserAndRating { UserId = e.UserId, ItemId = e.CategoryItemID, UserRating = e.UserRating }); //Diğer kullanıcı ratinglerine kolay ulaşmak için bir sözlük. var otherUsersAndRatings = new Dictionary <string, List <UserAndRating> >(); foreach (var ids in allUserIds) { var list = new List <UserAndRating>(); foreach (var userAndRating in other) { if (ids == userAndRating.UserId) { list.Add(userAndRating); } } otherUsersAndRatings.Add(ids, list); } //Kullanıcının rating ortalaması float userMean = (float)usersRatedItemsAndRatings.Sum(e => e.UserRating) / (float)usersRatedItemsAndRatings.Count; //Diğer kullanıcılar foreach (var otherUser in otherUsersAndRatings) { float otherUserMean = (float)otherUser.Value.Sum(e => e.UserRating) / (float)otherUser.Value.Count; float ortakPuanlanan = 0; float Proximity = 0; float Significance = 0; float Singularity = 0; float PSS = 0; float RoUser = 0; float RoOther = 0; float URP = 0; float JPSS = 0; float Jaccord = 0; //diğer kullanıcının Ratingleri foreach (var otherUserRating in otherUser.Value) { foreach (var usersRated in usersRatedItemsAndRatings) { if (usersRated.ItemId == otherUserRating.ItemId) { Proximity = 1 - (1 / (1 + ((float)Math.Exp(-Math.Abs(usersRated.UserRating - otherUserRating.UserRating))))); Significance = (1 / (1 + ((float)Math.Exp(-((Math.Abs(usersRated.UserRating - userMean)) * Math.Abs(otherUserRating.UserRating - otherUserMean)))))); Singularity = 1 - (1 / (1 + ((float)Math.Exp(-(Math.Abs((usersRated.UserRating + otherUserRating.UserRating) / 2) - allRatingsDictionary[usersRated.ItemId]))))); PSS += Proximity * Significance * Singularity; ortakPuanlanan++; } RoUser += (float)(Math.Pow(usersRated.UserRating - userMean, 2) / usersRatedItemsAndRatings.Count); } RoOther += (float)(Math.Pow(otherUserRating.UserRating - otherUserMean, 2) / otherUser.Value.Count); } RoUser = (float)Math.Sqrt(RoUser); RoOther = (float)Math.Sqrt(RoOther); Jaccord = (ortakPuanlanan) / (otherUser.Value.Count * usersRatedItemsAndRatings.Count); JPSS = PSS * Jaccord; URP = 1 - (1 / (1 + (float)(Math.Exp(-(Math.Abs(userMean - otherUserMean) * Math.Abs((RoUser - RoOther))))))); float sim = JPSS * URP; similarties.Add(new Similarity { OtherUserID = otherUser.Key, UserId = userId, UsersSimilarity = sim, RatedByUsersCount = otherUser.Value.Count }); } var similartiesOrdered = similarties.OrderByDescending(e => e.UsersSimilarity); foreach (var similarity in similartiesOrdered) { var tempUser = _db.Users.Find(similarity.OtherUserID); var temp = new FollowSuggestViewModel { UserId = similarity.OtherUserID, Name = tempUser.UserFirstName + " " + tempUser.UserSurname, ImagePath = tempUser.UserProfilePhoto, Similarity = Math.Round((1000 * similarity.UsersSimilarity), 2), UserCity = tempUser.UserCity }; usersToFollow.Add(temp); } return(usersToFollow); }
public static IQueryable <User> GetAllUsers(this ScoutUpDB context) { return(context.CompleteUsers()); }
public static User UserById(this ScoutUpDB context, string userId) { return(CompleteUsers(context).FirstOrDefault(e => e.Id == userId)); }
public static Post PostById(this ScoutUpDB context, int postId) { return(CompletePosts(context).FirstOrDefault(e => e.PostID == postId)); }
private LikePost SaveLike(int?postid, string userid) { var post = _db.Posts.Find(postid); var user = _db.Users.Find(userid); var isLiked = _db.PostLikes.Where(e => e.UserId == user.Id).FirstOrDefault(e => e.PostID == postid); if (isLiked != null) { using (var context = new ScoutUpDB()) { _db.Dispose(); context.PostLikes.Attach(isLiked); context.Entry(isLiked).State = EntityState.Deleted; context.SaveChanges(); return(new LikePost { LikeCount = post.PostLikes.Count, Liked = false, PostId = (int)postid }); } } var postLike = new PostLikes { UserId = user.Id, PostID = (int)postid, IsLiked = true }; try { using (var context = new ScoutUpDB()) { context.PostLikes.Add(postLike); context.UsersLastMoves.Add(new UsersLastMoves { MoveDate = DateTime.Now, UserId = user.Id, UsersLastMoveText = " bir gönderiyi beğendi.", UsersMoveLink = "/users/index/" + post.UserId + "#post" + post.PostID }); context.SaveChanges(); return(new LikePost { LikeCount = post.PostLikes.Count, Liked = true , PostId = (int)postid }); } } catch (Exception ex) { return(new LikePost { LikeCount = post.PostLikes.Count, Liked = false }); } // var postId = int.Parse(id); // var baseContext = Context.Request.GetHttpContext(); // var postRepository = _db; // var item = _db.Posts.Find(postId); // var liked = new PostLike // { // IPAddress = baseContext.Request.UserHostAddress, // PostId = item.Id, // UserAgent = baseContext.Request.UserAgent, // UserLike = true // }; // var dupe = item.PostLikes.FirstOrDefault(e => e.IPAddress == liked.IPAddress); // if (dupe == null) // { // item.PostLikes.Add(liked); // } // else // { // dupe.UserLike = !dupe.UserLike; // } // postRepository.SaveChanges(); // var post = postRepository.GetById(postId); // return new LikePost // { // LikeCount = post.PostLikes.Count(e => e.UserLike) // }; //} }