Пример #1
0
        public async Task <IActionResult> Create(CreateTeamViewModel formModel)
        {
            var userId = this.userManager.GetUserId(User);

            if (await this.teams.ContainsNameAsync(formModel.Name))
            {
                ModelState.AddModelError("Name",
                                         string.Format(ErrorMessages.TeamNameExists, formModel.Name));
                return(View(formModel));
            }

            if (await this.teams.ContainsTagAsync(formModel.Tag))
            {
                ModelState.AddModelError("Tag",
                                         string.Format(ErrorMessages.TeamTagExists, formModel.Tag));
                return(View(formModel));
            }

            var fileName      = formModel.Image.FileName;
            var imageModelKey = nameof(formModel.Image);

            // Checks if image file extensions is valid.
            if (!ImageFileExtensions.FileExtensionValidation(formModel.Image.FileName))
            {
                ModelState.AddModelError(imageModelKey, ErrorMessages.InvalidImageFileExtension);
                return(View(formModel));
            }

            // Checks if image file signature is valid
            if (!ImageFileExtensions.FileSignatureValidation(fileName, formModel.Image.OpenReadStream()))
            {
                ModelState.AddModelError(imageModelKey, ErrorMessages.InvalidImageFileSignature);
                return(View(formModel));
            }

            // Uploads image to root folder and returns generated image file name.
            var imageName = await ImageFileExtensions.UploadFileAsync(formModel.Image, webHostEnvironment.WebRootPath);


            await this.teams.CreateAsync(
                formModel.Name,
                formModel.Tag,
                imageName,
                userId);

            TempData.AddSuccessMessage(string.Format(SuccessMessages.CreateTeam, formModel.Name));

            return(RedirectToAction(nameof(HomeController.Index), "Home", new { area = string.Empty }));
        }
        public async Task <IActionResult> Add(AddGameFormModel formModel)
        {
            if (await this.games.ContainsAsync(formModel.Name))
            {
                ModelState.AddModelError(nameof(formModel.Name),
                                         string.Format(ErrorMessages.GameExists, formModel.Name));
                return(RedirectToAction(nameof(Add)));
            }

            var fileName      = formModel.Image.FileName;
            var imageModelKey = nameof(formModel.Image);

            // Checks if image file extensions is valid.
            if (!ImageFileExtensions.FileExtensionValidation(formModel.Image.FileName))
            {
                ModelState.AddModelError(imageModelKey, ErrorMessages.InvalidImageFileExtension);
                return(View(formModel));
            }

            // Checks if image file signature is valid
            if (!ImageFileExtensions.FileSignatureValidation(fileName, formModel.Image.OpenReadStream()))
            {
                ModelState.AddModelError(imageModelKey, ErrorMessages.InvalidImageFileSignature);
                return(View(formModel));
            }

            // Uploads image to root folder and returns generated image file name.
            var imageName = await ImageFileExtensions.UploadFileAsync(formModel.Image, webHostEnvironment.WebRootPath);



            await this.adminGames.AddAsync(
                formModel.Name,
                formModel.Developer,
                imageName);

            TempData.AddSuccessMessage(string.Format(SuccessMessages.AddedGame, formModel.Name));

            return(RedirectToAction(
                       nameof(Web.Controllers.GamesController.Index),
                       "Home", new { area = string.Empty }));
        }