public async Task EditProgramAsyncShouldThrowExceptionIfCategoryIsIncorrect()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var programModel = new ProgramAdminEditViewModel
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                CategoryId  = null,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await programsService.EditProgramAsync(programModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Exemple #2
0
        public async Task <IActionResult> Edit(ProgramAdminEditViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.programsService.EditProgramAsync(inputModel);

            return(this.RedirectToAction("All"));
        }
        public async Task EditProgramAsync(ProgramAdminEditViewModel inputModel)
        {
            if (!this.dbContext.ProgramCategories.Any(u => u.Id == inputModel.CategoryId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramCategoryId, inputModel.CategoryId));
            }

            var program = await this.dbContext.Programs
                          .Include(p => p.Category)
                          .FirstOrDefaultAsync(p => p.Id == inputModel.Id);

            if (program == null)
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramId, inputModel.Id));
            }

            Enum.TryParse(inputModel.Type, true, out ProgramType programType);

            Enum.TryParse(inputModel.MembershipType, true, out MembershipType membershipType);

            program.IsDeleted = inputModel.IsDeleted;

            program.DeletedOn = inputModel.DeletedOn;

            program.CreatedOn = inputModel.CreatedOn;

            program.ModifiedOn = inputModel.ModifiedOn;

            program.Title = inputModel.Title;

            program.Description = inputModel.Description;

            program.MembershipType = membershipType;

            program.CategoryId = inputModel.CategoryId;

            program.Category.Name = inputModel.CategoryName;

            program.Category.Description = inputModel.CategoryDescription;

            program.Category.IsDeleted = inputModel.CategoryIsDeleted;

            program.Category.Rating = inputModel.CategoryRating;

            program.Rating = inputModel.Rating;

            program.Type = programType;

            this.dbContext.Update(program);

            await this.dbContext.SaveChangesAsync();
        }
        public async Task EditProgramAsyncShouldEditProgramSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var category = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(category);

            await dbContext.SaveChangesAsync();

            for (int i = 0; i < 3; i++)
            {
                var program = new Program
                {
                    Title       = ProgramTitle,
                    Description = ProgramDescription,
                    CategoryId  = category.Id,
                };

                await dbContext.Programs.AddAsync(program);

                await dbContext.SaveChangesAsync();
            }

            var programToEdit = new Program
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                CategoryId  = category.Id,
            };

            await dbContext.Programs.AddAsync(programToEdit);

            await dbContext.SaveChangesAsync();

            var programModel = new ProgramAdminEditViewModel
            {
                Id          = programToEdit.Id,
                Title       = ProgramTitle,
                Description = ProgramDescription,
                Rating      = ProgramRating,
                CategoryId  = category.Id,
            };

            await programsService.EditProgramAsync(programModel);

            var actual = await dbContext.Programs.FirstOrDefaultAsync(p => p.Id == programToEdit.Id);

            Assert.Equal(programToEdit.Rating, actual.Rating);
        }