示例#1
0
        public HttpStatusCode AddItemToList(int listId, ShoppingListItem item)
        {
            if (ListQueries.AddItemToList(listId, item))
            {
                return HttpStatusCode.OK;
            }

            return HttpStatusCode.InternalServerError;
        }
示例#2
0
            public void TestBlankLAList()
            {
                ShoppingList testList3 = new ShoppingList() { Name = "Test List 3" };
                ShoppingListItem testInsertItem = new ShoppingListItem() { Description = "Test Insert", Checked = false };

                Assert.IsNotNull(testInsertItem, "Test insert item is null.");

                testList3.ShoppingListItems.Add(testInsertItem);
                Assert.AreEqual<int>(1, testList3.ShoppingListItems.Count, "List item not added.");
                testList3.ShoppingListItems.Remove(testInsertItem);
                Assert.AreEqual<int>(0, testList3.ShoppingListItems.Count, "List item not removed.");
            }
示例#3
0
        public static bool AddItemToList(int listId, ShoppingListItem item)
        {
            var success = false;

            Mapper.CreateMap<ShoppingListItem, LAListItem>()
                .ForMember(e => e.Done, opt => opt.MapFrom(s => s.Checked));
            Mapper.CreateMap<ShoppingList, LAList>()
                .ForMember(e => e.LAListItems, opt => opt.MapFrom(s => s.ShoppingListItems));

            var shoppingList = GetList(listId);

            if (shoppingList != null)
            {
                shoppingList.ShoppingListItems.Add(item);
                
                db.LALists.Add(Mapper.Map<ShoppingList, LAList>(shoppingList));
                db.SaveChanges();

                success = true;
            }

            return success;
        }
示例#4
0
            public void TestAddItemToNoList()
            {
                var newItem = new ShoppingListItem() { ListId = -1, Description = "Test Insert 0", Checked = false };
                var itemCount = db.LAListItems.ToList().Count;
                var result = listQueries.AddItemToList(newItem);

                Assert.AreEqual(false, result);
                Assert.AreEqual(itemCount, db.LAListItems.ToList().Count);
            }
示例#5
0
            public void TestAddDuplicateItem()
            {
                var newItem = new ShoppingListItem()
                {
                    ListId = 3,
                    Description = "Test Insert 1",
                    Checked = false
                };

                var result = listQueries.AddItemToList(newItem);

                Assert.AreEqual(true, result);
                Assert.IsNotNull(db.LAListItems.Where(e => e.Description.Equals("Test Insert 1")).FirstOrDefault());
                Assert.AreEqual(2, db.LALists.Find(3).LAListItems.Count);

                var item = db.LAListItems.Where(e => e.Description.Equals("Test Insert 1")).FirstOrDefault();

                Assert.IsNotNull(item);
            }
示例#6
0
        public HttpStatusCode UpdateList()
        {
            JObject jsonObj = null;
            ShoppingList newList = null;
            ShoppingListItem newItem = null;

            jsonObj = JObject.Parse(Request.Content.ReadAsStringAsync().Result);
            newList = new ShoppingList();
            newList.Id = (int)jsonObj["ID"];
            newList.Name = jsonObj["Name"].ToString();

            foreach (var jsonItem in jsonObj["LAListItems"].Children())
            {
                newItem = new ShoppingListItem();
                newItem.Id = (int)jsonItem["ID"];
                newItem.ListId = (int)jsonItem["ListID"];
                newItem.Description = jsonItem["Description"].ToString();
                newItem.Checked = (bool)jsonItem["Done"];

                newList.ShoppingListItems.Add(newItem);
            }

            var result = listQueries.UpdateList(newList.Id, newList.Name);

            if(result)
            {
                foreach (var item in newList.ShoppingListItems)
                {
                    if (!listQueries.UpdateItemFromList(item))
                    {
                        return HttpStatusCode.InternalServerError;
                    }
                }

                return HttpStatusCode.OK;
            }

            return HttpStatusCode.InternalServerError;
        }
示例#7
0
        public bool UpdateItemFromList(ShoppingListItem item)
        {
            Mapper.CreateMap<ShoppingListItem, LAListItem>()
                .ForMember(e => e.Done, opt => opt.MapFrom(s => s.Checked));

            var success = false;

            if (item != null)
            {
                var shoppingListItem = db.LAListItems.Find(item.Id);

                if (shoppingListItem != null)
                {
                    shoppingListItem.Description = item.Description;
                    shoppingListItem.Done = item.Checked;
                    db.Entry(shoppingListItem).State = EntityState.Modified;
                    db.SaveChanges();
                    success = true;
                }
            }

            return success;
        }
示例#8
0
        public bool AddItemToList(ShoppingListItem item)
        {
            Mapper.CreateMap<ShoppingListItem, LAListItem>()
                .ForMember(e => e.Done, opt => opt.MapFrom(s => s.Checked));

            var success = false;
            
            if (item != null)
            {
                var shoppingList = db.LALists.Find(item.ListId);

                if (shoppingList != null)
                {
                    var duplicate = db.LAListItems.Where(e => (e.ListID == item.ListId) && (e.Description.Equals(item.Description))).FirstOrDefault();

                    if (duplicate == null)
                    {
                        shoppingList.LAListItems.Add(Mapper.Map<ShoppingListItem, LAListItem>(item));
                        db.Entry(shoppingList).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        duplicate.Done = false;
                        db.Entry(duplicate).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    success = true;
                }
            }

            return success;
        }