public int AddEditJobListing(AngularJobListing selectedJobListing) { string success = "ono"; int newId = 0; try { using (var db = new curtisGodaddyEntities()) { JobListing dbJobListing = null; if (selectedJobListing.Id == 0) { dbJobListing = new JobListing { }; dbJobListing.JobSearchId = db.GetaJob_User.Where(u => u.UserName == User.Identity.Name).First().CurrentJobSearchId; db.JobListings.Add(dbJobListing); } else { dbJobListing = db.JobListings.Where(j => j.JobListingId == selectedJobListing.Id).First(); } dbJobListing.JobTitle = selectedJobListing.JobTitle; dbJobListing.PostedDate = DateTime.Parse(selectedJobListing.PostedDate); dbJobListing.Rate = selectedJobListing.Rate; dbJobListing.Comments = selectedJobListing.Comments; dbJobListing.Distance = selectedJobListing.Distance; dbJobListing.EmploymentType = selectedJobListing.EmploymentType; dbJobListing.ListingStatus = selectedJobListing.ListingStatus; dbJobListing.ListingSource = selectedJobListing.ListingSource; dbJobListing.Desirability = selectedJobListing.Desirability; dbJobListing.Fit = selectedJobListing.Fit; dbJobListing.TargetCompanyId = AddEditTargetCompany(selectedJobListing); int? agencyId = AddEditAgency(selectedJobListing); if (agencyId != null) selectedJobListing.AgencyId = agencyId.Value; dbJobListing.AgentId = AddEditAgent(selectedJobListing); db.SaveChanges(); newId = dbJobListing.JobListingId; } } catch (DbEntityValidationException ex) { success = "ValidationExceptions: "; foreach (var eve in ex.EntityValidationErrors) { success += eve.Entry.Entity.GetType().Name + ": " + eve.Entry.State; foreach (var ve in eve.ValidationErrors) { success += " Property: " + ve.PropertyName + " Error: " + ve.ErrorMessage; } throw new Exception(success); } } catch (Exception ex) { success = ex.Message; if (ex.InnerException != null) success += " inner: " + ex.InnerException.Message; throw new Exception(success); } return newId; }
int? AddEditTargetCompany(AngularJobListing selectedJobListing) { //Unable to update the EntitySet 'TargetCompany' because it has a DefiningQuery and no<InsertFunction> //element exists in the<ModificationFunctionMapping> element to support the current operation. int? targetCompanyId = null; TargetCompany targetCompany = null; if (!string.IsNullOrWhiteSpace(selectedJobListing.TargetCompanyName)) using (var db = new curtisGodaddyEntities()) { if ((selectedJobListing.Id != 0) && selectedJobListing.TargetCompanyId != 0) targetCompany = db.TargetCompanies.Where(tc => tc.CompanyId == selectedJobListing.TargetCompanyId).FirstOrDefault(); if (targetCompany == null) targetCompany = db.TargetCompanies.Where(tc => tc.Name == selectedJobListing.TargetCompanyName).FirstOrDefault(); if (targetCompany == null) { targetCompany = new TargetCompany(); db.TargetCompanies.Add(targetCompany); } targetCompany.Name = selectedJobListing.TargetCompanyName; targetCompany.StreetAddress = selectedJobListing.TargetCompanyStreetAddress; targetCompany.City = selectedJobListing.TargetCompanyCity; targetCompany.State = selectedJobListing.TargetCompanyState; targetCompany.Zip = selectedJobListing.TargetCompanyZip; db.SaveChanges(); targetCompanyId = db.TargetCompanies.Where(c => c.Name == selectedJobListing.TargetCompanyName).First().CompanyId; } return targetCompanyId; }
int? AddEditAgency(AngularJobListing selectedJobListing) { int? agencyId = null; if (!string.IsNullOrWhiteSpace(selectedJobListing.AgencyName)) { using (var db = new curtisGodaddyEntities()) { Agency agency = null; if ((selectedJobListing.Id != 0) && selectedJobListing.AgencyId != 0) { agency = db.Agencies.Where(a => a.AgencyId == selectedJobListing.AgencyId).FirstOrDefault(); } if (agency == null) agency = db.Agencies.Where(a => a.Name == selectedJobListing.AgencyName).FirstOrDefault(); if (agency == null) { agency = new Agency(); db.Agencies.Add(agency); } agency.Name = selectedJobListing.AgencyName; agency.StreetAddress = selectedJobListing.AgencyStreetAddress; agency.City = selectedJobListing.AgencyCity; agency.State = selectedJobListing.AgencyState; agency.Zip = selectedJobListing.AgencyZip; agency.WorkPhone = selectedJobListing.AgencyWorkPhone; agency.CellPhone = selectedJobListing.AgencyCellPhone; agency.Email = selectedJobListing.AgencyEmail; db.SaveChanges(); agencyId = db.Agencies.Where(c => c.Name == selectedJobListing.AgencyName).First().AgencyId; } } return agencyId; }
int? AddEditAgent(AngularJobListing selectedJobListing) { int? agentId = null; using (var db = new curtisGodaddyEntities()) { Agent agent = null; if (selectedJobListing.AgentId != 0) { agent = db.Agents.Where(a => a.AgentId == selectedJobListing.AgentId).FirstOrDefault(); } if (agent == null) { if (!string.IsNullOrWhiteSpace(selectedJobListing.AgentName)) agent = db.Agents.Where(a => a.AgentName == selectedJobListing.AgentName).FirstOrDefault(); } if (agent == null) { agent = new Agent(); db.Agents.Add(agent); } agent.AgentName = selectedJobListing.AgentName; agent.WorkPhone = selectedJobListing.AgentWorkPhone; agent.CellPhone = selectedJobListing.AgentCellPhone; agent.Email = selectedJobListing.AgentEmail; agent.AgencyId = selectedJobListing.AgencyId; db.SaveChanges(); agentId = agent.AgentId; } return agentId; }
public JobListingModel GetJobListings() { string success = "ono"; var jobListingModel = new JobListingModel(); try { using (var db = new curtisGodaddyEntities()) { var currentJobSearchId = db.GetaJob_User.Where(p => p.UserName == User.Identity.Name).FirstOrDefault().CurrentJobSearchId; var dbJobListings = db.JobListings.Where(jl => jl.JobSearchId == currentJobSearchId).ToList(); foreach (JobListing jl in dbJobListings) { var item = new AngularJobListing() { Id = jl.JobListingId, PostedDate = jl.PostedDate == null ? "" : jl.PostedDate.Value.ToShortDateString(), JobTitle = jl.JobTitle, Rate = jl.Rate, ListingStatus = jl.ListingStatus, EmploymentType = jl.EmploymentType, ListingSource = jl.ListingSource, Desirability = jl.Desirability, Fit = jl.Fit, Comments = jl.Comments, Distance = jl.Distance }; jobListingModel.JobListings.Add(item); if (jl.TargetCompanyId != null) { var targetCompany = db.TargetCompanies.Where(tc => tc.CompanyId == jl.TargetCompanyId).First(); item.TargetCompanyName = targetCompany.Name; item.TargetCompanyCity = targetCompany.City; item.TargetCompanyState = targetCompany.State; item.TargetCompanyZip = targetCompany.Zip; } if (jl.AgentId != null) { var agent = db.Agents.Where(a => a.AgentId == jl.AgentId).First(); item.AgentName = agent.AgentName; item.AgentWorkPhone = agent.WorkPhone; item.AgentCellPhone = agent.CellPhone; item.AgentEmail = agent.Email; if (agent.AgencyId != null) { var agency = db.Agencies.Where(a => a.AgencyId == agent.AgencyId).First(); item.AgencyId = agent.AgencyId.Value; item.AgencyName = agency.Name; item.AgencyStreetAddress = agency.StreetAddress; item.AgencyCity = agency.City; item.AgencyState = agency.State; item.AgencyZip = agency.Zip; item.AgencyWorkPhone = agency.WorkPhone; item.AgencyCellPhone = agency.CellPhone; item.AgencyEmail = agency.Email; } } } } jobListingModel.ListingStatuses = GetRefs(2); jobListingModel.ListingSources = GetRefs(3); jobListingModel.Desirabilities = GetRefs(4); jobListingModel.EmploymentTypes = GetRefs(5); jobListingModel.Fits = GetRefs(6); } catch (DbEntityValidationException ex) { success = "ValidationExceptions: "; foreach (var eve in ex.EntityValidationErrors) { success += eve.Entry.Entity.GetType().Name + ": " + eve.Entry.State; foreach (var ve in eve.ValidationErrors) { success += " Property: " + ve.PropertyName + " Error: " + ve.ErrorMessage; } } throw new Exception(success); } catch (Exception ex) { success = ex.Message; if (ex.InnerException != null) success += " inner: " + ex.InnerException.Message; throw new Exception(success); } return jobListingModel; }