コード例 #1
0
        public ShoppingListResult CreateShoppingList(AuthIdentity identity, ShoppingList list)
        {
            using (var session = GetSession())
            {
                var ret = new ShoppingListResult();

                using (var transaction = session.BeginTransaction())
                {
                    var dbList = new Models.ShoppingLists();
                    dbList.Title  = list.Title.Trim();
                    dbList.UserId = identity.UserId;
                    session.Save(dbList);

                    if (list.Any()) // Create ShoppingListItems
                    {
                        list.ToList().ForEach(i =>
                        {
                            var dbItem          = ShoppingListItems.FromShoppingListItem(i);
                            dbItem.ShoppingList = dbList;
                            dbItem.UserId       = dbList.UserId;
                            session.Save(dbItem);
                        });
                    }

                    transaction.Commit();

                    ret.NewShoppingListId = dbList.ShoppingListId;
                }

                ret.List = list;
                return(ret);
            }
        }
コード例 #2
0
ファイル: DatabaseAdapter.cs プロジェクト: relimited/core
        public ShoppingListResult CreateShoppingList(AuthIdentity identity, ShoppingList list)
        {
            using (var session = GetSession())
             {
            var ret = new ShoppingListResult();

            using (var transaction = session.BeginTransaction())
            {
               var dbList = new Models.ShoppingLists();
               dbList.Title = list.Title.Trim();
               dbList.UserId = identity.UserId;
               session.Save(dbList);

               if (list.Any()) // Create ShoppingListItems
               {
                  list.ToList().ForEach(i =>
                  {
                     var dbItem = ShoppingListItems.FromShoppingListItem(i);
                     dbItem.ShoppingList = dbList;
                     dbItem.UserId = dbList.UserId;
                     session.Save(dbItem);
                  });
               }

               transaction.Commit();

               ret.NewShoppingListId = dbList.ShoppingListId;
            }

            ret.List = list;
            return ret;
             }
        }
        public ShoppingListResult CreateShoppingList(AuthorIdentity identity, ShoppingList list)
        {
            using (var session = this.GetSession())
            {
                var shopingList = new ShoppingListResult();

                using (var transaction = session.BeginTransaction())
                {
                    var databaseList = new ShoppingLists();
                    databaseList.Title = list.Title.Trim();
                    databaseList.UserId = identity.UserId;
                    session.Save(databaseList);

                    CreateShopingList(list, databaseList, session);

                    transaction.Commit();

                    shopingList.NewShoppingListId = databaseList.ShoppingListId;
                }

                shopingList.List = list;
                return shopingList;
            }
        }
 private static void CreateShopingList(ShoppingList list, ShoppingLists databaseList, ISession session)
 {
     if (list.Any())
     {
         // Create ShoppingListItems
         list.ToList().ForEach(
             i =>
                 {
                     var dbItem = ShoppingListItems.FromShoppingListItem(i);
                     dbItem.ShoppingList = databaseList;
                     dbItem.UserId = databaseList.UserId;
                     session.Save(dbItem);
                 });
     }
 }
        private static ShoppingListResult AddShopingListItems(
            AuthorIdentity identity, 
            IShoppingListSource[] toAdd, 
            IList<ShoppingListItems> databaseItems, 
            ShoppingLists databaseList, 
            ISession session, 
            ITransaction transaction)
        {
            toAdd.ForEach(
                item =>
                    {
                        var source = item.GetItem();

                        if (source.Ingredient == null && !string.IsNullOrWhiteSpace(source.Raw))
                        {
                            // Raw shopping list item
                            if (!databaseItems.Any(i => source.Raw.Equals(i.Raw, StringComparison.OrdinalIgnoreCase)))
                            {
                                // Add it
                                var newItem = new ShoppingListItems
                                                  {
                                                      ShoppingList = databaseList,
                                                      UserId = identity.UserId,
                                                      Raw = source.Raw
                                                  };

                                session.Save(newItem);
                                databaseItems.Add(newItem);
                            }

                            return;
                        }

                        if (source.Ingredient != null && source.Amount == null)
                        {
                            // Raw ingredient without any amount
                            var existingItem =
                                databaseItems.FirstOrDefault(
                                    i => i.Ingredient != null && i.Ingredient.IngredientId == source.Ingredient.Id);

                            if (existingItem == null)
                            {
                                // Add it
                                var newItem = new ShoppingListItems
                                                  {
                                                      ShoppingList = databaseList,
                                                      UserId = identity.UserId,
                                                      Ingredient =
                                                          Ingredients.FromId(source.Ingredient.Id)
                                                  };

                                session.Save(newItem);
                                databaseItems.Add(newItem);
                            }
                            else
                            {
                                // Clear out existing amount
                                existingItem.Amount = null;
                            }
                        }

                        if (source.Ingredient != null && source.Amount != null)
                        {
                            // Ingredient with amount, aggregate if necessary
                            var existingItem =
                                databaseItems.FirstOrDefault(
                                    i => i.Ingredient != null && i.Ingredient.IngredientId == source.Ingredient.Id);

                            if (existingItem == null)
                            {
                                // Add it
                                var newItem = new ShoppingListItems
                                                  {
                                                      ShoppingList = databaseList,
                                                      UserId = identity.UserId,
                                                      Ingredient =
                                                          Ingredients.FromId(source.Ingredient.Id),
                                                      Amount = source.Amount
                                                  };

                                session.Save(newItem);
                                databaseItems.Add(newItem);
                            }
                            else if (existingItem.Amount != null)
                            {
                                // Add to total
                                existingItem.Amount += source.Amount;
                            }
                        }
                    });

            transaction.Commit();

            return new ShoppingListResult
                       {
                           List =
                               new ShoppingList(
                               databaseList != null ? (Guid?)databaseList.ShoppingListId : null,
                               databaseList != null ? databaseList.Title : null,
                               databaseItems.Select(i => i.AsShoppingListItem()))
                       };
        }
コード例 #6
0
        public ShoppingListResult UpdateShoppingList(AuthIdentity identity, Guid?listId, Guid[] toRemove, ShoppingListModification[] toModify, IShoppingListSource[] toAdd, string newName = null)
        {
            using (var session = GetSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    // Deletes
                    if (toRemove.Any())
                    {
                        var dbDeletes = session.QueryOver <ShoppingListItems>()
                                        .Where(p => p.UserId == identity.UserId)
                                        .Where(listId.HasValue
                        ? Expression.Eq("ShoppingList", listId.Value)
                        : Expression.IsNull("ShoppingList")
                                               ).AndRestrictionOn(p => p.ItemId).IsInG(toRemove)
                                        .List();

                        dbDeletes.ForEach(session.Delete);
                    }

                    // Updates
                    Models.ShoppingLists      dbList  = null;
                    IList <ShoppingListItems> dbItems = null;
                    if (listId.HasValue)
                    {
                        dbList = session.QueryOver <Models.ShoppingLists>()
                                 .Fetch(prop => prop.Items).Eager
                                 .Where(p => p.UserId == identity.UserId)
                                 .Where(p => p.ShoppingListId == listId.Value)
                                 .SingleOrDefault();

                        if (dbList == null)
                        {
                            throw new ShoppingListNotFoundException();
                        }

                        if (!String.IsNullOrWhiteSpace(newName))
                        {
                            dbList.Title = newName;
                        }

                        dbItems = dbList.Items;
                    }
                    else
                    {
                        dbItems = session.QueryOver <ShoppingListItems>()
                                  .Where(p => p.UserId == identity.UserId)
                                  .Where(p => p.ShoppingList == null)
                                  .List();
                    }

                    toModify.ForEach(item =>
                    {
                        var dbItem = dbItems.FirstOrDefault(i => i.ItemId == item.ModifiedItemId);
                        if (dbItem == null)
                        {
                            return;
                        }

                        if (item.CrossOut.HasValue)
                        {
                            dbItem.CrossedOut = item.CrossOut.Value;
                        }
                        if (item.NewAmount != null)
                        {
                            dbItem.Amount = item.NewAmount;
                        }
                    });

                    toAdd.ForEach(item =>
                    {
                        var source = item.GetItem();

                        if (source.Ingredient == null && !String.IsNullOrWhiteSpace(source.Raw))                 // Raw shopping list item
                        {
                            if (!dbItems.Any(i => source.Raw.Equals(i.Raw, StringComparison.OrdinalIgnoreCase))) // Add it
                            {
                                var newItem = new ShoppingListItems
                                {
                                    ShoppingList = dbList,
                                    UserId       = identity.UserId,
                                    Raw          = source.Raw
                                };

                                session.Save(newItem);
                                dbItems.Add(newItem);
                            }

                            return;
                        }

                        if (source.Ingredient != null && source.Amount == null) // Raw ingredient without any amount
                        {
                            var existingItem = dbItems.FirstOrDefault(i => i.Ingredient != null && i.Ingredient.IngredientId == source.Ingredient.Id);

                            if (existingItem == null) // Add it
                            {
                                var newItem = new ShoppingListItems
                                {
                                    ShoppingList = dbList,
                                    UserId       = identity.UserId,
                                    Ingredient   = Models.Ingredients.FromId(source.Ingredient.Id)
                                };

                                session.Save(newItem);
                                dbItems.Add(newItem);
                            }
                            else // Clear out existing amount
                            {
                                existingItem.Amount = null;
                            }
                        }

                        if (source.Ingredient != null && source.Amount != null) // Ingredient with amount, aggregate if necessary
                        {
                            var existingItem = dbItems.FirstOrDefault(i => i.Ingredient != null && i.Ingredient.IngredientId == source.Ingredient.Id);

                            if (existingItem == null) // Add it
                            {
                                var newItem = new ShoppingListItems
                                {
                                    ShoppingList = dbList,
                                    UserId       = identity.UserId,
                                    Ingredient   = Models.Ingredients.FromId(source.Ingredient.Id),
                                    Amount       = source.Amount
                                };

                                session.Save(newItem);
                                dbItems.Add(newItem);
                            }
                            else if (existingItem.Amount != null) // Add to total
                            {
                                existingItem.Amount += source.Amount;
                            }
                        }
                    });

                    transaction.Commit();

                    return(new ShoppingListResult
                    {
                        List = new ShoppingList(
                            dbList != null ? (Guid?)dbList.ShoppingListId : null,
                            dbList != null ? dbList.Title : null,
                            dbItems.Select(i => i.AsShoppingListItem()))
                    });
                }
            }
        }