public void AddCountyShare(CountyShareModel model)
 {
     try
     {
         model.Share = model.Share / 100;
         ProjectRepository.AddCountyShare(model);
     }
     catch (Exception ex)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed) { ReasonPhrase = ex.Message });
     }
 }
 public void RemoveCountyShare(CountyShareModel model)
 {
     try
     {
         ProjectRepository.DropCountyShare(model.ProjectId.Value, model.CountyId.Value);
     }
     catch (Exception ex)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed) { ReasonPhrase = ex.Message });
     }
 }
        /// <summary>
        /// Get the county shares for a project
        /// </summary>
        /// <param name="versionId"></param>
        /// <returns></returns>
        public IList<CountyShareModel> GetProjectCountyShares(int versionId)
        {
            IList<CountyShareModel> result = new List<CountyShareModel>();
            using (SqlCommand command = new SqlCommand("[TIP].[GetProjectCountyShares]") { CommandType = CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@PROJECTVERSIONID", versionId);

                using (IDataReader rdr = ExecuteReader(command))
                {
                    while (rdr.Read())
                    {
                        CountyShareModel model = new CountyShareModel();
                        model.CountyId = rdr["CountyGeographyId"] != DBNull.Value ? (int?)rdr["CountyGeographyId"] : null;
                        model.CountyName = rdr["CountyName"].ToString();
                        model.Primary = rdr["Primary"] != DBNull.Value ? (bool?)rdr["Primary"] : null;
                        model.Share = rdr["Share"] != DBNull.Value ? (double?)Convert.ToDouble(rdr["Share"]) : null;
                        model.ProjectId = rdr["ProjectId"] != DBNull.Value ? (int?)rdr["ProjectId"] : null;
                        result.Add(model);
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// Create a new county share record (ProjectCountyGeography table)
        /// </summary>
        /// <param name="model"></param>
        public void AddCountyShare(CountyShareModel model)
        {
            ////[TIP].[AddProjectCountyGeography]
            using (SqlCommand command = new SqlCommand("[TIP].[AddProjectCountyGeography]") { CommandType = CommandType.StoredProcedure })
            {
                //model.Adoption != null ? (object)model.Adoption.Value : (object)DBNull.Value
                command.Parameters.AddWithValue("@PROJECTID", model.ProjectId);
                command.Parameters.AddWithValue("@COUNTYID", model.CountyId);
                command.Parameters.AddWithValue("@SHARE", model.Share);
                command.Parameters.AddWithValue("@PRIMARY", model.Primary);

                this.ExecuteNonQuery(command);
            }
        }
        /// <summary>
        /// Parse the County Shares from the Form Parameter collection 
        /// </summary>
        /// <param name="formParams"></param>
        /// <returns></returns>
        public Dictionary<int, CountyShareModel> ExtractCountyShares(NameValueCollection formParams)
        {
            Dictionary<int, CountyShareModel> countyShares = new Dictionary<int, CountyShareModel>();
            //Get the projectId, as we need that in order to persist the share
            int projectId = Convert.ToInt32(formParams["ProjectId"]);

            foreach (string key in formParams)
            {
                if (key.StartsWith("cty_"))
                {
                    //Parse into Dict keyed by the CountyId
                    int ctyId = Convert.ToInt32(key.Split('_')[1]);

                    CountyShareModel ctyItem = null;
                    if (countyShares.ContainsKey(ctyId))
                    {
                        ctyItem = countyShares[ctyId];
                    }
                    else
                    {
                        ctyItem = new CountyShareModel();
                        ctyItem.CountyId = ctyId;
                        ctyItem.ProjectId = projectId;
                        countyShares.Add(ctyId, ctyItem);
                    }

                    //See if we got Primary or share
                    if (key.EndsWith("_IsPrimary"))
                    {
                        //Html checkboxes are only included in form posts
                        //if they are checked. So there is no real need
                        //for this check and there is no notification of
                        //a "false" value.
                        if (formParams[key] == "on")
                        {
                            ctyItem.Primary = true;
                        }
                    }
                    if (key.EndsWith("_share"))
                    {
                        //stored as 0 to 1 range not a percent
                        //so we divide by 100
                        ctyItem.Share = Convert.ToDouble(formParams[key]) / 100;
                    }
                }
            }
            return countyShares;
        }
 public JsonResult AddCountyShare(int projectId, int countyId, double share, bool isPrimary)
 {
     CountyShareModel model = new CountyShareModel();
     model.ProjectId = projectId;
     model.CountyId = countyId;
     model.Primary = isPrimary;
     model.Share = share / 100;
     try
     {
         _projectRepository.AddCountyShare(model);
     }
     catch (Exception ex)
     {
         //this.Logger.LogMethodError("ProjectController", "AddCountyShare", Request.Form.ToString(), ex);
         return Json(new { error = "Changes could not be stored. An error has been logged." });
     }
     return Json(new { message = "County successfully added." });
 }