public async Task <IActionResult> TryImport([FromBody] ImportRecipeDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            var importModel = new ImportRecipe();

            try
            {
                importModel.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            if (dto.CheckIfReviewRequired && _recipeService.IngredientsReviewIsRequired(dto.Id, importModel.UserId))
            {
                return(Ok(0));
            }

            importModel.Id = dto.Id;
            importModel.IngredientReplacements = dto.IngredientReplacements;

            // Copy recipe image if not default
            RecipeToNotify recipe = await _recipeService.GetAsync(importModel.Id);

            string tempImagePath = Path.Combine(_webHostEnvironment.ContentRootPath, "storage", "temp", Guid.NewGuid().ToString());

            importModel.ImageUri = await _cdnService.CopyAndUploadAsync(
                tempImagePath : tempImagePath,
                imageUriToCopy : recipe.ImageUri,
                uploadPath : $"users/{importModel.UserId}/recipes",
                template : "recipe"
                );

            int id = await _recipeService.ImportAsync(importModel, _importRecipeValidator);

            // Notify
            User currentUser = await _userService.GetAsync(importModel.UserId);

            User recipeUser = await _userService.GetAsync(recipe.UserId);

            CultureInfo.CurrentCulture = new CultureInfo(recipeUser.Language, false);
            var message = _localizer["AcceptedSendRequestNotification", IdentityHelper.GetUserName(User), recipe.Name];

            var pushNotification = new PushNotification
            {
                SenderImageUri = currentUser.ImageUri,
                UserId         = recipeUser.Id,
                Application    = "Cooking Assistant",
                Message        = message
            };

            _senderService.Enqueue(pushNotification);

            return(StatusCode(201, id));
        }
        public async Task <IActionResult> SetShareIsAccepted([FromBody] SetShareIsAcceptedDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            int userId;

            try
            {
                userId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            await _recipeService.SetShareIsAcceptedAsync(dto.RecipeId, userId, dto.IsAccepted);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfRecipeChangeAsync(dto.RecipeId, userId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(userId);

                RecipeToNotify recipe = await _recipeService.GetAsync(dto.RecipeId);

                var localizerKey = dto.IsAccepted ? "JoinedRecipeNotification" : "DeclinedShareRequestNotification";

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer[localizerKey, IdentityHelper.GetUserName(User), recipe.Name];

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "Cooking Assistant",
                        Message        = message
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Send([FromBody] CreateSendRequest dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            await _recipeService.SendAsync(dto, _createSendRequestValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfRecipeSentAsync(dto.RecipeId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                RecipeToNotify recipe = await _recipeService.GetAsync(dto.RecipeId);

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["SentRecipeNotification", IdentityHelper.GetUserName(User), recipe.Name];

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "Cooking Assistant",
                        Message        = message,
                        OpenUrl        = $"{_urls.CookingAssistant}/inbox"
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(StatusCode(201, null));
        }
        public async Task <IActionResult> Leave(int id)
        {
            int userId;

            try
            {
                userId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            bool shareWasAccepted = await _recipeService.LeaveAsync(id, userId);

            // Notify if joined in the first place
            if (shareWasAccepted)
            {
                var usersToBeNotified = await _userService.GetToBeNotifiedOfRecipeChangeAsync(id, userId);

                if (usersToBeNotified.Any())
                {
                    var currentUser = await _userService.GetAsync(userId);

                    RecipeToNotify recipe = await _recipeService.GetAsync(id);

                    foreach (var user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["LeftRecipeNotification", IdentityHelper.GetUserName(User), recipe.Name];

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "Cooking Assistant",
                            Message        = message
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Share([FromBody] ShareRecipe dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            foreach (int removedUserId in dto.RemovedShares)
            {
                // Notify
                if (await _userService.CheckIfUserCanBeNotifiedOfRecipeChangeAsync(dto.RecipeId, removedUserId))
                {
                    var currentUser = await _userService.GetAsync(dto.UserId);

                    var user = await _userService.GetAsync(removedUserId);

                    RecipeToNotify recipe = await _recipeService.GetAsync(dto.RecipeId);

                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["RemovedShareNotification", IdentityHelper.GetUserName(User), recipe.Name];

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "Cooking Assistant",
                        Message        = message
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            await _recipeService.ShareAsync(dto, _shareValidator);

            return(NoContent());
        }
        public async Task <IActionResult> Update([FromBody] UpdateRecipe dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            RecipeToNotify original = await _recipeService.UpdateAsync(dto, _updateRecipeValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfRecipeChangeAsync(original.Id, dto.UserId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["UpdatedRecipeNotification", IdentityHelper.GetUserName(User), original.Name];

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "Cooking Assistant",
                        Message        = message
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> DeclineSendRequest([FromBody] DeclineSendRequestDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            int userId;

            try
            {
                userId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            await _recipeService.DeclineSendRequestAsync(dto.RecipeId, userId);

            // Notify
            var currentUser = await _userService.GetAsync(userId);

            RecipeToNotify recipe = await _recipeService.GetAsync(dto.RecipeId);

            var recipeUser = await _userService.GetAsync(recipe.UserId);

            CultureInfo.CurrentCulture = new CultureInfo(recipeUser.Language, false);
            var message = _localizer["DeclinedSendRequestNotification", IdentityHelper.GetUserName(User), recipe.Name];

            var pushNotification = new PushNotification
            {
                SenderImageUri = currentUser.ImageUri,
                UserId         = recipeUser.Id,
                Application    = "Cooking Assistant",
                Message        = message
            };

            _senderService.Enqueue(pushNotification);

            return(NoContent());
        }