コード例 #1
0
        public async Task <IActionResult> EditFilm(EditFilmViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Film film = await _db.EditFilm(model);

                    if (film != null)
                    {
                        TempData["message3"] = "Фильм изменён";
                        return(RedirectToAction("editfilm", "film", new { model.Id, model.UserId }));
                    }
                    else
                    {
                        TempData["message2"] = "Фильма не существует";
                        return(RedirectToAction("index", "film"));
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    TempData["message2"] = "Ой! Что - то пошло не так. Попробуйте позже.";
                    return(RedirectToAction("index", "film"));
                }
            }
            else
            {
                model.Poster = Convert.FromBase64String(model.PosterAsString);
                return(View(model));
            }
        }
コード例 #2
0
        public IActionResult FilmEdit(EditFilmViewModel model, int id)
        {
            Film filmFromDb = _DbContext.Films.FirstOrDefault(a => a.Id == id);

            if (!TryValidateModel(model))
            {
                return(View(model));
            }
            if (model.Foto != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    model.Foto.CopyTo(stream);
                    filmFromDb.Foto = stream.ToArray();
                }
            }
            filmFromDb.Titel     = model.Titel;
            filmFromDb.Regisseur = model.Regisseur;
            filmFromDb.Hidden    = model.Hidden;
            _DbContext.SaveChanges();

            if (User.IsInRole("Admin"))
            {
                return(RedirectToAction("FilmsList"));
            }
            else
            {
                return(RedirectToAction("FilmsList", "Gebruiker"));
            }
        }
コード例 #3
0
        public async Task <IActionResult> EditFilm(int id, string userId)
        {
            try
            {
                string UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                Film   film   = await _db.GetFilmById(id);

                if (film != null)
                {
                    if (UserId == userId)
                    {
                        EditFilmViewModel model = new EditFilmViewModel {
                            Id = film.Id, Title = film.Title, Regisseur = film.Regisseur, YearofManufacture = film.YearofManufacture, Poster = film.Poster, Description = film.Description, UserId = film.UserId, PosterAsString = Convert.ToBase64String(film.Poster)
                        };
                        return(View(model));
                    }
                    else
                    {
                        TempData["message2"] = "Вы можете редактировать только, добавленные вами, фильмы";
                        return(RedirectToAction("index", "film"));
                    }
                }
                else
                {
                    TempData["message2"] = "Фильма не существует";
                    return(RedirectToAction("index", "film"));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                TempData["message2"] = "Ой! Что - то пошло не так. Попробуйте позже.";
                return(RedirectToAction("index", "film"));
            }
        }
コード例 #4
0
ファイル: FilmService.cs プロジェクト: CJRFerreira/FilmHaus
        public void UpdateFilmByMediaId(Guid mediaId, EditFilmViewModel film)
        {
            try
            {
                var result = FilmHausDbContext.Films.Find(mediaId);

                if (result == null)
                {
                    throw new ArgumentNullException();
                }

                result.PosterUri     = film.PosterUri;
                result.MediaName     = film.MediaName;
                result.DateOfRelease = film.DateOfRelease;
                result.Accolades     = film.Accolades;
                result.Runtime       = film.Runtime;

                FilmHausDbContext.Entry(result).State = EntityState.Modified;
                FilmHausDbContext.SaveChanges();
            }
            catch (InvalidOperationException ex)
            {
                throw ex;
            }
        }
コード例 #5
0
        public void Add()
        {
            EditFilmViewModel editFilmViewModel = _resolutionRoot.Get <EditFilmViewModel>();

            ((ISupportParentViewModel)editFilmViewModel).ParentViewModel = this;
            DialogService.ShowDialog("Film Editor", editFilmViewModel);
            Refresh();
        }
コード例 #6
0
        public void Edit()
        {
            EditFilmViewModel editFilmViewModel = _resolutionRoot.Get <EditFilmViewModel>(new ConstructorArgument("film", SelectedFilm));

            ((ISupportParentViewModel)editFilmViewModel).ParentViewModel = this;
            DialogService.ShowDialog("Film Editor", editFilmViewModel);
            Refresh();
        }
コード例 #7
0
        public IActionResult FilmEdit(int id)
        {
            Film filmFromDb        = _DbContext.Films.FirstOrDefault(a => a.Id == id);
            EditFilmViewModel film = new EditFilmViewModel();

            film.Titel         = filmFromDb.Titel;
            film.Regisseur     = filmFromDb.Regisseur;
            film.Id            = filmFromDb.Id;
            film.Hidden        = filmFromDb.Hidden;
            film.BestaandeFoto = filmFromDb.Foto;

            return(View(film));
        }
コード例 #8
0
        public ActionResult Edit(EditFilmViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            try
            {
                FilmService.UpdateFilmByMediaId(viewModel.MediaId, viewModel);
            }
            catch
            {
                throw;
            }

            return(RedirectToAction("Index"));
        }
コード例 #9
0
        public async Task <IActionResult> Edit(int filmId)  // Редактирование фильма
        {
            if (filmId <= 0)
            {
                return(BadRequest());
            }

            var film = await filmService.Get(filmId);

            if (film == null)
            {
                return(BadRequest());
            }

            var viewModel = new EditFilmViewModel();

            viewModel.CopyProperties(film);
            viewModel.ReturnUrl = Url.Action(nameof(Info), new { Id = filmId });

            return(View(viewModel));
        }
コード例 #10
0
        /// <summary>
        /// Сохранить изменённый фильм
        /// </summary>
        /// <param></param>
        /// <returns>Film</returns>
        public async Task <Film> EditFilm(EditFilmViewModel model)
        {
            Film film = await db.Films.FirstOrDefaultAsync(x => x.Id == model.Id);

            if (film != null)
            {
                film.Title             = model.Title;
                film.Description       = model.Description;
                film.Regisseur         = model.Regisseur;
                film.YearofManufacture = model.YearofManufacture.Value;
                film.UserId            = model.UserId;
                if (model.Image != null)
                {
                    byte[] imageData = null;
                    using (var binaryReader = new BinaryReader(model.Image.OpenReadStream()))
                    {
                        imageData = binaryReader.ReadBytes((int)model.Image.Length);
                    }
                    film.Poster = imageData;
                }
                await db.SaveChangesAsync();
            }
            return(film);
        }
コード例 #11
0
        public async Task <IActionResult> Edit(PostEditFilmViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            var film = (model.Id > 0) ? await filmService.Get(model.Id) : new Film();

            // Если в БД фильма с таким идентификатором уже нет
            if (model.Id > 0 && film == null)
            {
                return(NotFound());
            }

            var userId = userManager.GetUserId(User);

            // Если редактирует не тот пользователь, который создавал
            if (model.Id > 0 && userId != film.UserId)
            {
                return(BadRequest());
            }

            byte[] imageData = ReadPoster(model.PosterFile);

            if (model.Year < 1900 || model.Year > DateTime.Now.Year)
            {
                ModelState.AddModelError("Year", "Некорректный год");
            }

            if (!ModelState.IsValid)
            {
                var viewModel = new EditFilmViewModel();
                viewModel.CopyProperties(model);
                if (model.Id > 0)
                {
                    viewModel.Poster = film.Poster;
                }
                return(View(viewModel));
            }
            try
            {
                film.CopyProperties(model);
                film.UserId = userId;
                if (imageData != null)
                {
                    film.Poster = imageData;
                }

                var newFilm = film;
                if (model.Id == 0)
                {
                    newFilm = await filmService.Insert(film);
                }

                await filmService.Save();  // тут в newFilm.Id записывается новый идентификатор

                if (newFilm.Id == 0)
                {
                    return(BadRequest());
                }

                return(RedirectToAction(nameof(Info), new { filmId = newFilm.Id }));
            }
            catch
            {
                return(StatusCode(500));
            }
        }