public void AddMuniShare(MunicipalityShareModel model)
 {
     try
     {
         model.Share = model.Share / 100;
         ProjectRepository.AddMunicipalityShare(model);
     }
     catch (Exception ex)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed) { ReasonPhrase = ex.Message });
     }
 }
 public void RemoveMuniShare(MunicipalityShareModel model)
 {
     try
     {
         ProjectRepository.DropMunicipalityShare(model.ProjectId.Value, model.MunicipalityId.Value);
     }
     catch (Exception ex)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed) { ReasonPhrase = ex.Message });
     }
 }
 /// <summary>
 /// Adds a Muni Share record (ProjectMuniGeography table)
 /// </summary>
 /// <param name="model"></param>
 public void AddMunicipalityShare(MunicipalityShareModel model)
 {
     //[TIP].[AddProjectMuniGeography]
     using (SqlCommand command = new SqlCommand("[TIP].[AddProjectMuniGeography]") { CommandType = CommandType.StoredProcedure })
     {
         command.Parameters.AddWithValue("@PROJECTID", model.ProjectId);
         command.Parameters.AddWithValue("@MUNIID", model.MunicipalityId);
         command.Parameters.AddWithValue("@SHARE", model.Share);
         command.Parameters.AddWithValue("@PRIMARY", model.Primary);
         this.ExecuteNonQuery(command);
     }
 }
        /// <summary>
        /// Get the municipality shares for a project
        /// </summary>
        /// <param name="versionId"></param>
        /// <returns></returns>
        public IList<MunicipalityShareModel> GetProjectMunicipalityShares(int versionId)
        {
            IList<MunicipalityShareModel> result = new List<MunicipalityShareModel>();
            using (SqlCommand command = new SqlCommand("[TIP].[GetProjectMuniShares]") { CommandType = CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@PROJECTVERSIONID", versionId);

                using (IDataReader rdr = ExecuteReader(command))
                {
                    while (rdr.Read())
                    {
                        MunicipalityShareModel model = new MunicipalityShareModel();
                        model.MunicipalityId = rdr["MuniGeographyId"] != DBNull.Value ? (int?)rdr["MuniGeographyId"] : null;
                        model.MunicipalityName = rdr["MuniName"].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;
        }
 public JsonResult AddMuniShare(int projectId, int muniId, double share, bool isPrimary)
 {
     MunicipalityShareModel model = new MunicipalityShareModel();
     model.ProjectId = projectId;
     model.MunicipalityId = muniId;
     model.Primary = isPrimary;
     model.Share = share / 100;
     try
     {
         _projectRepository.AddMunicipalityShare(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 = "Municipality successfully added." });
 }
        /// <summary>
        /// Parse the Muni Shares from the Form Parameter collection 
        /// </summary>
        /// <param name="formParams"></param>
        /// <returns></returns>
        public Dictionary<int, MunicipalityShareModel> ExtractMuniShares(NameValueCollection formParams)
        {
            Dictionary<int, MunicipalityShareModel> shares = new Dictionary<int, MunicipalityShareModel>();
            //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("muni_"))
                {
                    //Parse into Dict keyed by the CountyId
                    int id = Convert.ToInt32(key.Split('_')[1]);
                    //bool isPrimary = false;
                    //double share = 0;

                    MunicipalityShareModel item = null;
                    if (shares.ContainsKey(id))
                    {
                        item = shares[id];
                    }
                    else
                    {
                        item = new MunicipalityShareModel();
                        item.MunicipalityId = id;
                        item.ProjectId = projectId;
                        shares.Add(id, item);
                    }

                    //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")
                        {
                            item.Primary = true;
                        }
                    }
                    if (key.EndsWith("_share"))
                    {
                        //stored as 0 to 1 range not a percent
                        //so we divide by 100
                        item.Share = Convert.ToDouble(formParams[key]) / 100;
                    }
                }
            }
            return shares;
        }