Beispiel #1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var attachment = await _context.Attachments.SingleOrDefaultAsync(x => x.Id == request.Id);

                if (attachment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Attachment = "Not found" });
                }

                var result = await _uploadAccessor.DeleteFileAsync(attachment.RelativePath, attachment.UploadLocation);

                if (result != "ok")
                {
                    throw new Exception("Problem deleting the file!");
                }

                _context.Attachments.Remove(attachment);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    await _logger.LogActivity(
                        attachment.SiteId,
                        ActivityCodeEnum.AttachmentDelete,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Attachemnt,
                        $"The {attachment.Type} with the name {attachment.FileName} deleted!");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }