Beispiel #1
0
        public async Task <IActionResult> Create(CreateGameInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.GenresItems    = this.genresService.GetAllAsKeyValuePairs();
                input.LanguagesItems = this.languagesService.GetAllAsKeyValuePairs();
                input.TagsItems      = this.tagsService.GetAllAsKeyValuePairs();
                input.PublisherItems = this.publishersService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

            try
            {
                await this.gamesService.CreateAsync(input, $"{this.environment.WebRootPath}/images/games");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                input.GenresItems    = this.genresService.GetAllAsKeyValuePairs();
                input.LanguagesItems = this.languagesService.GetAllAsKeyValuePairs();
                input.TagsItems      = this.tagsService.GetAllAsKeyValuePairs();
                input.PublisherItems = this.publishersService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

            return(this.RedirectToAction("All"));
        }
Beispiel #2
0
        public async Task GameCreateShouldWorkCorrectly()
        {
            CreateGameInputModel game = GetGameInModel();

            await this.service.CreateAsync(game, string.Empty);

            Assert.Single(this.gamesList);
        }
Beispiel #3
0
        public void GameCreateShouldThrowExceptionWithIncompleteInput()
        {
            var game = new CreateGameInputModel
            {
                Title = $"Game Test",
            };

            Assert.ThrowsAsync <NullReferenceException>(async() => await this.service.CreateAsync(game, string.Empty));
        }
Beispiel #4
0
        public IActionResult Create(CreateGameInputModel model)
        {
            var game = gamesService.CreateGame(model);

            this.Db.Add(game);

            this.Db.SaveChanges();

            return(this.RedirectToAction("Index"));
        }
        public async Task <IActionResult> Create(CreateGameInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.gamesService.CreateAsync(input);

            return(this.RedirectToAction(nameof(this.All)));
        }
Beispiel #6
0
        public IActionResult Create()
        {
            var viewModel = new CreateGameInputModel
            {
                GenresItems    = this.genresService.GetAllAsKeyValuePairs(),
                LanguagesItems = this.languagesService.GetAllAsKeyValuePairs(),
                TagsItems      = this.tagsService.GetAllAsKeyValuePairs(),
                PublisherItems = this.publishersService.GetAllAsKeyValuePairs(),
            };

            return(this.View(viewModel));
        }
Beispiel #7
0
        public async Task GetAllAsKeyValuePairsWorksCorrectly()
        {
            CreateGameInputModel game1 = GetGameInModel();
            CreateGameInputModel game2 = GetGameInModel();

            await this.service.CreateAsync(game1, string.Empty);

            await this.service.CreateAsync(game2, string.Empty);

            var result = this.service.GetAllAsKeyValuePairs();

            Assert.Equal(2, result.Count());
        }
Beispiel #8
0
        public async Task GetCountShouldReturnCorrectCount()
        {
            CreateGameInputModel game  = GetGameInModel();
            CreateGameInputModel game2 = GetGameInModel();

            await this.service.CreateAsync(game, string.Empty);

            await this.service.CreateAsync(game2, string.Empty);

            int result = this.service.GetCount();

            Assert.Equal(2, result);
        }
Beispiel #9
0
        public Game CreateGame(CreateGameInputModel model)
        {
            var game = new Game
            {
                Id          = model.Id,
                ImageUrl    = model.ImageUrl,
                Name        = model.Name,
                Description = model.Description,
                Price       = model.Price
            };

            return(game);
        }
Beispiel #10
0
        public void GameCreateShouldThrowExceptionForInvalidImageExtension()
        {
            CreateGameInputModel game = GetGameInModel();
            IFormFile            file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");

            game.Images = new List <IFormFile>
            {
                file,
            };

            var exception = Assert.ThrowsAsync <Exception>(async() => await this.service.CreateAsync(game, string.Empty));

            Assert.ThrowsAsync <Exception>(async() => await this.service.CreateAsync(game, string.Empty));
            Assert.Equal("Invalid image extension txt", exception.Result.Message);
        }
Beispiel #11
0
        public async Task CreateAsync(CreateGameInputModel input)
        {
            var game = new Game()
            {
                ImageUrl    = input.ImageUrl,
                Price       = input.Price,
                Description = input.Description,
                ReleaseDate = input.ReleaseDate,
                Title       = input.Title,
            };

            await this.gamesRepository.AddAsync(game);

            await this.gamesRepository.SaveChangesAsync();
        }
Beispiel #12
0
        public async Task <IActionResult> Create([Bind("Name", "MainImage")] CreateGameInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View());
            }

            var game = new Game
            {
                CreatedOn = DateTime.Now,
                Name      = model.Name,
            };

            this.gameService.CreateGame(game);

            if (model.MainImage != null)
            {
                var imageUrl = await this.pictureService.UploadImage(model.MainImage, GlobalConstants.GAME_PATH_TEMPLATE, game.Name, game.Id);

                this.gameService.AddImageUrl(game.Id, imageUrl);
            }

            return(this.RedirectToAction(nameof(this.GameList)));
        }
Beispiel #13
0
        public async Task CreateAsync(CreateGameInputModel input, string imagePath)
        {
            var game = new Game
            {
                Title                   = input.Title,
                Price                   = input.Price,
                CurrentPrice            = input.Price,
                Description             = input.Description,
                ReleaseDate             = input.ReleaseDate,
                GenreId                 = input.GenreId,
                PublisherId             = input.PublisherId,
                MininumRequirements     = input.MininumRequirements,
                RecommendedRequirements = input.RecommendedRequirements,
            };

            foreach (var inputLanguage in input.Languages)
            {
                var language = this.languagesRepository
                               .All()
                               .FirstOrDefault(x => x.Id == inputLanguage);

                game.Languages.Add(new GameLanguage
                {
                    Language = language,
                });
            }

            foreach (var inputTag in input.Tags)
            {
                var tag = this.tagsRepository
                          .All()
                          .FirstOrDefault(x => x.Id == inputTag);

                game.Tags.Add(new GameTag
                {
                    Tag = tag,
                });
            }

            foreach (var image in input.Images)
            {
                var extentsion = Path.GetExtension(image.FileName).TrimStart('.');
                if (!this.allowedExtensions.Any(x => extentsion.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extentsion}");
                }

                var dbImage = new Image
                {
                    UploadName = image.FileName,
                    Extension  = extentsion,
                };

                game.Images.Add(dbImage);

                var path = $"{imagePath}/{dbImage.Id}.{extentsion}";

                using Stream fileStream = new FileStream(path, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.gamesRepository.AddAsync(game);

            await this.gamesRepository.SaveChangesAsync();
        }