コード例 #1
0
        public async Task <LunchItem> AddItemInstanceAsync(int lunchId, int productId)
        {
            var lunch = await GetLunchByIdAsync(lunchId);

            var product = await _catalog.GetById(productId);

            var shoppingCartLunchItem = lunch.Items?.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem == null)
            {
                shoppingCartLunchItem = new LunchItem {
                    Lunch = lunch, Product = product, Quantity = 1
                };
                _appDbContext.LunchItems.Add(shoppingCartLunchItem);
            }
            else
            {
                shoppingCartLunchItem.Quantity += 1;
                _appDbContext.LunchItems.Update(shoppingCartLunchItem);
            }


            _appDbContext.SaveChanges();
            return(shoppingCartLunchItem);
        }
コード例 #2
0
        public async Task <LunchItem> RemoveItemInstanceAsync(int lunchId, int productId)
        {
            LunchItem result = null;
            var       lunch  = await GetLunchByIdAsync(lunchId);

            var product = await _catalog.GetById(productId);

            var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem != null)
            {
                if (shoppingCartLunchItem.Quantity > 1)
                {
                    shoppingCartLunchItem.Quantity -= 1;
                    _appDbContext.LunchItems.Update(shoppingCartLunchItem);
                    result = shoppingCartLunchItem;
                }
                else
                {
                    _appDbContext.LunchItems.Remove(shoppingCartLunchItem);
                }
                _appDbContext.SaveChanges();
            }

            return(result);
        }
コード例 #3
0
        public IActionResult Create([FromBody] LunchItem item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            m_context.LunchItems.Add(item);
            m_context.SaveChanges();

            return(CreatedAtRoute("GetLunch", new { ID = item.ID }, item));
        }
コード例 #4
0
        public LunchItem RemoveItem(int lunchId, int productId)
        {
            LunchItem result  = null;
            var       lunch   = GetLunch(lunchId);
            var       product = _appDbContext.Products.First(x => x.ProductId == productId);

            var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem != null)
            {
                _appDbContext.LunchItems.Remove(shoppingCartLunchItem);
                _appDbContext.SaveChanges();
            }
            return(result);
        }
コード例 #5
0
        public async Task <LunchItem> RemoveItemAsync(int lunchId, int productId)
        {
            LunchItem result = null;
            var       lunch  = await GetLunchByIdAsync(lunchId);

            var product = await _catalog.GetById(productId);

            var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem != null)
            {
                _appDbContext.LunchItems.Remove(shoppingCartLunchItem);
                _appDbContext.SaveChanges();
            }

            return(result);
        }
コード例 #6
0
        public LunchItem AddItem(int lunchId, int productId)
        {
            var lunch   = GetLunch(lunchId);
            var product = _appDbContext.Products.First(x => x.ProductId == productId);

            var shoppingCartLunchItem = lunch.Items?.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem == null)
            {
                shoppingCartLunchItem = new LunchItem {
                    Lunch = lunch, Product = product, Quantity = 1
                };
                _appDbContext.LunchItems.Add(shoppingCartLunchItem);
                _appDbContext.SaveChanges();
            }
            return(shoppingCartLunchItem);
        }
コード例 #7
0
        public async Task <LunchItem> AddItemAsync(int lunchId, int productId, int?quantity)
        {
            var lunch = await GetLunchByIdAsync(lunchId);

            var product = await _catalog.GetById(productId);

            var shoppingCartLunchItem = lunch.Items?.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem == null)
            {
                shoppingCartLunchItem = new LunchItem {
                    Lunch = lunch, Product = product, Quantity = quantity.HasValue ? quantity.Value : 1
                };
                _appDbContext.LunchItems.Add(shoppingCartLunchItem);
                _appDbContext.SaveChanges();
            }

            return(shoppingCartLunchItem);
        }
コード例 #8
0
        public IActionResult Update(long ID, [FromBody] LunchItem item)
        {
            if (item == null || item.ID != ID)
            {
                return(BadRequest());
            }

            var luch = m_context.LunchItems.FirstOrDefault(t => t.ID == ID);

            if (luch == null)
            {
                return(NotFound());
            }

            luch.CreatedAt = DateTimeOffset.Now.ToUnixTimeSeconds();
            luch.Food      = item.Food;

            m_context.LunchItems.Update(luch);
            m_context.SaveChanges();
            return(new NoContentResult());
        }
コード例 #9
0
        public LunchItem RemoveItemInstance(int lunchId, int productId)
        {
            LunchItem result  = null;
            var       lunch   = GetLunch(lunchId);
            var       product = _appDbContext.Products.First(x => x.ProductId == productId);

            var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product);

            if (shoppingCartLunchItem != null)
            {
                if (shoppingCartLunchItem.Quantity > 1)
                {
                    shoppingCartLunchItem.Quantity -= 1;
                    _appDbContext.LunchItems.Update(shoppingCartLunchItem);
                    result = shoppingCartLunchItem;
                }
                else
                {
                    _appDbContext.LunchItems.Remove(shoppingCartLunchItem);
                }
                _appDbContext.SaveChanges();
            }
            return(result);
        }