public async Task <IActionResult> RegisterItem(AddItemModelView itemToAdd)
        {
            if (!ModelState.IsValid || itemToAdd == null)
            {
                return(BadRequest(new ErrorModelView("information not accepted")));
            }

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Get, $"meals/{itemToAdd.mealId}");

                var client = clientFactory.CreateClient("gfmm");

                var clientResponse = await client.SendAsync(request);

                if (clientResponse.StatusCode == HttpStatusCode.NotFound)
                {
                    return(BadRequest(new ErrorModelView($"no meal was found by the resource identifier '{itemToAdd.mealId}'")));
                }

                var responsePayload = await clientResponse.Content.ReadAsStreamAsync();

                JsonSerializerOptions deserializationOptions = new JsonSerializerOptions();
                deserializationOptions.PropertyNameCaseInsensitive = true;

                GetDetailedMealInformationModelView meal = await JsonSerializer.DeserializeAsync <GetDetailedMealInformationModelView>(responsePayload, deserializationOptions);

                DateTime expirationDate = DateTime.Parse(itemToAdd.expirationDate);
                DateTime productionDate = DateTime.Parse(itemToAdd.productionDate);

                Item item = new Item(MealID.ValueOf(meal.Designation), productionDate, expirationDate);

                this.repositoryFactory.ItemRepository().Save(item);

                RegisterItemModelView response = new RegisterItemModelView(item);

                return(StatusCode(201, response));
            }
            catch (InvalidOperationException argumentException)
            {
                // TODO: Log exception
                return(BadRequest(new ErrorModelView(argumentException.Message)));
            }
            catch (ArgumentException argumentException)
            {
                // TODO: Log exception
                return(BadRequest(new ErrorModelView(argumentException.Message)));
            }
            catch (FormatException dateFormatException)
            {
                // TODO: Log exception
                return(BadRequest(new ErrorModelView(dateFormatException.Message)));
            }
            catch (Exception databaseException)
            {
                // TODO: Log exception
                return(StatusCode(500, null));
            }
        }
        public void TestMealIDWithIdThatIsLessThanThreeCharactersLongThrowsArgumentException()
        {
            string id = "St";

            Action valueOf = () => MealID.ValueOf(id);

            Assert.Throws <ArgumentException>(valueOf);
        }
        public void TestMealIDWithNullIdThrowsArgumentNullException()
        {
            string id = null;

            Action valueOf = () => MealID.ValueOf(id);

            Assert.Throws <ArgumentNullException>(valueOf);
        }
        public void TestMealIDWithIdThatStartsWithLowerCaseLetterThrowsArgumentException()
        {
            string id = "stone soup";

            Action valueOf = () => MealID.ValueOf(id);

            Assert.Throws <ArgumentException>(valueOf);
        }
        public void TestMealIDWithIdThatContainsSymbolsThrowsArgumentException()
        {
            string id = "Stone$";

            Action valueOf = () => MealID.ValueOf(id);

            Assert.Throws <ArgumentException>(valueOf);
        }
        public void TestMealIDWithIdThatIsPrecededBySpaceCharactersThrowsArgumentException()
        {
            string id = "   Stone Soup";

            Action valueOf = () => MealID.ValueOf(id);

            Assert.Throws <ArgumentException>(valueOf);
        }
        public void TestMealIDIdReturnsStringPassedOnValueOf()
        {
            string id = "Stone Soup";

            MealID mealId = MealID.ValueOf(id);

            string Id = mealId.Id;

            Assert.Equal(id, Id);
        }
        public void TestMealIDWithIdThatCompliesWithRulesCompletesSuccessfully()
        {
            string id = "Stone Soup";

            MealID.ValueOf(id);

            string idWithoutSpace = "Stonesoup";

            MealID.ValueOf(idWithoutSpace);
        }
        public void TestMealIDDoesNotProveEqualityToMealIDWithDifferentName()
        {
            string name = "Stone Soup";

            MealID mealId = MealID.ValueOf(name);

            MealID differentMealID = MealID.ValueOf("Duck Rice");

            Assert.NotEqual(mealId, differentMealID);
        }
        public void TestMealIDProvesEqualityToMealIDWithEqualName()
        {
            string name = "Stone Soup";

            MealID mealId = MealID.ValueOf(name);

            MealID equalMealID = MealID.ValueOf(name);

            Assert.Equal(mealId, equalMealID);
        }
        public void TestMealIDDoesNotProveEqualityToDifferentClassObject()
        {
            string name = "Stone Soup";

            MealID mealId = MealID.ValueOf(name);

            object differentClassObject = "";

            Assert.NotEqual(mealId, differentClassObject);
        }
Beispiel #12
0
        public Meal Find(MealID rootIdentifier)
        {
            IQueryable <Meal> meals = ctx.Meals.Where((meal) => meal.Id().Equals(rootIdentifier));

            if (meals.Count() == 0)
            {
                throw new ArgumentException("no meal matches the given business identifier");
            }

            return(meals.First());
        }
Beispiel #13
0
        public Meal Find(MealID rootIdentifier)
        {
            List <Meal> allMeals = All();

            IEnumerable <Meal> meals = allMeals.Where((meal) => meal.Designation.Equals(rootIdentifier));

            if (meals.Count() == 0)
            {
                throw new ArgumentException("no meal matches the given business identifier");
            }

            return(meals.First());
        }
        public async Task <IActionResult> AvailableItems([FromQuery] long mealId)
        {
            try
            {
                List <Item> items = null;

                if (mealId == 0)
                {
                    items = repositoryFactory.ItemRepository().All();
                }
                else
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"meals/{mealId}");

                    var client = clientFactory.CreateClient("gfmm");

                    var clientResponse = await client.SendAsync(request);

                    if (clientResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(NotFound(null));
                    }

                    var responsePayload = await clientResponse.Content.ReadAsStreamAsync();;

                    JsonSerializerOptions deserializationOptions = new JsonSerializerOptions();
                    deserializationOptions.PropertyNameCaseInsensitive = true;

                    GetDetailedMealInformationModelView meal = await JsonSerializer.DeserializeAsync <GetDetailedMealInformationModelView>(responsePayload, deserializationOptions);

                    MealID mealIdVO = MealID.ValueOf(meal.Designation);

                    items = repositoryFactory.ItemRepository().All().Where((item) => item.MealId.Equals(mealIdVO)).ToList();
                }

                if (items.Count == 0)
                {
                    return(NotFound(null));
                }

                GetAvailableItemsModelView modelview = new GetAvailableItemsModelView(items);

                return(Ok(modelview));
            }
            catch (Exception databaseException)
            {
                // TODO: Log exception
                return(StatusCode(500, null));
            }
        }
        public void TestMealIDHasDifferentHashCodeAsMealIDWithEqualName()
        {
            string name = "Stone Soup";

            MealID mealId = MealID.ValueOf(name);

            MealID differentMealID = MealID.ValueOf("Duck Rice");

            int mealIdHashCode = mealId.GetHashCode();

            int differentMealIDHashCode = differentMealID.GetHashCode();

            Assert.NotEqual(mealIdHashCode, differentMealIDHashCode);
        }
        public void TestMealIDHasSameHashCodeAsMealIDWithEqualName()
        {
            string name = "Stone Soup";

            MealID mealId = MealID.ValueOf(name);

            MealID equalMealID = MealID.ValueOf(name);

            int mealIdHashCode = mealId.GetHashCode();

            int equalMealIDHashCode = equalMealID.GetHashCode();

            Assert.Equal(mealIdHashCode, equalMealIDHashCode);
        }
Beispiel #17
0
    public void cook()
    {
        MealID mealId     = MealID.DefaultItem; //set to default Item
        string recipeCode = "";

        for (int i = 0; i < ingredientTypes; i++)
        {
            recipeCode += ingredients[i].ToString();
        }
        if (recipes.ContainsKey(recipeCode))
        {
            mealId = recipes[recipeCode];
        }
    }
Beispiel #18
0
 public string GetGenNumber() => $"{Prefix}{MealID.ToString()}{Suffix}";