Beispiel #1
0
        public async Task Handle(AddFriendCommand command, CancellationToken cancellationToken)
        {
            if (command.CurrentUser == command.UserToAdd)
            {
                return;
            }

            await _repo.AddFriend(command.CurrentUser, command.UserToAdd);
        }
        public IActionResult AddFriend([FromBody] FriendRequestViewModel friendRequestVieModel)
        {
            try
            {
                var checkSuccess = _friendshipRepo.AddFriend(friendRequestVieModel.FromUserId, friendRequestVieModel.ToUserId);
                if (checkSuccess)
                {
                    //get the UserName of the request sender and create a notification
                    var RequestSender = _userRepo.Find(u => u.Id == friendRequestVieModel.FromUserId).SingleOrDefault();
                    var notification  = new Notification
                    {
                        NotificationTypeId       = (int)NotificationTypeConstants.FriendRequestSent,
                        NotificationDetails      = $"{RequestSender.FirstName + ' ' + RequestSender.LastName} sent you a friend request",
                        NotificationAcknowledged = false,
                        CreatedBy       = friendRequestVieModel.FromUserId,
                        CreatedDateTime = DateTime.UtcNow
                    };
                    _notificationRespository.Add(notification);
                    _notificationRespository.SaveChanges();

                    //get the notification id and save it in the UserNotificationRefTable
                    int NotificationId      = notification.NotificationId;
                    var userNotificationRef = new UserNotificationRef
                    {
                        NotificationId  = NotificationId,
                        RecipientUserId = friendRequestVieModel.ToUserId
                    };
                    _userNotificationRef.Add(userNotificationRef);
                    _userNotificationRef.SaveChanges();

                    return(Ok(true));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception e)
            {
                return(BadRequest());
            }
        }