public async Task CreateImageAsync(ImageAdminCreateViewModel inputModel)
        {
            if (inputModel.PostId != null && !this.dbContext.Post.Any(p => p.Id == inputModel.PostId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidPostId, inputModel.PostId));
            }

            if (inputModel.ProductId != null && !this.dbContext.Products.Any(p => p.Id == inputModel.ProductId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProductId, inputModel.ProductId));
            }

            if (inputModel.ProgramId != null && !this.dbContext.Programs.Any(p => p.Id == inputModel.ProgramId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramId, inputModel.ProgramId));
            }

            if (inputModel.ExerciseId != null && !this.dbContext.Exercises.Any(p => p.Id == inputModel.ExerciseId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidExerciseId, inputModel.ExerciseId));
            }

            var image = new Image
            {
                Url        = inputModel.Url,
                ProductId  = inputModel.ProductId,
                PostId     = inputModel.PostId,
                ExerciseId = inputModel.ExerciseId,
                ProgramId  = inputModel.ProgramId,
            };

            await this.dbContext.Images.AddAsync(image);

            await this.dbContext.SaveChangesAsync();
        }
        public async Task <IActionResult> Create(ImageAdminCreateViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.imagesService.CreateImageAsync(inputModel);

            return(this.RedirectToAction("All"));
        }
Example #3
0
        public async Task CreateImageAsyncShouldCreateImageSuccessfully()
        {
            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 postsService = new PostsService(dbContext, mapper, usersService, categoriesService);

            var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService);

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

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService);

            var imageModel = new ImageAdminCreateViewModel
            {
                Url = ImageUrl,
            };

            await imagesService.CreateImageAsync(imageModel);

            var actual = await dbContext.Images.FirstOrDefaultAsync(i => i.Url == imageModel.Url);

            Assert.Equal(imageModel.Url, actual.Url);
        }
Example #4
0
        public async Task CreateImageAsyncShouldThrowExpetionIfExerciseIdIsIncorrect()
        {
            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 postsService = new PostsService(dbContext, mapper, usersService, categoriesService);

            var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService);

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

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService);

            var imageModel = new ImageAdminCreateViewModel
            {
                Url        = ImageUrl,
                ExerciseId = ExerciseId,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await imagesService.CreateImageAsync(imageModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
        public async Task <IActionResult> Create()
        {
            var posts = await this.postsService.GetAllPostsForAdminAsync();

            var products = await this.productsService.GetProductsForAdminAsync();

            var exercises = await this.exercisesService.GetAllExercisesForAdminAsync();

            var programs = await this.programsService.GetAllProgramsForAdminAsync();

            var imageCreateViewModel = new ImageAdminCreateViewModel();

            imageCreateViewModel.Posts = posts
                                         .Select(c => new List <string> {
                c.Id, c.Title
            })
                                         .SelectMany(c => c);

            imageCreateViewModel.Products = products
                                            .Select(c => new List <string> {
                c.Id, c.Name
            })
                                            .SelectMany(c => c);

            imageCreateViewModel.Exercises = exercises
                                             .Select(c => new List <string> {
                c.Id, c.Name
            })
                                             .SelectMany(c => c);

            imageCreateViewModel.Programs = programs
                                            .Select(c => new List <string> {
                c.Id, c.Title
            })
                                            .SelectMany(c => c);

            return(this.View(imageCreateViewModel));
        }