示例#1
0
        public async Task <ActionResult <ApiResultViewModel <FriendRequest> > > SendFriendRequest([FromRoute] long accountId,
                                                                                                  [FromBody] FriendRequestInputModel inputModel, CancellationToken cancellationToken)
        {
            if (accountId != AccountId)
            {
                return(Forbidden());
            }

            if (inputModel.FriendAccountId == accountId)
            {
                return(BadRequest("invalid_friendAccountId", "you cannot friend yourself"));
            }

            var friendAccount = await _accountManager.GetAsync(inputModel.FriendAccountId, cancellationToken);

            if (friendAccount == null || friendAccount.IsArchived)
            {
                return(BadRequest("invalid_friendAcccountId", "friend account not found."));
            }

            var friendRequest =
                await _friendRequestManager.GetExistingRequest(accountId, inputModel.FriendAccountId,
                                                               cancellationToken);

            if (friendRequest != null)
            {
                return(BadRequest("already_exists", "Friend request already sent."));
            }

            if (await _accountFriendManager.IsFriendAsync(accountId, inputModel.FriendAccountId, cancellationToken))
            {
                return(BadRequest("already_friends", "Requested player is already in your friend list."));
            }

            friendRequest = new FriendRequest
            {
                FromAccountId   = accountId,
                ToAccountId     = inputModel.FriendAccountId,
                RequestDateTime = DateTime.UtcNow,
                StatusId        = FriendRequestStatus.Pending
            };

            var account = await _accountManager.GetAsync(accountId, cancellationToken);

            friendRequest = await _friendRequestManager.SaveAsync(friendRequest, cancellationToken);

            await _notificationManager.SaveAsync(
                new Core.Entities.Notification
            {
                AccountId          = friendRequest.ToAccountId,
                Body               = $"friend request sent by {account.Email}",
                NotificationTypeId = NotificationTypeIds.General,
                Title              = "Friend Request"
            }, cancellationToken);

            return(CreatedData(friendRequest));
        }