コード例 #1
0
ファイル: UserSocket.cs プロジェクト: Toni751/SEP3_Tier3
        /// <summary>
        /// Creates a new user action and its corresponding notification, if any
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> PostUserActionAsync(ActualRequest actualRequest)
        {
            Request            request            = actualRequest.Request;
            ModelActionSockets modelActionSockets =
                JsonSerializer.Deserialize <ModelActionSockets>(request.Argument.ToString());

            Console.WriteLine("Posting user action " + modelActionSockets.ActionType);
            int notificationId;

            if (modelActionSockets.ActionType.Equals("USER_FRIEND_REMOVE"))
            {
                notificationId = await userRepo.RemoveFriendshipAsync(modelActionSockets);
            }
            else if (modelActionSockets.ActionType.Equals("USER_RATE_PAGE"))
            {
                notificationId = await userRepo.PostPageRatingAsync(modelActionSockets);
            }
            else
            {
                notificationId = await userRepo.PostUserActionAsync(modelActionSockets);
            }

            Request responseRequest = new Request
            {
                ActionType = modelActionSockets.ActionType,
                Argument   = JsonSerializer.Serialize(notificationId)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
コード例 #2
0
        public async Task <int> PostPageRatingAsync(ModelActionSockets modelActionSockets)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                PageRating pageRating = await ctx.PageRatings.FirstOrDefaultAsync(pr =>
                                                                                  pr.UserId == modelActionSockets.SenderId && pr.PageId == modelActionSockets.ReceiverId);

                if (pageRating == null)
                {
                    await ctx.PageRatings.AddAsync(new PageRating
                    {
                        UserId = modelActionSockets.SenderId,
                        PageId = modelActionSockets.ReceiverId,
                        Rating = int.Parse(modelActionSockets.Value.ToString())
                    });
                }
                else
                {
                    pageRating.Rating = int.Parse(modelActionSockets.Value.ToString());
                    ctx.PageRatings.Update(pageRating);
                }

                await ctx.SaveChangesAsync();

                return(0);
            }
        }
コード例 #3
0
        public async Task <int> RemoveFriendshipAsync(ModelActionSockets modelActionSockets)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    Friendship friendship = await ctx.Friendships.FirstAsync(fr =>
                                                                             (fr.FirstUserId == modelActionSockets.SenderId &&
                                                                              fr.SecondUserId == modelActionSockets.ReceiverId) ||
                                                                             (fr.FirstUserId == modelActionSockets.ReceiverId &&
                                                                              fr.SecondUserId == modelActionSockets.SenderId));

                    ctx.Friendships.Remove(friendship);
                    await ctx.SaveChangesAsync();

                    return(0);
                }
                catch (Exception e)
                {
                    return(-1);
                }
            }
        }
コード例 #4
0
        public async Task <int> PostUserActionAsync(ModelActionSockets modelActionSockets)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                UserAction userAction = await ctx.UserActions.FirstOrDefaultAsync(ua =>
                                                                                  ua.SenderId == modelActionSockets.SenderId &&
                                                                                  ua.ReceiverId == modelActionSockets.ReceiverId);

                UserAction ifNullUserAction = new UserAction();
                if (userAction == null)
                {
                    ifNullUserAction = new UserAction
                    {
                        SenderId   = modelActionSockets.SenderId,
                        ReceiverId = modelActionSockets.ReceiverId
                    }
                }
                ;

                if (modelActionSockets.Value.ToString().Equals("False"))
                {
                    modelActionSockets.Value = false;
                }
                if (modelActionSockets.Value.ToString().Equals("True"))
                {
                    modelActionSockets.Value = true;
                }

                int returnId = 0;
                switch (modelActionSockets.ActionType)
                {
                case "USER_FRIEND_REQUEST_SEND":
                    if (userAction != null)
                    {
                        userAction.IsFriendRequest = (bool)modelActionSockets.Value;
                    }
                    else
                    {
                        ifNullUserAction.IsFriendRequest = (bool)modelActionSockets.Value;
                    }
                    if ((bool)modelActionSockets.Value)
                    {
                        returnId = await AddNotification("USER_FRIEND_REQUEST_SEND", modelActionSockets.SenderId, modelActionSockets.ReceiverId);

                        Console.WriteLine("Returning notification id: " + returnId);
                    }
                    else
                    {
                        Notification notification = await ctx.Notifications.FirstAsync(n =>
                                                                                       n.SenderId == modelActionSockets.SenderId && n.ReceiverId == modelActionSockets.ReceiverId &&
                                                                                       n.NotificationType.Equals("USER_FRIEND_REQUEST_SEND"));

                        ctx.Notifications.Remove(notification);
                    }
                    break;

                case "USER_FRIEND_REQUEST_RESPONSE":
                    if (userAction != null)
                    {
                        userAction.IsFriendRequest = false;
                        if ((bool)modelActionSockets.Value)
                        {
                            await ctx.Friendships.AddAsync(new Friendship
                            {
                                FirstUserId  = modelActionSockets.SenderId,
                                SecondUserId = modelActionSockets.ReceiverId
                            });
                        }
                    }

                    break;

                case "USER_SHARE_TRAININGS":
                    if (userAction != null)
                    {
                        userAction.IsShareTrainings = (bool)modelActionSockets.Value;
                    }
                    else
                    {
                        ifNullUserAction.IsShareTrainings = (bool)modelActionSockets.Value;
                    }
                    break;

                case "USER_SHARE_DIETS":
                    if (userAction != null)
                    {
                        userAction.IsShareDiets = (bool)modelActionSockets.Value;
                    }
                    else
                    {
                        ifNullUserAction.IsShareDiets = (bool)modelActionSockets.Value;
                    }
                    break;

                case "USER_FOLLOW_PAGE":
                    if (userAction != null)
                    {
                        userAction.IsFollowPage = (bool)modelActionSockets.Value;
                    }
                    else
                    {
                        ifNullUserAction.IsFollowPage = (bool)modelActionSockets.Value;
                    }
                    if ((bool)modelActionSockets.Value)
                    {
                        returnId = await AddNotification("USER_FOLLOW_PAGE", modelActionSockets.SenderId, modelActionSockets.ReceiverId);

                        Console.WriteLine("Returning notification id: " + returnId);
                    }
                    // else {
                    //     Notification notification = await ctx.Notifications.FirstAsync(n =>
                    //         n.SenderId == modelActionSockets.SenderId && n.ReceiverId == modelActionSockets.ReceiverId
                    //                                                   && n.NotificationType.Equals("USER_FOLLOW_PAGE"));
                    //     ctx.Notifications.Remove(notification);
                    // }
                    break;

                case "USER_REPORT":
                    if (userAction != null)
                    {
                        userAction.IsReport = (bool)modelActionSockets.Value;
                    }
                    else
                    {
                        ifNullUserAction.IsReport = (bool)modelActionSockets.Value;
                    }
                    break;
                }

                if (userAction == null)
                {
                    await ctx.UserActions.AddAsync(ifNullUserAction);
                }
                else
                {
                    if (userAction.IsReport || userAction.IsFollowPage || userAction.IsFriendRequest ||
                        userAction.IsShareDiets || userAction.IsShareTrainings)
                    {
                        ctx.UserActions.Update(userAction);
                    }
                    else
                    {
                        ctx.UserActions.Remove(userAction);
                    }
                }

                try
                {
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(-1);
                }

                return(returnId);
            }
        }