Ejemplo n.º 1
0
        public async Task ShareAsync(ShareRecipe model, IValidator <ShareRecipe> validator)
        {
            ValidateAndThrow(model, validator);

            var now = DateTime.UtcNow;

            var newShares = new List <RecipeShare>();

            foreach (int userId in model.NewShares)
            {
                if (_recipesRepository.UserHasBlockedSharing(model.RecipeId, model.UserId, userId))
                {
                    continue;
                }

                newShares.Add(new RecipeShare
                {
                    RecipeId       = model.RecipeId,
                    UserId         = userId,
                    LastOpenedDate = now,
                    CreatedDate    = now,
                    ModifiedDate   = now
                });
            }

            var removedShares = model.RemovedShares.Select(x => new RecipeShare
            {
                RecipeId = model.RecipeId,
                UserId   = x
            });

            await _recipesRepository.SaveSharingDetailsAsync(newShares, removedShares);
        }
        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());
        }