public string DeleteHoardingMaster(string ID) { string result = string.Empty; try { int HoardingID = Convert.ToInt32(ID); //Cascade check in hoarding history table var IsUsedInHoarding = Global.dc.HoardingHistories.FirstOrDefault(i => i.IsActive == true && (!i.IsDeleted.HasValue || !i.IsDeleted.Value) && i.HoardingMasterID == HoardingID); if (IsUsedInHoarding != null) { return("4"); } else { //extra condition //&& i.IsActive == true && (!i.IsDeleted.Value || !i.IsDeleted.HasValue) HoardingMaster hoarding = Global.dc.HoardingMasters.FirstOrDefault(i => i.ID == HoardingID); if (hoarding != null) { hoarding.IsDeleted = true; int count = Global.dc.SaveChanges(); result = Convert.ToString(count); } else { result = "3"; } } } catch (Exception) { result = "-1"; } return(result); }
public string AddHoardingMaster(string CityID, string AreaID, string SubAreaID, string HoardingNo, string HoardingMasterID, string HoardingSize, string Operation) { string result = string.Empty; int count = 0; HoardingMaster hoardingMaster = new HoardingMaster(); try { int?subAreaID; if (!string.IsNullOrEmpty(SubAreaID)) { subAreaID = Convert.ToInt32(SubAreaID); } else { subAreaID = null; } if (Operation.ToLower() == "add") { var hoarding = Global.dc.IsHoardingExsist(subAreaID, (int?)Convert.ToInt32(HoardingNo), (int?)Convert.ToInt32(AreaID)).ToList(); if (hoarding.Count > 0) { result = "2"; } else { hoardingMaster.CityID = Convert.ToInt32(CityID); hoardingMaster.AreaID = Convert.ToInt32(AreaID); hoardingMaster.HoardingSize = HoardingSize; if (!string.IsNullOrEmpty(SubAreaID)) { hoardingMaster.SubAreaID = subAreaID; } hoardingMaster.HoardingNo = (int?)Convert.ToInt32(HoardingNo.Trim()); hoardingMaster.IsActive = true; hoardingMaster.IsDeleted = false; Global.dc.HoardingMasters.Add(hoardingMaster); count = Global.dc.SaveChanges(); result = count.ToString(); } } else if (Operation.ToLower() == "update") { var hoarding = Global.dc.IsHoardingExsist(subAreaID, (int?)Convert.ToInt32(HoardingNo), (int?)Convert.ToInt32(AreaID)).ToList(); if (hoarding.Count > 0 && hoarding.FirstOrDefault().HoardingSize != null) { //HoardingSize field is added at the end so while updating we need to always check for already assign hoarding int ID = Convert.ToInt32(HoardingMasterID); hoardingMaster = Global.dc.HoardingMasters.FirstOrDefault(i => i.ID == ID); if (hoardingMaster != null) { hoardingMaster.HoardingSize = HoardingSize; count = Global.dc.SaveChanges(); result = Convert.ToString(count); } else { result = "2"; } } else { int ID = Convert.ToInt32(HoardingMasterID); hoardingMaster = Global.dc.HoardingMasters.FirstOrDefault(i => i.ID == ID); if (hoardingMaster != null) { hoardingMaster.CityID = Convert.ToInt32(CityID); hoardingMaster.AreaID = Convert.ToInt32(AreaID); hoardingMaster.SubAreaID = subAreaID; hoardingMaster.HoardingSize = HoardingSize; hoardingMaster.HoardingNo = (int?)Convert.ToInt32(HoardingNo.Trim()); hoardingMaster.IsActive = true; hoardingMaster.IsDeleted = false; count = Global.dc.SaveChanges(); result = count.ToString(); } else { //Record not found result = "3"; } } } } catch (Exception) { result = "-1"; } return(result); }