public async Task <IActionResult> GetAsync()
        {
            var id = GuidFromToken.Get(HttpContext);

            var list = await _repository.GetAsync(id);

            if (list == null)
            {
                return(NotFound(new ApiStatus(404, "NotFoundError", "Cant get SmartFridge items.")));
            }
            return(Ok(list));
        }
        public async Task <IActionResult> CreateAsync([FromBody] CreateFoodItem command)
        {
            var userId = GuidFromToken.Get(HttpContext);

            if (userId == Guid.Empty)
            {
                return(BadRequest(new ApiStatus(400, "UserIdEmpty", "User id is empty.")));
            }

            if (command is null)
            {
                return(BadRequest(new ApiStatus(400, "UnknownError", "Item null error.")));
            }

            if (string.IsNullOrEmpty(command.Name))
            {
                return(BadRequest(new ApiStatus(400, "ArticleNameError", "ArticleName null error.")));
            }

            if (string.IsNullOrEmpty(command.Quantity.ToString()))
            {
                return(BadRequest(new ApiStatus(400, "ItemQuantityError", "Item quantity null error.")));
            }

            if (string.IsNullOrEmpty(command.Weight.ToString()))
            {
                return(BadRequest(new ApiStatus(400, "UnknownError", "Item weight null error.")));
            }

            if (string.IsNullOrEmpty(command.CategoryId.ToString()))
            {
                return(BadRequest(new ApiStatus(400, "CategoryError", "Item category ID null error.")));
            }

            var isCreated = await _repository.CreateAsync(
                new FoodItem(
                    Guid.NewGuid(),
                    command.Name.ToLower(),
                    command.Weight,
                    command.Quantity,
                    command.CategoryId,
                    userId)
                );

            if (isCreated)
            {
                return(Ok(new ApiStatus(200, "Created", "Food item created.")));
            }
            return(BadRequest(new ApiStatus(400, "UnknownError", "Unknown SmartFridgeCreate error.")));
        }
Example #3
0
        public async Task <IActionResult> ConsumeAsync([FromBody] List <Ingredient> ingredientsList)
        {
            bool isSucceded = false;
            var  userId     = GuidFromToken.Get(HttpContext);

            if (userId == Guid.Empty)
            {
                return(BadRequest(new ApiStatus(400, "UserIdEmpty", "User id is empty.")));
            }

            isSucceded = await _foodItemsRepository.ConsumeFoodItemsAsync(ingredientsList, userId);

            if (isSucceded)
            {
                return(Ok(new ApiStatus(200, "FoodItemsConsumed", "Food items from recipe consumed.")));
            }
            return(BadRequest(new ApiStatus(400, "RecipesError", "Recipes id bad request.")));
        }