public async Task <IActionResult> Delete(int id, IFormCollection collection)
        {
            var deletedBlogPost = await _blogPostRepository.GetByIdAsync(id);

            String postUrl           = "wwwroot/images/" + deletedBlogPost.BlogPostId.ToString() + "/";
            var    DirectoryToDelete = Path.Combine(Directory.GetCurrentDirectory(), postUrl);

            try
            {
                Directory.Delete(DirectoryToDelete, true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                // var notDeletedBlogPost = await _blogPostRepository.GetByIdAsync(id);
                //PostDetailViewModel myPostDetailViewModel = new PostDetailViewModel();
            }
            try
            {
                await _blogPostRepository.DeleteAsync(deletedBlogPost);

                await _blogTagAppliedRepository.GetAllByPostId(deletedBlogPost.BlogPostId, true);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(await Delete(id));
            }
        }
Exemple #2
0
        public async Task <ActionResult> Delete(int id)
        {
            var post = await repository.GetAsync(id); //[Note] - potencjalnie zawsze można usuwać od razu - czy w praktyce tak się robi, czy raczej od razu próbować usunąć dla lepszej wydajności?

            if (post == null)
            {
                logger.LogWarning("Element with given ID doesn't exist - nothing is deleted");
                return(NoContent());
            }

            await repository.DeleteAsync(id);

            logger.LogInformation("Element with given ID has been successfully removed");
            return(NoContent()); //[Note] Jak zwrócić element usunięty? Ma być wtedy zwracany status OK z obiektem? [OK + obiekt]
        }
        public async Task <IActionResult> Delete(long id)
        {
            await _postsRepo.DeleteAsync(id);

            return(NoContent());
        }
Exemple #4
0
        public async Task SynchronizeBlogPostsAsync(IBlogPostRepository sourceRepo,
                                                    IBlogPostRepository destRepo,
                                                    bool incremental,
                                                    string overrideCursor,
                                                    CancellationToken cancellationToken)
        {
            try
            {
                await _lockRepository.AcquireLockAsync("synchelperlock", 10, TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1), cancellationToken);

                _logger.LogInformation($"SynchronizeBlogPostsAsync with incremental = {incremental}");

                var dropboxCursor = new CursorContainer();

                if (incremental)
                {
                    // Try to get a persisted cursor from our SQL database, if that's null (so we haven't got one), then we'll do a full update
                    dropboxCursor.Cursor = overrideCursor ?? await destRepo.GetDropboxCursorAsync(cancellationToken);

                    _logger.LogDebug($"Cursor = {dropboxCursor.Cursor}");
                }

                var cursor      = incremental && !string.IsNullOrWhiteSpace(dropboxCursor.Cursor) ? dropboxCursor : null;
                var sourcePosts = (await sourceRepo.GetAllAsync(cursor, cancellationToken)).ToList();

                if (string.IsNullOrWhiteSpace(dropboxCursor.Cursor))
                {
                    _logger.LogInformation("No current dropbox cursor, so explicitly requesting current cursor ...");
                    dropboxCursor.Cursor = await _dropboxHelper.GetCurrentCursorAsync(cancellationToken);

                    _logger.LogInformation($"Returned cursor {dropboxCursor.Cursor}");
                }

                _logger.LogInformation($"Processing {sourcePosts.Count} source posts ...");

                foreach (var sourcePost in sourcePosts)
                {
                    await destRepo.AddOrUpdateAsync(sourcePost, cancellationToken);
                }

                _logger.LogInformation("Processing images ...");

                var imageTasks = new List <Task>();

                foreach (var sourcePost in sourcePosts)
                {
                    foreach (var imageData in sourcePost.ImageData)
                    {
                        var imageContent = await imageData.ImageDataTask;

                        var resizedImageFileContent = imageData.FileName.ToLower().Contains("noresize")
                            ? imageContent
                            : _imageResizer.Resize(imageContent, _settings.MaxResizedImageSize);

                        imageTasks.Add(_imageRepository.AddAsync(imageData.PostFolder, imageData.FileName, resizedImageFileContent, cancellationToken));
                    }
                }

                await Task.WhenAll(imageTasks);

                if (!incremental) // Do not delete posts when in incremental mode (todo) Is this comment correct? Surely as we're reading the json file even on incremental sync, we can still delete on incremental?
                {
                    var destPosts = (await destRepo.GetAllAsync(null, cancellationToken)).ToList();

                    var postsToDelete = destPosts.Where(d => sourcePosts.All(s => s.Id != d.Id)).ToList();

                    _logger.LogInformation($"Found {postsToDelete.Count} to delete out of {destPosts.Count} posts");

                    await destRepo.DeleteAsync(postsToDelete, cancellationToken);
                }

                await destRepo.RemoveUnusedTagsAsync(cancellationToken);

                _logger.LogInformation($"Saving new Dropbox cursor: {dropboxCursor.Cursor}");

                await destRepo.SetDropboxCursorAsync(dropboxCursor.Cursor, cancellationToken);
            }
            finally
            {
                await _lockRepository.ReleaseLockAsync(cancellationToken);
            }
        }