void AddUserLike(WallPostLike like)
        {
            if (like.User == null)
            {
                return;
            }

            if (Users.ContainsKey(like.User.Id))
            {
                return;
            }

            UserModel model = AppModel.Instance.UsersModelsWrapper.UsersModels.Find(like.User.Id);
            bool      isNew;

            if (model == null)
            {
                model = AppModel.Instance.UsersModelsWrapper.AddModel(like.User, out isNew);
            }

            Users.Add(like.User.Id, model);
            Device.BeginInvokeOnMainThread(() => {
                InsertItem(0, model);
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(
                                        NotifyCollectionChangedAction.Add, model));
            });
        }
        protected override void OnResult(AddWallPostLike result)
        {
            WallPostLike like = new WallPostLike()
            {
                Id            = result.Id,
                CreatedAtTime = result.CreatedAtTime,
                UpdatedAtTime = result.UpdatedAtTime,
                User          = User,
                Post          = WallPost.Id,
                UserId        = User.Id
            };

            AppModel.Instance.WallPostLikes.AddOne(like);
            DbClient.Instance.SaveItemData <WallPostLike> (like).ConfigureAwait(false);
        }
Beispiel #3
0
        public LikeResult LikeWallPost(int userId, int postId)
        {
            User user = ctx.Users.First(u => u.Id == userId);

            WallPost post = ctx.UserPosts.First(p => p.Id == postId);

            if (ctx.UserPostLikes.Any(l => l.LikerId == user.Id && l.WallPostId == postId))
            {
                List <WallPostLike> likes =
                    ctx.UserPostLikes.Where(l => l.LikerId == user.Id && l.WallPostId == postId).ToList();
                ctx.UserPostLikes.RemoveRange(likes);
                ctx.SaveChanges();
                return(LikeResult.Unliked);
            }
            else
            {
                WallPostLike like = new WallPostLike();

                like.WallPost   = post;
                like.WallPostId = post.Id;

                like.Liker   = user;
                like.LikerId = user.Id;

                ctx.UserPostLikes.Add(like);


                if (post.Owner.Id != user.Id)
                {
                    Notification notification = Notification.From(DateTime.Now, NotificationType.WallPostWritten,
                                                                  user, post.Owner, "@" + user.Username + " оцінив запис на вашій стіні", "/Home/Post/" + postId);
                    ctx.Notifications.Add(notification);
                }
                ctx.SaveChanges();
                return(LikeResult.Liked);
            }
        }