public ManageCompanyResponse deleteCompany(ManageCompanyRequest request) { ManageCompanyResponse response = new ManageCompanyResponse(); //creating a new instance is not required //Company_table company = new Company_table(); //calling the constructor would suffice Company_table company = default(Company_table); using (var context = new Company_dbEntities()) { int?compId = AdapterHelper.GetCompanyId(request.CompanyName); if (compId == null) { throw new Exception("No company found. Try again!"); } company = (from a in context.Company_table where a.CompanyID == compId select a).FirstOrDefault(); if (company != null) { context.Company_table.Remove(company); response.IsCompanyUpdated = true; } context.SaveChanges(); } return(response); }
public ManageCompanyResponse AddCompany(ManageCompanyRequest request) { ManageCompanyResponse response = new ManageCompanyResponse(); using (var context = new Company_dbEntities()) { int?compId = AdapterHelper.GetCompanyId(request.CompanyName); if (compId != null) { throw new Exception("Company Name already Exists. Try another name."); } var comp = (from a in context.Company_table where a.Prefix == request.Prefix select a).FirstOrDefault(); if (comp == null) { comp = new Company_table(); comp.CompanyName = request.CompanyName; comp.OwnerName = request.OwnerName; comp.Address = request.Address; comp.Contact = request.Contact; comp.EmployeeCount = 0; comp.ResourceCount = 0; comp.Email = request.Email; comp.Prefix = request.Prefix; comp.ModifiedOn = DateTime.Now; comp.IsActive = true; context.Company_table.Add(comp); context.SaveChanges(); response.IsCompanyCreated = true; response.CompanyId = comp.CompanyID; } else { response.IsCompanyCreated = false; } } return(response); }