public override ListModel partialUpdate(int foodId, ListModel data) { using (MyBuyListEntities entities = new MyBuyListEntities()) { UserShoppingList list = entities.UserShoppingLists.FirstOrDefault(item => item.USER_ID == Globals.UserId.Value && item.FOOD_ID == foodId); List <PropertyInfo> listProperties = list.GetType().GetProperties().ToList(); foreach (var dataProp in data.GetType().GetProperties()) { var dataPropValue = dataProp.GetValue(data); if (dataPropValue != null && dataProp.Name != "version") { var listProp = listProperties.Find(item => item.Name == ListMapping[dataProp.Name]); if (listProp != null) { listProp.SetValue(list, dataPropValue); } } } entities.SaveChanges(); return(new ListModel { ownerId = Globals.UserId.Value, itemId = list.FOOD_ID, itemName = list.FOOD_NAME, itemUnit = list.MEASUREMENT_NAME, value = list.QUANTITY.Value }); } }
public override bool Delete(int foodId) { using (MyBuyListEntities entities = new MyBuyListEntities()) { UserShoppingList list = entities.UserShoppingLists.SingleOrDefault(item => item.USER_ID == Globals.UserId && item.FOOD_ID == foodId); if (list != null) { entities.UserShoppingLists.Remove(list); entities.SaveChanges(); return(true); } else { return(false); } }; }
public string Login([FromBody] LoginInfo login) { using (MyBuyListEntities entities = new MyBuyListEntities()) { User user = entities.Users.FirstOrDefault(p => p.Name == login.userName && p.Password == login.password); if (user != null) { Globals.UserId = user.UserId; string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", login.userName, login.password))); user.Token = token; entities.SaveChanges(); return(token); } else { return(null); } } }
public override void Add(ListModel data) { using (MyBuyListEntities entities = new MyBuyListEntities()) { Food food = entities.Foods.Where(item => item.FoodId == data.itemId).SingleOrDefault(); if (food != null) { UserShoppingList _new = new UserShoppingList { FOOD_ID = food.FoodId, FOOD_NAME = food.FoodName, CATEGORY_ID = food.FoodCategoryId, CATEGORY_NAME = food.FoodCategory.FoodCategoryName, QUANTITY = 0, USER_ID = Globals.UserId.Value, ACTIVE = true, CAN_DELETE = true }; entities.UserShoppingLists.Add(_new); entities.SaveChanges(); } } }