Beispiel #1
0
        public async Task <List <TreatmentAttachDTO> > SetNewAttachmentsAsync(List <IFormFile> files, int treatmentId, string username)
        {
            var person = await _dbContext.Persons.FirstOrDefaultAsync(x => x.Mobile == username);

            if (person == null)
            {
                throw new AwroNoreException(NewResource.UserNotFound);
            }

            var treatment = await _dbContext.TreatmentHistories.FindAsync(treatmentId);

            if (treatment == null)
            {
                throw new AwroNoreException(NewResource.TreatmentNotFound);
            }

            if (treatment.Patient.Person.Mobile != username)
            {
                throw new AwroNoreException(NewResource.UserCannotAccessTreatment);
            }

            var mainDirectoryPath = _uploadService.GetTreatmentAttachDirectoryPath(person.Id, treatment.Id);

            #region Process Photos & Generate New Names
            var photosDictionary = new Dictionary <IFormFile, (string newName, string thumbName, string dirPath, string baseUrl)>();
            var newPhotos        = new List <Attachment>();
            if (files != null && files.Any())
            {
                foreach (var photo in files)
                {
                    var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateTreatmentAttachFileName(person.Id, treatment.Id, photo);

                    photosDictionary.Add(photo, (newName, thumbName, dirPath, baseUrl));

                    newPhotos.Add(new Attachment
                    {
                        Name         = newName,
                        CreatedAt    = DateTime.Now,
                        DeleteUrl    = $"{baseUrl}/{newName}",
                        Url          = $"{baseUrl}/{newName}",
                        Order        = 1,
                        ThumbnailUrl = $"{baseUrl}/{thumbName}",
                        Size         = (int)photo.Length,
                        FileType     = FileType.Image,
                        Owner        = FileOwner.TREATMENT_HISTORY,
                        OwnerTableId = treatment.Id
                    });
                }
            }
            #endregion

            var result   = new List <TreatmentAttachDTO>();
            var strategy = _dbContext.Database.CreateExecutionStrategy();
            await strategy.ExecuteAsync(async() =>
            {
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    if (newPhotos.Any())
                    {
                        await _dbContext.Attachments.AddRangeAsync(newPhotos);

                        await _dbContext.SaveChangesAsync();

                        result = newPhotos.Select(x => new TreatmentAttachDTO
                        {
                            CreatedAt    = x.CreatedAt.ToShortDateString(),
                            Description  = x.Description,
                            FileType     = x.FileType,
                            Id           = x.Id,
                            ThumbnailUrl = x.ThumbnailUrl,
                            Url          = x.Url
                        }).ToList();

                        foreach (var photo in photosDictionary)
                        {
                            await _uploadService.UploadTreatmentAttachmentAsync(photo.Key, photo.Value.dirPath, photo.Value.newName, photo.Value.thumbName);
                        }
                    }
                    transaction.Commit();
                }
            });

            return(result);
        }