コード例 #1
0
        public async Task CreateProgramAsyncShouldThrowExceptionIfCategoryIsIncorrect()
        {
            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 ProgramAdminCreateViewModel
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                Rating      = ProgramRating,
                CategoryId  = null,
            };

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

            Assert.IsType <ArgumentNullException>(exception);
        }
コード例 #2
0
        public async Task <IActionResult> Create(ProgramAdminCreateViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.programsService.CreateProgramAsync(inputModel);

            return(this.RedirectToAction("All"));
        }
コード例 #3
0
        public IActionResult Create()
        {
            var categories = this.categoriesService.GetAllProgramCategories();

            var programViewModel = new ProgramAdminCreateViewModel
            {
                Categories = categories,
            };

            return(this.View(programViewModel));
        }
コード例 #4
0
        public async Task CreateProgramAsyncShouldCreateProgramSuccessfully()
        {
            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();

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

            await programsService.CreateProgramAsync(programModel);

            var actual = await dbContext.Programs.FirstOrDefaultAsync();

            Assert.Equal(programModel.Title, actual.Title);
        }
コード例 #5
0
        public async Task CreateProgramAsync(ProgramAdminCreateViewModel inputModel)
        {
            if (!this.dbContext.ProgramCategories.Any(u => u.Id == inputModel.CategoryId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramCategoryId, inputModel.CategoryId));
            }

            var program = new Program
            {
                Title          = inputModel.Title,
                Type           = inputModel.Type,
                Description    = inputModel.Description,
                Rating         = inputModel.Rating,
                MembershipType = inputModel.MembershipType,
                CategoryId     = inputModel.CategoryId,
            };

            await this.dbContext.Programs.AddAsync(program);

            await this.dbContext.SaveChangesAsync();
        }