public async Task <Result <int> > Handle(DeleteDocumentCommand command, CancellationToken cancellationToken)
        {
            var document = await _unitOfWork.Repository <Document>().GetByIdAsync(command.Id);

            await _unitOfWork.Repository <Document>().DeleteAsync(document);

            await _unitOfWork.Commit(cancellationToken);

            return(await Result <int> .SuccessAsync(document.Id, _localizer["Document Deleted"]));
        }
Exemplo n.º 2
0
        public async Task <Result <int> > Handle(DeleteDocumentCommand command, CancellationToken cancellationToken)
        {
            var documentsWithExtendedAttributes = _unitOfWork.Repository <Document>().Entities.Include(x => x.ExtendedAttributes);

            var document = await _unitOfWork.Repository <Document>().GetByIdAsync(command.Id);

            if (document != null)
            {
                await _unitOfWork.Repository <Document>().DeleteAsync(document);

                // delete all caches related with deleted entity
                var cacheKeys = await documentsWithExtendedAttributes.SelectMany(x => x.ExtendedAttributes).Where(x => x.EntityId == command.Id).Distinct().Select(x => ApplicationConstants.Cache.GetAllEntityExtendedAttributesByEntityIdCacheKey(nameof(Document), x.EntityId))
                                .ToListAsync(cancellationToken);

                cacheKeys.Add(ApplicationConstants.Cache.GetAllEntityExtendedAttributesCacheKey(nameof(Document)));
                await _unitOfWork.CommitAndRemoveCache(cancellationToken, cacheKeys.ToArray());

                return(await Result <int> .SuccessAsync(document.Id, _localizer["Document Deleted"]));
            }
            else
            {
                return(await Result <int> .FailAsync(_localizer["Document Not Found!"]));
            }
        }