/// <summary> /// Creates a new customer. /// </summary> /// <param name="userName">userName.</param> /// <param name="password">The password.</param> /// <param name="userType">Type of the user.</param> /// <param name="customerId">The customer id.</param> /// <param name="errorMessage">The out errorMessage in case of an error.</param> /// <returns> /// the user if created; null if failed to create /// </returns> public Customer CreateCustomer(string userName, string password, UserTypes userType, string customerId, out string errorMessage) { errorMessage = String.Empty ; Customer customer = new Customer(); customer.UserName = userName; customer.UserType = userType; customer.Password = password; customer.CustomerId = customerId; try { this._db.Customers.Add(customer); this._db.SaveChanges(); } catch (SystemException ex) { customer = null; errorMessage = this.GetDetaildMessage(ex); Logger.Log(String.Format("Failed to create user:{0}" +"{1}", Environment.NewLine, errorMessage)); } return customer; }
/// <summary> /// Gets a sorted shop list from JSON string in expected format /// </summary> /// <param name="json">the json string</param> /// <returns>the shoplist</returns> public static ShopList GetShopList(string json, List<Product> allProducts, Customer customer, IList<Supermarket> markets) { JObject jObject = JObject.Parse(json); JToken jlistData = jObject["listData"]; JToken jmarketName = jObject["supermarket"]; string superName = jmarketName["superName"].ToString().Trim(); Supermarket market = markets.FirstOrDefault(m => m.Name == superName) ?? markets[0]; JToken jlistItems = jlistData["listItems"];//jlistData.First; var AllItems = jlistItems.Where(token => token.HasValues).ToList();//jlistItems.Children()[1];//.FirstOrDefault()["Id"]; int temp; var tuplesStr = AllItems.OfType<JObject>() .Cast<IDictionary<string, JToken>>() //easier to handle as dictionary .Where(i => i.ContainsKey("Id") && i["Id"] !=null && !String.IsNullOrWhiteSpace(i["Id"].ToString())) //make sure ID is available .Select(i => new Tuple<string, string>( i["Id"].ToString() , i.ContainsKey("Quantity") ? i["Quantity"].ToString() : "1"))//get tuple with ID / quantity .Where(t => int.TryParse(t.Item1, out temp) && int.TryParse(t.Item2, out temp))//parse to int .ToList();//list is easier to debug var quantityByProductDic= new Dictionary<Product,int>(); //add products to dictionary tuplesStr.ToList() .ForEach(t => quantityByProductDic.Add(allProducts.FirstOrDefault(p => p.Id == int.Parse(t.Item1)), int.Parse(t.Item2))); ShopList sl = Logics.GetShoppingList(quantityByProductDic, market, customer); return sl; }
/// <summary> /// Creates a new customer. /// </summary> /// <param name="userName">userName.</param> /// <param name="password">The password.</param> /// <param name="userType">Type of the user.</param> /// <param name="customerId">The customer id.</param> /// <param name="errorMessage">The out errorMessage in case of an error.</param> /// <returns> /// the user if created; null if failed to create /// </returns> public Customer CreateCustomer(string userName, string password, UserTypes userType, string customerId, out string errorMessage) { errorMessage = String.Empty ; Customer customer = new Customer(); customer.UserName = userName; customer.UserType = userType; customer.Password = password; customer.CustomerId = customerId; try { this._db.Customers.Add(customer); this._db.SaveChanges(); } catch (SystemException ex) { customer = null; //if we can get more specific details, do it... if (ex is DbEntityValidationException) { List<DbEntityValidationResult> validationErrors = this._db.GetValidationErrors().ToList(); foreach (DbEntityValidationResult currResult in validationErrors) { //get the error message List<DbValidationError> errors= currResult.ValidationErrors.ToList(); List<String> errorString = errors.Select(error => error.ErrorMessage).ToList(); errorMessage += String.Join(Environment.NewLine, errorString); } //errorMessage = String.Join(Environment.NewLine,this._db.GetValidationErrors().ToList()); } else { errorMessage = ex.Message; } Logger.Log(String.Format("Failed to create user:{0}" +"{1}", Environment.NewLine, errorMessage)); } return customer; }
/// <summary> /// Shows a message about who is current user. /// </summary> /// <param name="customer">The customer.</param> /// <param name="message">The message.</param> private void ShowUserChangedMessage(Customer customer, string message) { string msg; if (customer == null) { msg = String.Format("כניסת משתמש רשום נכשלה:{0}{1}", Environment.NewLine, message); } else { msg = String.Format("כניסת משתמש רשום הצליחה עבור {0}", customer.UserName); } this.ShowMessage(msg); }
/// <summary> /// Gets the archived lists of specifrid customer. /// </summary> /// <param name="customer">The customer.</param> /// <returns>the lists saved for customer</returns> public List<ShopList> GetArchivedLists(Customer customer) { List<ShopList> savedLists = this._db.ShopLists.Where(sl => sl.Customer.Id == customer.Id).ToList(); return savedLists; }
/// <summary> /// Saves the shop listto DB. /// </summary> /// <param name="list">The list.</param> /// <param name="customer">The customer.</param> private bool SaveShopList(ShopList list, Customer customer) { list.Customer = customer; list.CustomerId = customer.Id; if (list.Date == DateTime.MinValue) { list.Date = DateTime.Now; } string errorMessage; return this._db.SaveShoplist(list, out errorMessage); }
public ShopList GetSortedList(ShopList list,Customer customer) { this.SortShopList(list); if (customer != null) { this.SaveShopList(list, customer); } return list; }
public ShopList GetShoppingList(Dictionary<Product, int> quantityByProduct, Supermarket market, Customer customer) { ShopList list = new ShopList(); foreach (var pair in quantityByProduct) { var product = pair.Key; var quantity = pair.Value; ShoplistItem item = new ShoplistItem(); item.Product = product; item.ProductId = product.Id; item.Quantity = quantity; item.ShopList = list; list.ShoplistItems.Add(item); } if (market == null) { var markets = this.GetAllSuperMarkets(); market = markets[0]; } list.Supermarket = market; list.SuperMarketId = market.Id; return this.GetSortedList(list,customer); }
/// <summary> /// Gets the archived lists of specifrid customer. /// </summary> /// <param name="customer">The customer.</param> /// <returns>the lists saved for customer</returns> public List<ShopList> GetArchivedLists(Customer customer) { return this._db.GetArchivedLists(customer); }