private ShoppingListViewModel GenerateViewModel(ShoppingList shoppingList) { var approvedMenus = _unitOfWork.WeeklyMenus.Query(_ => _.Approved).OrderBy(_ => _.PublishDate).ToList(); return new ShoppingListViewModel { Id = shoppingList.Id, Comment = shoppingList.Comment, FirstWeek = approvedMenus.First().WeekNumber, LastWeek = approvedMenus.Last().WeekNumber, CurrentWeek = shoppingList.WeekNumber, Recipes = _iRecipeService.GenerateSearchRecipeViewModels(shoppingList.Recipes), Items = shoppingList.ShoppingListItems.Select( _ => new ShoppingListItemViewModel { Id = _.Id, AddedByUser = _.AddedByUser, Amount = _.Amount, Name = _.Name, Unit = _.Unit }).ToList() }; }
private ShoppingList CreateShoppingList(WeeklyMenu weeklyMenu, Guid? userId) { var listOfamounts = new List<Amount>(); var shoppingList = new ShoppingList { Id = Guid.NewGuid(), UserId = userId, WeeklyMenuId = weeklyMenu.Id, ShoppingListItems = new List<ShoppingListItem>(), Recipes = weeklyMenu.Recipes, WeekNumber = weeklyMenu.WeekNumber }; weeklyMenu.Recipes.ForEach(_=> listOfamounts.AddRange(_.Amounts)); var viewModel = AddToShoppingList(shoppingList, listOfamounts); _unitOfWork.ShoppingLists.Add(shoppingList); _unitOfWork.Commit(); return viewModel; }
private static void AddToShoppingList(ShoppingList shoppingList, IEnumerable<ShoppingListItemViewModel> shoppingListItems) { shoppingList.ShoppingListItems.Clear(); foreach (var item in shoppingListItems) { shoppingList.ShoppingListItems.Add(new ShoppingListItem{AddedByUser = item.AddedByUser, Amount = item.Amount, Id = item.Id, Name = item.Name, Unit = item.Unit}); } }
private static ShoppingList AddToShoppingList(ShoppingList shoppingList, IEnumerable<Amount> amounts) { foreach (var amount in amounts) { var shoppingListItem = shoppingList.ShoppingListItems.FirstOrDefault(_ => _.Name == amount.Ingredient.Name && _.Unit == amount.Unit); if (shoppingListItem == null) { shoppingList.ShoppingListItems.Add(new ShoppingListItem { AddedByUser = false, Amount = amount.AmountOf, Id = Guid.NewGuid(), Name = amount.Ingredient.Name, Unit = amount.Unit }); continue; } shoppingListItem.Amount += amount.AmountOf; } return shoppingList; }
public void DeleteShoppingLists(AuthIdentity identity, ShoppingList[] lists) { if (!lists.Any()) { throw new ArgumentException("DeleteShoppingLists requires at least one list to delete."); } using (var session = this.GetSession()) { using (var transaction = session.BeginTransaction()) { var shoppingListses = session.QueryOver<ShoppingLists>() .AndRestrictionOn(p => p.ShoppingListId).IsInG(lists.Where(l => l.Id.HasValue).Select(l => l.Id.Value)) .Where(p => p.UserId == identity.UserId) .List(); shoppingListses.ForEach(session.Delete); transaction.Commit(); } } }
public ShoppingListResult CreateShoppingList(AuthIdentity identity, ShoppingList list) { using (var session = this.GetSession()) { var ret = new ShoppingListResult(); using (var transaction = session.BeginTransaction()) { var shoppingList = new ShoppingLists(); shoppingList.Title = list.Title.Trim(); shoppingList.UserId = identity.UserId; session.Save(shoppingList); if (list.Any()) { list.ToList().ForEach(i => { var dbItem = ShoppingListItems.FromShoppingListItem(i); dbItem.ShoppingList = shoppingList; dbItem.UserId = shoppingList.UserId; session.Save(dbItem); }); } transaction.Commit(); ret.NewShoppingListId = shoppingList.ShoppingListId; } ret.List = list; return ret; } }