Esempio n. 1
0
        public async Task <ActionResult <ShoppingListResponse> > GetApiShoppingList()
        {
            var userId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var query =
                from shoppingList in _context.ShoppingList
                where shoppingList.UserId == userId
                select new ShoppingListResponse
            {
                Ingredients = (
                    from ingredientInList in _context.IngrInShoppingList
                    join ingredient in _context.Ingredient on ingredientInList.IngrName equals ingredient.Name
                    where ingredientInList.UserId == shoppingList.UserId
                    select new QuantifiedIngredient
                {
                    Ingredient = ingredient.ToApiIngredient(),
                    Quantity = ingredientInList.Quantity,
                    Unit = ingredientInList.Unit.ToApiUnit()
                }).ToList(),
                _shoppingList = shoppingList.ToApiShoppingList()
            };

            var shoppingListResponse = await query.FirstOrDefaultAsync();

            if (shoppingListResponse != null)
            {
                return(shoppingListResponse);
            }

            var defaultShoppingList = new ShoppingList
            {
                Name           = "My Shopping List",
                LastUpdate     = DateTime.Now,
                UserId         = userId,
                NumIngredients = 0
            };
            await _context.AddAsync(defaultShoppingList);

            return(new ShoppingListResponse
            {
                _shoppingList = defaultShoppingList.ToApiShoppingList(),
                Ingredients = new List <QuantifiedIngredient>()
            });
        }
        public async Task <ActionResult <ApiIngrInShoppingList> > PostIngrInShoppingList(
            ApiIngrInShoppingList apiIngredient)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (apiIngredient.UserId != currentUserId)
            {
                return(Unauthorized());
            }

            var shoppingList = await _context.ShoppingList.FindAsync(currentUserId);

            if (shoppingList == null)
            {
                var defaultShoppingList = new ShoppingList
                {
                    Name           = "My Shopping List",
                    LastUpdate     = DateTime.Now,
                    UserId         = currentUserId,
                    NumIngredients = 0
                };
                await _context.AddAsync(defaultShoppingList);
            }

            var ingredient = apiIngredient.ToIngrInShoppingList();

            _context.IngrInShoppingList.Add(ingredient);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (IngrInShoppingListExists(ingredient))
                {
                    return(Conflict());
                }

                throw;
            }

            return(Created("", apiIngredient));
        }