Exemplo n.º 1
0
        public async Task <ActionResult <Food> > PostFood(Food food)
        {
            if (_context.Food.Any(f => f.Name == food.Name && f.UserId == food.UserId))
            {
                ICollection <Food> foodList = await _context.Food.Include(f => f.Stockitem).ToListAsync();

                foreach (var foodItem in foodList)
                {
                    if (foodItem.Name == food.Name && foodItem.UserId == food.UserId)
                    {
                        foreach (var item in food.Stockitem)
                        {
                            item.FoodId = foodItem.FoodId;
                            _context.Stockitem.Add(item);
                        }
                        await _context.SaveChangesAsync();

                        return(Ok(food));
                    }
                }
                Debug.WriteLine("------- OH BOI -----");
                return(Ok()); // Fix this
            }
            else
            {
                _context.Food.Add(food);
                await _context.SaveChangesAsync();

                return(Ok(food));
                //return CreatedAtAction("GetFood", new { id = food.FoodId }, food);
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Stockitem> > DeleteStockitem(int id, int userid)
        {
            var stockitem = await _context.Stockitem.Where(s => s.StockitemId == id && s.UserId == userid).FirstAsync();

            if (stockitem == null)
            {
                Debug.WriteLine("not found -------------------");
                return(NotFound());
            }

            _context.Stockitem.Remove(stockitem);
            await _context.SaveChangesAsync();

            return(stockitem);
        }
Exemplo n.º 3
0
        public async Task <ActionResult <User> > PostNewUser(User user)
        {
            try
            {
                var existingUser = await _context.User.Where(u => u.Email == user.Email).FirstAsync();

                return(Ok(existingUser)); // this has to return the full user
            }
            catch (InvalidOperationException)
            {
                _context.User.Add(user);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetUser", new { id = user.UserId }, user));
            }
        }