public ShareModel(Share location)
 {
     this.ShareId = location.ShareId;
     this.ShareName = location.ShareName;
     this.TotalSpace = location.TotalSpace;
     this.FreeSpace = location.FreeSpace;
     this.NetworkSharePath = location.NetworkSharePath;
 }
 void IShareProvider.DeleteShare(Share location)
 {
     // Call will come here only if the location is valid.
     var existingShare = (from s in locations where s.ShareId == location.ShareId select s).First();
     if (existingShare != null)
     {
         locations.Remove(existingShare);
     }
 }
        void IShareProvider.UpdateShare(Share location)
        {
            var existingShare = (from s in locations where s.ShareId == location.ShareId select s).First();
            existingShare.TotalSpace = location.TotalSpace;
            existingShare.FreeSpace = location.FreeSpace;

            // For now, we only allow updating free and total space.
            // We do not allow the location name or path to be edited, as files will be already written there.
            ////existingShare.ShareName = location.ShareName;
            ////existingShare.NetworkSharePath = location.NetworkSharePath;
        }
 void IShareProvider.CreateShare(Share location)
 {
     CurrentMaxShareId++;
     locations.Add(new Share
     {
         ShareId = CurrentMaxShareId,
         ShareName = location.ShareName,
         TotalSpace = location.TotalSpace,
         FreeSpace = location.TotalSpace,   // When we start, all space is free.
         NetworkSharePath = location.NetworkSharePath
     });
 }
        internal static bool IsShareValid(Share share)
        {
            if (share == null ||
                share.TotalSpace <= 0 ||
                string.IsNullOrWhiteSpace(share.NetworkSharePath) ||
                string.IsNullOrWhiteSpace(share.ShareName))
            {
                return false;
            }

            return true;
        }
        public void DeleteShare(Share location)
        {
            if (location == null || location.ShareId == 0)
            {
                throw Utility.ThrowResponseException(this.Request, System.Net.HttpStatusCode.BadRequest, ErrorMessages.ShareEmpty);
            }

            var locations = DataProviderFactory.ShareInstance.GetShares();
            var existingShare = (from s in locations where s.ShareId == location.ShareId select s).FirstOrDefault();

            if (existingShare == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, ErrorMessages.ShareNotFound, location.ShareName);
                throw Utility.ThrowResponseException(null, System.Net.HttpStatusCode.BadRequest, message);
            }

            DataProviderFactory.ShareInstance.DeleteShare(location);
        }
        public void AddShare(Share location)
        {
            if (location == null)
            {
                throw Utility.ThrowResponseException(this.Request, System.Net.HttpStatusCode.BadRequest, ErrorMessages.ShareEmpty);
            }

            if (!DataValidationUtil.IsShareValid(location))
            {
                string message = string.Format(CultureInfo.CurrentCulture, ErrorMessages.NullInput);
                throw Utility.ThrowResponseException(null, System.Net.HttpStatusCode.BadRequest, message);
            }

            var locations = DataProviderFactory.ShareInstance.GetShares();
            var existingShare = (from s in locations where s.ShareName.ToLower() == location.ShareName.ToLower() select s).FirstOrDefault();
            if (existingShare != null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, ErrorMessages.ShareAlreadyExist, location.ShareName);
                throw Utility.ThrowResponseException(null, System.Net.HttpStatusCode.BadRequest, message);
            };

            if (!DataValidationUtil.IsNetworkShareReachable(location.NetworkSharePath))
            {
                string message = string.Format(CultureInfo.CurrentCulture, ErrorMessages.ShareNotFound, location.NetworkSharePath);
                throw Utility.ThrowResponseException(null, System.Net.HttpStatusCode.BadRequest, message);
            }

            // Trim trailing slash and space from path.
            location.NetworkSharePath = location.NetworkSharePath.TrimEnd(new char[] { ' ', '\\' });
            if (DataValidationUtil.IsNetworkAlreadyMapped(location.NetworkSharePath, locations))
            {
                string message = string.Format(CultureInfo.CurrentCulture, ErrorMessages.NetworkShareAlreadyMapped, location.NetworkSharePath);
                throw Utility.ThrowResponseException(null, System.Net.HttpStatusCode.BadRequest, message);
            }

            DataProviderFactory.ShareInstance.CreateShare(location);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ShareModel" /> class.
 /// </summary>
 /// <param name="ProductModel">The domain name from API.</param>
 public ShareModel(Share locationFromApi)
 {
     this.ShareId = locationFromApi.ShareId;
     this.ShareName = locationFromApi.ShareName;
 }
        public void UpdateShare(Share location)
        {
            if (location == null)
            {
                throw Utility.ThrowResponseException(this.Request, System.Net.HttpStatusCode.BadRequest, ErrorMessages.ShareEmpty);
            }

            // TODO: Fix issue around HTTP POST method.
            if (location.ShareId == 0)
            {
                // Treat this as Add Share
                InMemoryShareProvider.Instance.CreateShare(location);
                return;
            }

            var locations = DataProviderFactory.ShareInstance.GetShares();
            var existingShare = (from s in locations where s.ShareId == location.ShareId select s).FirstOrDefault();

            if (existingShare == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, ErrorMessages.ShareNotFound, location.ShareName);
                throw Utility.ThrowResponseException(null, System.Net.HttpStatusCode.BadRequest, message);
            }

            DataProviderFactory.ShareInstance.UpdateShare(location);
        }
 public async Task<JsonResult> CreateShare(Share share)
 {
     try
     {
         await ClientFactory.StorageSampleClient.AddShareAsync(share);
         return this.JsonDataSet(new object());
     }
     catch (HttpRequestException ex)
     {
         // http://msdn.microsoft.com/en-us/library/dn528486.aspx
         throw new PortalException(ex.Message, ex, HttpStatusCode.BadRequest);
     }
 }