public static async Task <IList <OldImage> > GetImageURLs(ProductDBModel product) { int size = product?.ImageFilePaths?.Count ?? 0; IList <OldImage> ret = new List <OldImage>(size); for (int i = 0; i < size; i++) { FilePath filePath = product.ImageFilePaths?.ElementAt(i); string fileName = filePath?.FileName; if (!String.IsNullOrEmpty(fileName)) { try { //ret[i] = (await GlobalCache.GetImageBlob(fileName)).Uri.AbsoluteUri; string URL = (await GlobalCache.GetImageBlob(fileName)).Uri.AbsoluteUri; ret.Add(new OldImage() { URL = URL, FPid = filePath.FilePathId }); } catch { //If we can't get the image, just return null. NBD ret.Add(new OldImage("")); } } } return(ret); }
/// <summary> /// Has the side effects of uploading images that shoud be uploaded /// </summary> /// <returns></returns> public async Task <ProductDBModel> ToProductDB(ProductDBModel oldProduct = null) { ProductDBModel ret = Mapper.Map <ProductViewModel, ProductDBModel>(this); //now to map the IFormFiles to ImageFilePaths ret.ImageFilePaths = new List <FilePath>(); if (this.ImageFiles != null) { foreach (IFormFile file in this.ImageFiles) { FilePath uploadedImage = new FilePath() { FileName = await UploadImageWrapper(file) }; ret.ImageFilePaths.Add(uploadedImage); } } if (oldProduct?.ImageFilePaths != null) { foreach (OldImage oi in this.OldFileURLs) { if (!oi.ShouldBeDeleted) { FilePath fp = oldProduct.ImageFilePaths.FirstOrDefault(x => x.FilePathId == oi.FPid); ret.ImageFilePaths.Add(fp); } } } return(ret); }
public static async Task <ProductViewModel> FromDBProduct(ProductDBModel product) { ProductViewModel ret = Mapper.Map <ProductDBModel, ProductViewModel>(product); ret.OldFileURLs = await GetImageURLs(product); return(ret); }