internal static bool UpdateUserInfo(string userLogin, int age, int growth, int weight, short gender) { if (userLogin == null || userLogin.Length.Equals(0)) { return(false); } if (age <= 0 || age > 120 || growth <= 0 || growth > 300 || weight <= 0) { return(false); } AteDatabase entity = new AteDatabase(); try { User user = entity.Users.Single(s => s.Login == userLogin); user.Age = age; user.Growth = growth; user.Weight = weight; user.Gender = gender; entity.SaveChanges(); } catch (Exception) { return(false); } return(true); }
internal static bool TryCreateDish(string userLogin, string Name, List <Component> ComponentsList) { int userId; try { userId = GetUserByUserName(userLogin); } catch (ArgumentException e) { Console.Write("Missing user name" + e); return(false); } catch (Exception e) { Console.Write("User not exist" + e); return(false); } if (userId == 0 || Name.Equals("") || ComponentsList.Count == 0) { return(false); } int TotalDishMass = 0; foreach (Component compo in ComponentsList) { TotalDishMass += (int)compo.TempWeigth; } AteDatabase entity = new AteDatabase(); try { Dish dish = new Dish() { FKUserId = userId, Weigth = TotalDishMass, Name = Name }; foreach (Component com in ComponentsList) { dish.Connectors.Add(new Connector() { FK_ComponentId = com.ComponentId, ComponentWeigth = com.TempWeigth.GetValueOrDefault(), }); } entity.Dishes.Add(dish); entity.SaveChanges(); } catch (Exception) { Console.Write("TryCreateDish Error"); return(false); } return(true); }
internal static bool TryCreateComponent(Component component) { AteDatabase entity = new AteDatabase(); if (!ValidateComponentData(component)) { return(false); } try { entity.Components.Add(component); entity.SaveChanges(); return(true); } catch (Exception) { return(false); } }
internal static bool TryCreateUserAccount(User userAccountCreateData) { AteDatabase entity = new AteDatabase(); if (!ValidateUserData(userAccountCreateData)) { return(false); } try { entity.Users.Add(userAccountCreateData); entity.SaveChanges(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Function create meal from given dishes /// in case of eating single component it is needed to create virtual dish connected by Conncetor list in dish with component, but without adding it to Context /// Dishes should be given without connectors but when they are virtual it should ahve Connector list /// not tested cause UI not prepared YET /// </summary> /// <param name="userId"></param> /// <param name="weigth"></param> /// <param name="mealType"></param> /// <param name="dishesList"></param> /// <returns></returns> internal static bool AddMeal(string userLogin, long weigth, Enums.MealType mealType, List <Dish> dishesList) { int userId; try { userId = GetUserByUserName(userLogin); } catch (ArgumentException e) { Console.Write("Missing user name" + e); return(false); } catch (Exception e) { Console.Write("User not exist" + e); return(false); } if (userId == 0 || weigth == 0 || dishesList.Count == 0) { return(false); } AteDatabase entity = new AteDatabase(); try { Meal meal = new Meal() { FKUserId = userId, Weigth = weigth, MealType = (short)mealType, MealDate = DateTime.Now }; List <Dish> tempDish = new List <Dish>(); foreach (Dish dish in dishesList) { if (dish.Connectors.Count == 0) { tempDish.Add(entity.Dishes.Where(w => w.DishId == dish.DishId).Single()); } else { tempDish.Add(dish); } } foreach (var dish in tempDish) { foreach (Connector con in dish.Connectors) { meal.Connectors.Add(new Connector() { FK_ComponentId = con.FK_ComponentId, ComponentWeigth = con.ComponentWeigth }); } } entity.Meals.Add(meal); entity.SaveChanges(); } catch (Exception e) { Console.Write("AddMeal error" + e); return(false); } return(true); }