Beispiel #1
0
        public ActionResult DoCreate(HttpPostedFileBase file, FilmInput input)
        {
            var film = input.CreateFilm(file, CountriesDao, ActorsDao, DirectorsDao, GenresDao);

            FilmsDao.Add(film);

            return(RedirectToAction("Index"));
        }
        public static void CopyToData(this FilmInput input, Film data, HttpPostedFileBase poster,
                                      IDaoCountry daoCountry, IDaoGenre daoGenre, IDaoDirector daoDirector, IDaoActor daoActor)
        {
            if (data.ID != input.ID)
            {
                throw new Exception("Cannot copy from foreign view to data");
            }

            data.Title       = input.Title;
            data.ReleaseDate = input.ReleaseDate;
            data.IMDbRating  = input.IMDbRating;
            data.Duration    = input.Duration;
            data.Description = input.Description;
            data.Countries   = new List <Country>();

            foreach (var name in input.Countries)
            {
                data.Countries.Add(daoCountry.GetCountryByEnglishName(name));
            }

            data.RemoveAllGenres();

            if (input.Genres != null)
            {
                foreach (var id in input.Genres)
                {
                    var genre = daoGenre.Find(id);
                    data.AddGenre(genre);
                }
            }

            if (input.DirectorID != Guid.Empty)
            {
                data.Director = daoDirector.Find(input.DirectorID);
            }

            data.RemoveAllActors();
            if (input.Actors != null)
            {
                foreach (var id in input.Actors)
                {
                    var actor = daoActor.Find(id);

                    data.AddActor(actor);
                }
            }

            if (poster != null && poster.ContentLength > 0)
            {
                try
                {
                    if (poster.ContentType.Contains("image"))
                    {
                        var filename = Guid.NewGuid().ToString() + Path.GetExtension(poster.FileName);

                        var path = Path.Combine(PathUtils.GetProjectDirectory(),
                                                "Cinematheque.WebSite\\images\\films\\",
                                                filename);
                        poster.SaveAs(path);

                        /*File.Delete(Path.Combine(PathUtils.GetProjectDirectory(),
                         *                      "Cinematheque.WebSite\\images\\films\\",
                         *                      filename);*/

                        data.PosterFileName = filename;
                    }
                    else
                    {
                        throw new Exception("ERROR: Uploaded file is not image");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("ERROR:" + ex.Message.ToString());
                }
            }
        }
Beispiel #3
0
        public ActionResult DoEdit([ModelBinder(typeof(UserModelBinder))] User user, Guid id, HttpPostedFileBase file, FilmInput input)
        {
            var data = FilmsDao.GetFilmWithFullInfo(id);

            input.CopyToData(data, file, CountriesDao, GenresDao, DirectorsDao, ActorsDao);
            FilmsDao.Update(data);

            if (user.HasFavourite(id))
            {
                user.FavFilms.RemoveAll(f => f.ID == id);

                user.FavFilms.Add(data);

                var cookie = new HttpCookie(nameof(User))
                {
                    Value   = JsonConvert.SerializeObject(user),
                    Expires = DateTime.Now.AddYears(1),
                    Path    = "/"
                };

                Response.Cookies.Add(cookie);
            }

            return(RedirectToAction("Index"));
        }
        public static Film CreateFilm(this FilmInput input, HttpPostedFileBase poster,
                                      IDaoCountry daoCountry, IDaoActor daoActor, IDaoDirector daoDirector, IDaoGenre daoGenre)
        {
            var film = new Film
            {
                Title       = input.Title,
                ReleaseDate = input.ReleaseDate,
                IMDbRating  = input.IMDbRating,
                Duration    = input.Duration,
                Description = input.Description,
                Countries   = new List <Country>()
            };

            foreach (var name in input.Countries)
            {
                film.Countries.Add(daoCountry.GetCountryByEnglishName(name));
            }

            if (poster != null && poster.ContentLength > 0)
            {
                try
                {
                    if (poster.ContentType.Contains("image"))
                    {
                        var filename = Guid.NewGuid().ToString() + Path.GetExtension(poster.FileName);

                        var path = Path.Combine(PathUtils.GetProjectDirectory(),
                                                "Cinematheque.WebSite\\images\\films\\",
                                                filename);
                        poster.SaveAs(path);

                        film.PosterFileName = filename;
                    }
                    else
                    {
                        throw new Exception("ERROR: Uploaded file is not image");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("ERROR:" + ex.Message.ToString());
                }
            }
            else
            {
                film.PosterFileName = "default.jpg";
            }

            if (input.Genres != null)
            {
                foreach (var id in input.Genres)
                {
                    var genre = daoGenre.Find(id);
                    film.AddGenre(genre);
                }
            }

            film.Director = daoDirector.Find(input.DirectorID);

            if (input.Actors != null)
            {
                foreach (var id in input.Actors)
                {
                    var actor = daoActor.Find(id);

                    film.AddActor(actor);
                }
            }

            return(film);
        }