Esempio n. 1
0
        public IHttpActionResult AcceptRequest(AcceptFriendRequestInputModel model)
        {
            if (!this.CheckIfUserExist(model.FriendId))
            {
                return(this.BadRequest($"User with id {model.FriendId} does not exist."));
            }

            int  userId         = GetUserId();
            bool isRequestExist = this.ChechIfRequestExist(userId, STATUS_PENDING, model.FriendId);

            if (!isRequestExist)
            {
                return(this.BadRequest($"You don't have request from user with id {model.FriendId}."));
            }

            try
            {
                this.friendRequestSevice.AcceptRequest(userId, model.FriendId);
                this.communicationService.Create(userId, model.FriendId);

                return(Ok($"User with id {model.FriendId} friend request is accepted successfully."));
            }
            catch (Exception)
            {
                return(this.BadRequest("Invalid operation.Try again."));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> AcceptFriendRequest([FromBody] AcceptFriendRequestInputModel input,
                                                              CancellationToken cancellationToken)
        {
            var friendRequest = await _friendRequestManager.GetAsync(input.FriendRequestId, cancellationToken);

            if (friendRequest == null)
            {
                return(NotFound());
            }

            if (AccountId != friendRequest.ToAccountId)
            {
                return(Forbidden());
            }

            if (friendRequest.StatusId != FriendRequestStatus.Pending)
            {
                return(BadRequest("invalid_state", "friend request in invalid state"));
            }

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                friendRequest.StatusId = FriendRequestStatus.Accepted;
                await _friendRequestManager.SaveAsync(friendRequest, cancellationToken);

                await _accountFriendManager.SaveAsync(new AccountFriend
                {
                    AccountId       = friendRequest.FromAccountId,
                    FriendAccountId = friendRequest.ToAccountId
                }, cancellationToken);

                await _accountFriendManager.SaveAsync(new AccountFriend
                {
                    AccountId       = friendRequest.ToAccountId,
                    FriendAccountId = friendRequest.FromAccountId
                }, cancellationToken);

                transaction.Complete();
            }

            return(Ok());
        }