コード例 #1
0
        //hostingEnvironmentPath - physical path to WebAPI folder
        //requestUriLeftPart - URL
        public async Task AddPhotosToExistingLotAsync(int lotId, LotPhoto[] lotPhotos, string hostingEnvironmentPath, string requestUriLeftPart)
        {
            Lot lot = LotOperationsHandler.GetLot(lotId);

            lot.LotPhotos = (await GetLotPhotosAsync(lotId)).ToList();
            if (lot.LotPhotos.Count() + lotPhotos.Count() > 10)
            {
                throw new WrongModelException("Maximum 10 photos per lot, currently this lot has " + lot.LotPhotos.Count());
            }
            string currentDirectory = hostingEnvironmentPath + "\\Photos\\" + lot.SellerUserId;

            if (!Directory.Exists(currentDirectory))
            {
                Directory.CreateDirectory(currentDirectory);
            }
            for (int i = 0; i < lotPhotos.Length; i++)
            {
                byte[] photoBytes = Convert.FromBase64String(lotPhotos[i].Path);
                string photoPath  = currentDirectory + "\\" + lot.Name + "_" + DateTime.Now.ToFileTime() + ".jpeg";
                File.WriteAllBytes(photoPath, photoBytes);
                lotPhotos[i].Path = photoPath.Replace(hostingEnvironmentPath, requestUriLeftPart);
                lot.AddPhoto(lotPhotos[i]);
                lotPhotos[i].LotId = lot.Id;
                UoW.LotPhotos.Add(mapper.Map <LotPhotoEntity>(lotPhotos[i]));
            }
            await UoW.SaveChangesAsync();

            await lotOperationsHandler.ChangeLotUnsafeAsync(lot);
        }