Beispiel #1
0
        public async Task <IActionResult> CreateInviteAsync(
            [FromBody] HouseInviteCreationRequestViewModel houseInviteCreationRequestViewModel,
            CancellationToken cancellationToken = default)
        {
            var result = await _houseInviteService.CreateInviteAsync(
                houseInviteCreationRequestViewModel,
                cancellationToken);

            return(result);
        }
        public async Task <ObjectResult> CreateInviteAsync(
            HouseInviteCreationRequestViewModel houseInviteCreationRequestViewModel,
            CancellationToken cancellationToken = default)
        {
            var userFromIdentity = GetUserFromIdentity();

            var houseInviteCreationRequest = _mapper.Map <HouseInviteCreationRequest>(houseInviteCreationRequestViewModel);

            houseInviteCreationRequest.InvitedByUserId = userFromIdentity.GoogleId;

            HouseInviteSimpleInfo houseInvite;

            try
            {
                houseInvite = await _houseInviteService.CreateInviteAsync(
                    houseInviteCreationRequest,
                    cancellationToken);
            }
            catch (TooManyHouseInvitesException)
            {
                var modelState = new ModelStateDictionary();
                modelState.AddModelError(
                    nameof(houseInviteCreationRequestViewModel.HouseId),
                    "Too many requests from you are still pending acceptance");

                var badResult = new BadRequestObjectResult(modelState);
                return(badResult);
            }
            catch (HouseInviteDuplicateInsertionException)
            {
                var modelState = new ModelStateDictionary();
                modelState.AddModelError(
                    nameof(houseInviteCreationRequestViewModel.UserEmail),
                    "User already invited");

                var badResult = new BadRequestObjectResult(modelState);
                return(badResult);
            }
            catch (HouseNotFoundException)
            {
                var modelState = new ModelStateDictionary();
                modelState.AddModelError(
                    nameof(houseInviteCreationRequestViewModel.HouseId),
                    "House not found");

                var badResult = new BadRequestObjectResult(modelState);
                return(badResult);
            }
            catch (SelfReferencingHouseInviteException)
            {
                var modelState = new ModelStateDictionary();
                modelState.AddModelError(
                    nameof(houseInviteCreationRequestViewModel.UserEmail),
                    "House owner can't invite himself");

                var badResult = new BadRequestObjectResult(modelState);
                return(badResult);
            }

            var houseInviteViewModel = _mapper.Map <HouseInviteSimpleInfoViewModel>(houseInvite);

            var result = new OkObjectResult(houseInviteViewModel);

            return(result);
        }