Ejemplo n.º 1
0
        private async Task <IActionResult> TrySendFriendRequestAsync(int userId, int friendId)
        {
            var userToFriend = await _context.UserToFriends.FirstOrDefaultAsync(x => x.UserId == userId && x.FriendId == friendId);

            if (userToFriend != null)             // Уже друзья
            {
                return(new JsonResult(new Response {
                    Ok = false, StatusCode = 403, Description = "Пользователи уже в друзьях друг у друга"
                }));
            }

            var userToRequest = await _context.UserToRequests.FirstOrDefaultAsync(x => x.UserId == userId && x.RequestId == friendId);

            if (userToRequest != null)             // Уже имеет запрос пользователю с Id = friendId. Аналогично уже имеется зеркальная запись в подписчиках юзера с Id = friendId
            {
                return(new JsonResult(new Response {
                    Ok = false, StatusCode = 403, Description = "Пользователь уже отправил заявку"
                }));
            }

            var mirrorUserToRequest = await _context.UserToRequests.FirstOrDefaultAsync(x => x.UserId == friendId && x.RequestId == userId);

            if (mirrorUserToRequest != null)             // Тот пользователь уже подписан, т.е. взаимная дружба => добавляем в друзья
            {
                var newUserToFriend = new UserToFriend {
                    UserId = userId, FriendId = friendId
                };
                var newMirrorUserToFriend = new UserToFriend {
                    UserId = friendId, FriendId = userId
                };
                await _context.UserToFriends.AddRangeAsync(newUserToFriend, newMirrorUserToFriend);

                // И удаляю запрос
                _context.UserToRequests.Remove(mirrorUserToRequest);
                // И удаляю подписчика
                var userToFollower = await _context.UserToFollowers.FirstAsync(x => x.UserId == userId && x.FollowerId == friendId);

                _context.UserToFollowers.Remove(userToFollower);

                await _context.SaveChangesAsync();

                return(new JsonResult(new Response {
                    Ok = true, StatusCode = 200, Result = new { Message = "Added", Id = newUserToFriend.Id }
                }));
            }
            else             // Тот пользователь не подписан, т.е. отправляется запрос на дружбу => добавляем запрос и подписчика
            {
                var newUserToRequest = new UserToRequest {
                    UserId = userId, RequestId = friendId
                };                                                                                                  // У userId добавляем запрос (кому?) friendId
                _context.UserToRequests.Add(newUserToRequest);
                var newUserToFollower = new UserToFollower {
                    UserId = friendId, FollowerId = userId
                };                                                                                                     // У friendId добавляем подписчика (кого?) userId
                _context.UserToFollowers.Add(newUserToFollower);
                await _context.SaveChangesAsync();

                return(new JsonResult(new Response {
                    Ok = true, StatusCode = 200, Result = new { Message = "Requested", Id = newUserToRequest.Id }
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task InvokeAsync(HttpContext context)
        {
            _db          = context.RequestServices.GetService(typeof(ApplicationContext)) as ApplicationContext;
            _userManager = context.RequestServices.GetService(typeof(UserManager <User>)) as UserManager <User>;

            if (_db.Users.Count() != 0)
            {
                await _next.Invoke(context);

                return;
            }

            var user1 = new User {
                UserName = "******", NormalizedUserName = "******"
            };
            var user2 = new User {
                UserName = "******", NormalizedUserName = "******"
            };
            var user3 = new User {
                UserName = "******", NormalizedUserName = "******"
            };
            await _userManager.CreateAsync(user1, "123456");

            await _userManager.CreateAsync(user2, "123456");

            await _userManager.CreateAsync(user3, "123456");


            var dialog1 = new Dialog {
                Title = "Первый диалог", CreatingTime = DateTime.Now, LastMessageTime = DateTime.Now
            };
            var dialog2 = new Dialog {
                Title = "Второй диалог", CreatingTime = DateTime.Now, LastMessageTime = DateTime.Now
            };
            var dialog3 = new Dialog {
                Title = "Третий диалог", CreatingTime = DateTime.Now, LastMessageTime = DateTime.Now
            };
            await _db.Dialogs.AddRangeAsync(dialog1, dialog2, dialog3);

            await _db.SaveChangesAsync();


            dialog1.Users = new List <UserToDialog>
            {
                new UserToDialog {
                    Dialog = dialog1, User = user1
                },
                new UserToDialog {
                    Dialog = dialog1, User = user2
                },
                new UserToDialog {
                    Dialog = dialog1, User = user3
                },
            };
            dialog2.Users = new List <UserToDialog>
            {
                new UserToDialog {
                    Dialog = dialog2, User = user1
                },
                new UserToDialog {
                    Dialog = dialog2, User = user3
                },
            };
            dialog3.Users = new List <UserToDialog>
            {
                new UserToDialog {
                    Dialog = dialog3, User = user2
                },
            };
            _db.Dialogs.UpdateRange(dialog1, dialog2, dialog3);
            await _db.SaveChangesAsync();


            var message1 = new Message {
                Text = "Привет", Author = user1, Dialog = dialog1, SendingTime = DateTime.Now
            };
            var message2 = new Message {
                Text = "Это я!", Author = user1, Dialog = dialog2, SendingTime = DateTime.Now
            };
            var message3 = new Message {
                Text = "Здравствуй!", Author = user2, Dialog = dialog3, SendingTime = DateTime.Now
            };
            var message4 = new Message {
                Text = "Как дела?", Author = user1, Dialog = dialog3, SendingTime = DateTime.Now
            };
            var message5 = new Message {
                Text = "Дурка уже в пути", Author = user2, Dialog = dialog2, SendingTime = DateTime.Now
            };
            await _db.Messages.AddRangeAsync(message1, message2, message3, message4, message5);

            await _db.SaveChangesAsync();


            var uf1 = new UserToFriend {
                User = user1, FriendId = user2.Id
            };
            var uf2 = new UserToFriend {
                User = user1, FriendId = user3.Id
            };
            var uf3 = new UserToFriend {
                User = user2, FriendId = user1.Id
            };
            var uf5 = new UserToFriend {
                User = user3, FriendId = user1.Id
            };

            _db.UserToFriends.AddRange(uf1, uf2, uf3, uf5);
            await _db.SaveChangesAsync();


            var ufw1 = new UserToFollower {
                User = user2, FollowerId = user3.Id
            };
            var ur1 = new UserToRequest {
                User = user3, RequestId = user2.Id
            };

            _db.UserToFollowers.AddRange(ufw1);
            _db.UserToRequests.AddRange(ur1);
            await _db.SaveChangesAsync();

            await _next.Invoke(context);
        }