public async Task <IActionResult> CreateNewMedicalRequest(List <IFormFile> files) { if (string.IsNullOrEmpty(CurrentUserName)) { return(Unauthorized()); } var formData = Request.Form["FormData"]; var formModel = JsonConvert.DeserializeObject <MedicalRequestBaseFormDTO>(formData); var model = new MedicalRequestDTO { Date = formModel.Date, Note = formModel.Note, CountryId = formModel.CountryId, Files = files }; _logger.Info($"{model.Files.Count} files uploaded. Country id is {model.CountryId}"); await _medicalRequestService.CreateNewMedicalRequestAsync(CurrentUserName, model); return(Ok()); }
public async Task CreateNewMedicalRequestAsync(string currentUsername, MedicalRequestDTO model) { var person = await _dbContext.Persons.FirstOrDefaultAsync(x => x.Mobile == currentUsername); if (person == null) { throw new AwroNoreException("Not any person found for this mobile number"); } var strategy = _dbContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _dbContext.Database.BeginTransaction()) { var request = new AN.Core.Domain.MedicalRequest { Date = model.Date, Note = model.Note, RequestedCountryId = model.CountryId, RequesterPersonId = person.Id, CreatedAt = DateTime.Now }; await _dbContext.MedicalRequests.AddAsync(request); await _dbContext.SaveChangesAsync(); // Proccess Files if (model.Files.Any()) { // Files base url var basePhotosUrl = _uploadService.GenerateMedicalRequestFilesBaseUrl(request.Id, request.CreatedAt); var photosDictionary = new Dictionary <IFormFile, (string newFileName, string thumbName)>(); // Process Files Logical Names foreach (var file in model.Files) { var(newFileName, thumbFileName) = _uploadService.GenerateMedicalRequestFileName(request.Id, file); photosDictionary.Add(file, (newFileName, thumbFileName)); await _dbContext.Attachments.AddAsync(new Attachment { Name = newFileName, Url = $"{basePhotosUrl}/{newFileName}", ThumbnailUrl = $"{basePhotosUrl}/{thumbFileName}", DeleteUrl = "DeleteUrl", Size = (int)file.Length, Order = model.Files.IndexOf(file), CreatedAt = DateTime.Now, Owner = AN.Core.Enums.FileOwner.MEDICAL_REQUEST, OwnerTableId = request.Id, FileType = AN.Core.Enums.FileType.Image, Description = "" }); } await _dbContext.SaveChangesAsync(); await _uploadService.UploadMedicalRequestFilesAsync(request.Id, request.CreatedAt, photosDictionary); } transaction.Commit(); } }); }