Example #1
0
        private async void CreateButton_Click(object sender, EventArgs e)
        {
            using var createForm = new RecipeCreateDialogForm(_clientsService, _diseasesService, _recipeCreator);

            var dialogResult = createForm.ShowDialog(this);

            if (dialogResult == DialogResult.OK)
            {
                var createModel = createForm.DialogResultEntity;

                var recipeMedicaments = createModel.RecipeMedicaments.ToList();
                createModel.RecipeMedicaments = null;

                var recipe = await _recipesService.CreateAsync(createModel);

                foreach (var recipeMedicament in recipeMedicaments)
                {
                    recipeMedicament.RecipeId = recipe.Id;
                    await _recipesMedicamentsService.CreateRelationAsync(recipeMedicament);

                    var medicament = await _medicamentsService.ReadByIdAsync(recipeMedicament.MedicamentId);

                    medicament.StockQuantity -= recipeMedicament.Dose;
                    await _medicamentsService.UpdateAsync(medicament);
                }
                await RefreshRecipesDataGridViewAsync();
            }

            createForm.Close();
        }
        public async Task AddReviewShouldCreateANewReviewToRecipe()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext = new ApplicationDbContext(options);

            var recipeRepo            = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo         = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo           = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo              = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service               = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);
            var category              = new Category();
            var nutrValue             = new NutritionValue();
            var user                  = new ApplicationUser();
            var prod                  = new Collection <RecipeByIdProductsViewModel>();
            var recipeCreateViewModel = new RecipeCreateViewModel
            {
                CategoryId     = 1,
                CookProcedure  = "cookProc",
                Photo          = "photo",
                Serving        = 1,
                Title          = "addNew",
                CookTime       = 2,
                NutritionValue = new RecipeCreateNutritionValuesViewModel
                {
                    Calories = 1, Carbohydrates = 1, Fats = 1, Fiber = 1, Protein = 1, Salt = 1, Sugar = 1,
                },
                Products = new List <RecipeCreateProductsViewModel>(),
            };
            string       userId       = "trayan";
            StringValues sv           = new StringValues("one");
            StringValues sk           = new StringValues("1");
            var          recipeResult = await service.CreateAsync(recipeCreateViewModel, userId, sv, sk);

            var model = new ReviewForRecipeViewModel
            {
                Comment   = "commentOne",
                RecipeId  = recipeResult,
                Score     = 5,
                UserId    = "trayan",
                CreatedOn = DateTime.Now,
            };
            var reviewResult = await service.AddReview(model);

            Assert.IsType <string>(reviewResult);
            Assert.NotEmpty(reviewResult);
            Assert.NotNull(reviewResult);
            Assert.True(dbContext.Reviews.Any());
        }
        public async Task EditByAdminShouldChangeRecipe()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext             = new ApplicationDbContext(options);
            var recipeRepo            = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo         = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo           = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo              = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service               = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);
            var category              = new Category();
            var nutrValue             = new NutritionValue();
            var user                  = new ApplicationUser();
            var prod                  = new Collection <RecipeByIdProductsViewModel>();
            var recipeCreateViewModel = new RecipeCreateViewModel
            {
                CategoryId     = 1,
                CookProcedure  = "cookProc",
                Photo          = "photo",
                Serving        = 1,
                Title          = "addNew",
                CookTime       = 2,
                NutritionValue = new RecipeCreateNutritionValuesViewModel
                {
                    Calories = 1, Carbohydrates = 1, Fats = 1, Fiber = 1, Protein = 1, Salt = 1, Sugar = 1
                },
                Products = new List <RecipeCreateProductsViewModel>(),
            };
            string       userId       = "trayan";
            StringValues sv           = new StringValues("one");
            StringValues sk           = new StringValues("1");
            var          recipeResult = await service.CreateAsync(recipeCreateViewModel, userId, sv, sk);

            var model = new AdminRecipeViewModel
            {
                Id            = recipeResult,
                CategoryId    = 5,
                CookProcedure = "five",
                CookTime      = 5,
                Photo         = "fifthPhoto",
                Serving       = 5,
                Title         = "fifthEdit",
            };

            await service.EditByAdmin(model);

            Assert.Equal(5, dbContext.Recipes.FirstOrDefault(x => x.Id == recipeResult).CategoryId);
        }