public void Add(ILsoCustomer item) { // create the customer row and update it using the passed in value var customer = new Customer(); /* Note: Code generally relies on Linq to create the associated NewCustomerOption, * but there are two cases that need to be handled * First, many customers exist that do not have a NewCustomerOption yet. That case is handled in Customer.cs * Second, a completely new customer. That case is handled below. */ customer.NewCustomerOptions = new NewCustomerOption(); var actualitem = (LsoCustomer)item; // We need access to the underlying implementation actualitem.CreatedFrom = customer; // This empty customer will get updated in the factory below // set defaults if those fields are not already set new LsoCustomerNewDefaults().SetDefaults(actualitem); // perform copy to LINQ to SQL class LsoCustomerFactory.Update(actualitem); // linq to sql is ready to push to DB, but we need to test that first _Db.Customers.InsertOnSubmit(customer); // check to see if this object will fit into the DB columns try { _Db.TestSubmitChanges(); } catch (InvalidOperationException ex) { // discard the changes _Db.DiscardInsertsAndDeletes(); _Db.SubmitChanges(); // rethrow the exception for the benefit of our caller); throw; } // If we made it to here, we can proceed safely // Get a new customer ID from the DB int?newCustId = 0; _Db.sp_GetCustID(ref newCustId); if (!newCustId.HasValue || newCustId == 0) { // discard the changes _Db.DiscardInsertsAndDeletes(); _Db.SubmitChanges(); throw new CannotCreateNewIdException("LsoCustomerRepository.Add: sp_GetCustID returned null or zero for the next customer ID"); } // populate the customerID now that we have it and are clear to save in the DB item.CustID = newCustId.Value; actualitem.CreatedFrom.CustID = newCustId.Value; // flush DB _Db.SubmitChanges(); }
// Creates Customer account and on success creates a new webship account for it public static AccountsCreationResponse CreateAccounts(AccountsCreationRequest request) { // TODO: Some of the tables updated in webship account creation do not have domain objects // TODO: So they lack code to prevent killing the database insert with bad data // TODO: For now, just scrub the fields in question here request.BillingZip = ToDigitsOnly(request.BillingZip); request.CompanyBillZip = ToDigitsOnly(request.CompanyBillZip); request.CCNumber = ToDigitsOnly(request.CCNumber); request.RequesterPhone = ToDigitsOnly(request.RequesterPhone); request.CompanyPhone = ToDigitsOnly(request.CompanyPhone); request.CompanyPhyZip = ToDigitsOnly(request.CompanyPhyZip); var response = new AccountsCreationResponse(); var custfactory = new LsoCustomerFactory(); var webacctfactory = new WebshipAccountFactory(); var cust = custfactory.Create(request); var webacct = webacctfactory.Create(request); // TODO: make a validator class that looks at all data in these new // domain instances and either run it on them, or weave it into the // property set code in each domain object var customerRepository = RepositoryFactory.GetNewCustomerRepository(); var webshipAccountRepository = RepositoryFactory.GetNewWebshipAccountRepository(); response.Success = true; // TODO: We don't have validation in place, so bad data errors will be lost with this current implementation try { // TODO: With different databases, and the repository pattern we don't get transactions // TODO: We will need to modify this code to catch errors and remove spurious accounts // TODO: Or add a transaction pattern customerRepository.Add(cust); webacct.CustID = cust.CustID; webshipAccountRepository.Add(webacct); response.CustomerId = webacct.CustID; response.LoginName = webacct.LoginName; } catch (Exception ex) { response.Success = false; response.Errors = new ValidationErrors(); response.Errors.ErrorsMessage = new List <string>(); var trace = new System.Diagnostics.StackTrace(ex, true); response.Errors.ErrorsMessage.Add(ex.Message + " " + trace.GetFrame(0).GetMethod().Name + " " + "Line: " + trace.GetFrame(0).GetFileLineNumber() + " " + "Column: " + trace.GetFrame(0).GetFileColumnNumber()); if (ex.InnerException != null) { var trace2 = new System.Diagnostics.StackTrace(ex.InnerException, true); response.Errors.ErrorsMessage.Add(ex.InnerException.Message + " " + trace2.GetFrame(0).GetMethod().Name + " " + "Line: " + trace2.GetFrame(0).GetFileLineNumber() + " " + "Column: " + trace2.GetFrame(0).GetFileColumnNumber()); } } return(response); }
public IList <ILsoCustomer> GetAllWithCustIdZipcode(int custId, string zipcode) { var accounts = from p in _Db.Customers where p.CustID == custId && p.PhyZip == zipcode select p; var converter = new LsoCustomerFactory(); return(accounts.Select(customer => converter.Create(customer)).ToList()); }
public IList <ILsoCustomer> GetAllWithCustId(int custId) { var accounts = from p in _Db.Customers where p.CustID == custId select p; var converter = new LsoCustomerFactory(); return(accounts.Select(customer => converter.Create(customer)).ToList()); }
public IList <ILsoCustomer> GetAllWithCustName(string custName) { // TODO: Names should be normalized to prevent entries with extra spaces from failing the search var accounts = from p in _Db.Customers where p.CustName.ToLower() == custName.ToLower() select p; var converter = new LsoCustomerFactory(); return(accounts.Select(customer => converter.Create(customer)).ToList()); }
public IList <ILsoCustomer> GetAllWithCustContactPhone(string CustContactPhone) { var searchstring = DbUtils.ConvertToSqlLike(CustContactPhone); var accounts = from p in _Db.Customers where System.Data.Linq.SqlClient.SqlMethods.Like(p.PhyAddress1, searchstring) select p; var converter = new LsoCustomerFactory(); return(accounts.Select(customer => converter.Create(customer)).ToList()); }
public IList <ILsoCustomer> GetAllWithDataLike(string custName, string CustContactName, string BillToAddress1, string CustContactPhone, string PhyAddress1) { // TODO: we are using a hard coded take on this right now as a web client // doesn't normally need to see many records. instead we should rework the interface to // work in both use cases // For the moment it makes the most sense if we require all of these to be present because the // primary place where this will be used will require them // later it might be nice to use only one or 2, but that can wait if (!String.IsNullOrEmpty(custName) && !String.IsNullOrEmpty(CustContactName) && !String.IsNullOrEmpty(BillToAddress1) && !String.IsNullOrEmpty(CustContactPhone) && !String.IsNullOrEmpty(PhyAddress1)) { var accounts = (from p in _Db.Customers where System.Data.Linq.SqlClient.SqlMethods.Like(p.CustName, DbUtils.ConvertToSqlLike(custName)) || System.Data.Linq.SqlClient.SqlMethods.Like(p.CustContactName, DbUtils.ConvertToSqlLike(CustContactName)) || System.Data.Linq.SqlClient.SqlMethods.Like(p.BillToAddress1, DbUtils.ConvertToSqlLike(BillToAddress1)) || System.Data.Linq.SqlClient.SqlMethods.Like(p.CustContactPhone, DbUtils.ConvertToSqlLike(CustContactPhone)) || System.Data.Linq.SqlClient.SqlMethods.Like(p.PhyAddress1, DbUtils.ConvertToSqlLike(PhyAddress1)) select p).Take(1000); var converter = new LsoCustomerFactory(); return(accounts.Select(customer => converter.Create(customer)).ToList()); } return(new List <ILsoCustomer>()); }