public async Task <StorePhoneModel> CreateModelsAsync(UpdatePhoneModelRequest createRequest, int phoneId, CloudBlobContainer blobContainer, HttpPostedFile photo)
        {
            var dbModels = await _context.Models.Where(h => h.Code == createRequest.Code).ToArrayAsync();

            if (dbModels.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            StorePhoneModel dbModel = Mapper.Map <UpdatePhoneModelRequest, StorePhoneModel>(createRequest);

            dbModel.StorePhoneId = phoneId;

            var            photoName = GetRandomBlobName(photo.FileName);
            CloudBlockBlob blob      = blobContainer.GetBlockBlobReference(photoName);

            using (var fileStream = photo.InputStream)
            {
                await blob.UploadFromStreamAsync(fileStream);
            }

            dbModel.UrlPhoto = blob.Uri.ToString();

            _context.Models.Add(dbModel);

            await _context.SaveChangesAsync();

            return(Mapper.Map <StorePhoneModel>(dbModel));
        }
        public async Task <StorePhoneModel> UpdatePhoneModelAsync(UpdatePhoneModelRequest updateRequest, CloudBlobContainer blobContainer, HttpPostedFile photo)
        {
            var dbModels = _context.Models.Where(p => p.Id != updateRequest.Id).ToArray();

            if (dbModels.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            dbModels = await _context.Models.Where(p => p.Id == updateRequest.Id).ToArrayAsync();

            if (dbModels.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbModel = dbModels[0];

            string filename = Path.GetFileName(dbModel.UrlPhoto);

            var blobDelete = blobContainer.GetBlockBlobReference(filename);
            await blobDelete.DeleteIfExistsAsync();

            var            photoName = GetRandomBlobName(photo.FileName);
            CloudBlockBlob blob      = blobContainer.GetBlockBlobReference(photoName);

            using (var fileStream = photo.InputStream)
            {
                await blob.UploadFromStreamAsync(fileStream);
            }

            updateRequest.UrlPhoto = blob.Uri.ToString();
            Mapper.Map(updateRequest, dbModel);

            await _context.SaveChangesAsync();

            return(Mapper.Map <StorePhoneModel>(dbModel));
        }