private async Task <IActionResult> SendGameEventInvitation(
            GameEventDto gameEvent,
            string userName,
            SendGameEventInvitationDto sendGameEventInvitationDto)
        {
            try
            {
                await _gameEventParticipationService.CreateGameEventParticipationAsync(sendGameEventInvitationDto);
            }
            catch (ApplicationException exception)
            {
                return(Error.FromController(this).ErrorJson("Error!", exception.Message, HttpStatusCode.BadRequest));
            }
            catch
            {
                return(Error.FromController(this).ErrorJson(
                           "Error!",
                           "An unexpected error has occured while processing your request.",
                           HttpStatusCode.InternalServerError));
            }

            return(Ok(
                       new
            {
                title = "Invite sent.",
                message =
                    $"An email with your invitation to the {gameEvent.Name} event has been sent to {userName}."
            }));
        }
        public async Task <IActionResult> SendGameEventInvite(int gameEventId, string userName)
        {
            GameEventDto gameEvent;

            var sendGameEventInvitationDto = new SendGameEventInvitationDto(
                gameEventId,
                userName,
                eventId => _hostConfiguration.HostAddress + Url.Action(
                    "GameEvent",
                    "GameEvent",
                    new { id = eventId }));

            try
            {
                gameEvent = await _gameEventService.GetGameEventAsync(gameEventId);
            }
            catch (ApplicationException exception)
            {
                return(Error.FromController(this).ErrorJson("Error!", exception.Message, HttpStatusCode.BadRequest));
            }
            catch
            {
                return(Error.FromController(this).ErrorJson(
                           "Error!",
                           "An unexpected error has occured while processing your request.",
                           HttpStatusCode.InternalServerError));
            }

            if (gameEvent == null)
            {
                return(Error.FromController(this).ErrorJson(
                           "Error!",
                           "Specified game event does not exist.",
                           HttpStatusCode.NotFound));
            }

            if (gameEvent.Creator.UserName != User.Identity.Name)
            {
                return(Error.FromController(this).ErrorJson(
                           "Error!",
                           "You're unauthorized to perform this action.",
                           HttpStatusCode.Unauthorized));
            }

            return(await SendGameEventInvitation(gameEvent, userName, sendGameEventInvitationDto));
        }
Exemplo n.º 3
0
        public async Task CreateGameEventParticipationAsync(SendGameEventInvitationDto gameEventInvitationDto)
        {
            var gameEventId = gameEventInvitationDto.GameEventId;
            var userNameTo  = gameEventInvitationDto.UserNameTo;

            var participation = await GetActiveGameEventParticipation(gameEventId, userNameTo);

            if (participation != null)
            {
                switch (participation.ParticipationStatus)
                {
                case ParticipationStatus.PendingGuest:
                    throw new GameEventParticipationException("You have already invited this user to this event.");

                case ParticipationStatus.AcceptedGuest:
                    throw new GameEventParticipationException("This user already participates in this event.");

                case ParticipationStatus.Creator:
                    throw new GameEventParticipationException("You cannot invite yourself.");

                // Nothing else can be returned by GetActiveGameEventParticipation.
                default:
                    throw new ArgumentOutOfRangeException(
                              nameof(participation.ParticipationStatus),
                              $"The value {participation.ParticipationStatus} of ParticipationStatus " +
                              "is not included in this switch statement.");
                }
            }

            var userTo = await _repository.ApplicationUsers.SingleAsync(ApplicationUser.UserNameEquals(userNameTo));

            using (var transaction = _repository.BeginTransaction())
            {
                await CreateNewGameEventParticipation(gameEventId, userTo.Id);
                await SendGameEventInvitationAsync(
                    gameEventInvitationDto.GameEventId,
                    userTo,
                    gameEventInvitationDto.GenerateGameEventLink);

                transaction.Commit();
            }
        }