Example #1
0
        // GET developers/edit/id
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DevViewModel.Developer = await _repo.GetDeveloper(id);

            if (DevViewModel.Developer == null)
            {
                return(NotFound());
            }

            return(View(DevViewModel));
        }
Example #2
0
        public async Task <IActionResult> EditGame([FromRoute] int?id)
        {
            if (!ModelState.IsValid)
            {
                GameViewModel.Developers = await _repo.GetDevelopers();

                return(View(GameViewModel));
            }

            if (id != GameViewModel.Game.Id)
            {
                SetMessage("danger", "Something went wrong");
                return(RedirectToAction(nameof(Index)));
            }

            var gameFromRepo = await _repo.GetGame(id);

            if (gameFromRepo == null)
            {
                SetMessage("danger", "Not found");
                return(RedirectToAction(nameof(Index)));
            }

            var devFromRepo = await _repo.GetDeveloper(GameViewModel.Game.DeveloperId);

            if (devFromRepo == null)
            {
                SetMessage("danger", "Invalid developer");
                return(RedirectToAction(nameof(Index)));
            }

            var file = GameViewModel.GameImage;

            var uploadResult = new ImageUploadResult();

            // if new image is uploaded
            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);
                }

                if (uploadResult.Error == null)
                {
                    if (gameFromRepo.PhotoId != null)
                    {
                        var deletionParams = new DeletionParams(gameFromRepo.PhotoId);
                        var deletionResult = _cloudinary.Destroy(deletionParams);
                    }

                    gameFromRepo.PhotoId  = uploadResult.PublicId;
                    gameFromRepo.PhotoUrl = uploadResult.Uri.ToString();
                }
            }

            gameFromRepo.Name        = GameViewModel.Game.Name;
            gameFromRepo.Year        = GameViewModel.Game.Year;
            gameFromRepo.Rating      = GameViewModel.Game.Rating;
            gameFromRepo.DeveloperId = GameViewModel.Game.DeveloperId;

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

            SetMessage("danger", "Could not save changes to database");
            return(RedirectToAction(nameof(Index)));
        }