示例#1
0
        public async Task <IActionResult> CreateDeveloper()
        {
            if (!ModelState.IsValid)
            {
                return(View(DevViewModel));
            }

            var file = DevViewModel.DeveloperImage;

            // if file is uploaded with form
            if (file != null && file.Length > 0)
            {
                var validation = new ImageValidation()
                {
                    FileSize = 3000
                };

                var validationResult = validation.FileCheck(file);

                if (validationResult == "ok")
                {
                    //image validation succeeds
                    var uploadResult = new ImageUploadResult();

                    using (var stream = file.OpenReadStream())
                    {
                        var uploadParams = new ImageUploadParams()
                        {
                            File = new FileDescription(file.Name, stream)
                        };

                        uploadResult = _cloudinary.Upload(uploadParams);
                    }

                    if (uploadResult.Error == null)
                    {
                        DevViewModel.Developer.PhotoId  = uploadResult.PublicId;
                        DevViewModel.Developer.PhotoUrl = uploadResult.Uri.ToString();
                    }
                }
                else //image validation fails
                {
                    SetMessage("danger", validationResult);
                    return(RedirectToAction(nameof(Index)));
                }
            }

            _repo.Add(DevViewModel.Developer);

            if (await _repo.SaveAll())
            {
                SetMessage("info", "Developer added");
                return(RedirectToAction(nameof(Index)));
            }

            SetMessage("danger", "Something went wrong while trying to save changes to the database");
            return(RedirectToAction(nameof(Index)));
        }
示例#2
0
        public async Task <IActionResult> CreateGame()
        {
            if (!ModelState.IsValid)
            {
                return(View(GameViewModel));
            }

            var file = GameViewModel.GameImage;

            var uploadResult = new ImageUploadResult();

            if (file != null && file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(file.Name, stream)
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
                GameViewModel.Game.PhotoUrl = uploadResult.Uri.ToString();
                GameViewModel.Game.PhotoId  = uploadResult.PublicId;
            }

            _repo.Add(GameViewModel.Game);

            if (await _repo.SaveAll())
            {
                SetMessage("info", "Game added");
                return(RedirectToAction(nameof(Index)));
            }

            SetMessage("danger", "Something went wrong when saving to database");
            return(RedirectToAction(nameof(Index)));
        }