Exemplo n.º 1
0
        public ActionResult Create(FilmPersonRoleViewModel filmPersonRoleViewModel)
        {
            //flag if the action was called from the film edit view or the person edit view.
            bool isFromFilmView;

            if (filmPersonRoleViewModel.ThisFilm.FilmId == null || filmPersonRoleViewModel.ThisFilm.FilmId == 0)
            {
                filmPersonRoleViewModel.ThisFilmPersonRole.FilmId = Int32.Parse(Request["Films"]);

                filmPersonRoleViewModel.ThisFilmPersonRole.PersonId = filmPersonRoleViewModel.ThisPerson.PersonId;

                isFromFilmView = false;
            }
            else
            {
                filmPersonRoleViewModel.ThisFilmPersonRole.FilmId = filmPersonRoleViewModel.ThisFilm.FilmId;

                filmPersonRoleViewModel.ThisFilmPersonRole.PersonId = Int32.Parse(Request["Persons"]);

                isFromFilmView = true;
            }


            db.FilmPersonRoles.Add(filmPersonRoleViewModel.ThisFilmPersonRole);
            db.SaveChanges();

            if (isFromFilmView)
            {
                return(RedirectToAction("Index", "Films"));
            }
            else
            {
                return(RedirectToAction("Index", "Persons"));
            }
        }
Exemplo n.º 2
0
        // Get viewmodel data for the edit view.
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Person person = db.Persons.Find(id);

            if (person == null)
            {
                return(HttpNotFound());
            }

            PersonViewModel personViewModel = new PersonViewModel();

            PersonImage personImage = db.PersonImages.Where(x => x.ImageId == person.ImageId).Single();

            List <FilmPersonRoleViewModel> personRoleViewModelList = new List <FilmPersonRoleViewModel>();

            List <FilmPersonRole> rolesList = new List <FilmPersonRole>();

            rolesList = db.FilmPersonRoles.ToList();


            // IF a role exists for this person, add it to the roles list.
            if (rolesList.Count > 0)
            {
                foreach (FilmPersonRole role in rolesList)
                {
                    if (role.PersonId == person.PersonId)
                    {
                        FilmPersonRoleViewModel roleViewModel = new FilmPersonRoleViewModel();

                        Film film = db.Films.Where(x => x.FilmId == role.FilmId).Single();

                        roleViewModel.ThisFilm           = film;
                        roleViewModel.ThisFilmPersonRole = role;

                        personRoleViewModelList.Add(roleViewModel);
                    }
                }
            }

            personViewModel.ThisFilmPersonRolesViewModel = personRoleViewModelList;
            personViewModel.ThisPerson      = person;
            personViewModel.ThisPersonImage = personImage;

            return(View(personViewModel));
        }
Exemplo n.º 3
0
        // GET: FilmPersonRoles
        public ActionResult Index()
        {
            //create a list for the view model to link Film and Person
            List <FilmPersonRoleViewModel> FilmPersonRoleList = new List <FilmPersonRoleViewModel>();

            //separate list for the FilmPersonRole to get the keys
            List <FilmPersonRole> filmPersonRoles;

            //populate the FilmPersonRoleList by selecting all record from the db context
            filmPersonRoles = db.FilmPersonRoles.ToList();

            //loop through each record to get the foreign keys
            //then populate the view model with the relevant
            //Film / Person
            foreach (FilmPersonRole role in filmPersonRoles)
            {
                //match the ID between FilmPersonRole and Film - store the single record in 'film'
                Film film = db.Films.Where(x => x.FilmId == role.FilmId).Single();

                //match the ID between FilmPersonRole and Film - store the single record in 'person'
                Person person = db.Persons.Where(x => x.PersonId == role.PersonId).Single();

                //NEED TO ADD BYTE DATA TO DATABASE FOR THIS TO WORK
                FilmImage filmImage = db.FilmImages.Where(x => x.ImageId == role.FilmId).Single();

                //new FilmPersonRolViewModel object to then add to the list
                FilmPersonRoleViewModel toAdd = new FilmPersonRoleViewModel();

                toAdd.ThisFilmPersonRole = role;    //get the FilmPersonRole record
                toAdd.ThisFilm           = film;    //get the film record
                toAdd.ThisPerson         = person;  //get the person record
                toAdd.ThisFilmImage      = filmImage;

                //add to the FilmPersonRoleList (list of ViewModel objects)
                FilmPersonRoleList.Add(toAdd);
            }

            //send the FilmPersonRoleListViewModel List to the View for display
            return(View(FilmPersonRoleList));
        }
Exemplo n.º 4
0
        // Get viewmodel data for the edit view.
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Film film = db.Films.Find(id);

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

            // Get genre collection for drop down box.
            var genreQuery = from m in db.Genres
                             orderby m.GenreName
                             select m;

            if (film.GenreId == 0)
            {
                ViewBag.Genres = new SelectList(genreQuery, "GenreId", "GenreName", null);
            }
            else
            {
                ViewBag.Genres = new SelectList(genreQuery, "GenreId", "GenreName", film.GenreId);
            }



            // Get certificate selection for drop down box.
            var certificateQuery = from m in db.Certificates
                                   orderby m.CertificateName
                                   select m;

            if (film.CertificateId == 0)
            {
                ViewBag.Certificates = new SelectList(certificateQuery, "CertificateId", "CertificateName", null);
            }
            else
            {
                ViewBag.Certificates = new SelectList(certificateQuery, "CertificateId", "CertificateName", film.CertificateId);
            }

            // Get record for the viewmodel data.
            Genre genre = db.Genres.Where(x => x.GenreId == film.GenreId).Single();

            Certificate certificate = db.Certificates.Where(x => x.CertificateId == film.CertificateId).Single();

            FilmImage filmImage = db.FilmImages.Where(x => x.ImageId == film.ImageId).Single();

            List <FilmPersonRole> filmPersonRoles = db.FilmPersonRoles.ToList();

            List <FilmPersonRoleViewModel> rolesForThisFilm = new List <FilmPersonRoleViewModel>();

            // Loop through film roles for roles related to this film.
            // Add matches to the role viewmodel list.
            foreach (FilmPersonRole role in filmPersonRoles)
            {
                if (role.FilmId == id)
                {
                    Person person = db.Persons.Where(x => x.PersonId == role.PersonId).Single();

                    PersonImage personImage = new PersonImage();

                    if (person != null)
                    {
                        personImage = db.PersonImages.Where(x => x.ImageId == person.ImageId).Single();
                    }

                    FilmPersonRoleViewModel filmPersonRoleViewModel = new FilmPersonRoleViewModel();

                    filmPersonRoleViewModel.ThisFilm           = film;
                    filmPersonRoleViewModel.ThisPerson         = person;
                    filmPersonRoleViewModel.ThisPersonImage    = personImage;
                    filmPersonRoleViewModel.ThisFilmPersonRole = role;

                    rolesForThisFilm.Add(filmPersonRoleViewModel);
                }
            }

            FilmViewModel filmViewModel = new FilmViewModel();

            filmViewModel.ThisFilm                    = film;
            filmViewModel.ThisGenre                   = genre;
            filmViewModel.ThisCertificate             = certificate;
            filmViewModel.ThisFilmImage               = filmImage;
            filmViewModel.ThisFilmPersonRoleViewModel = rolesForThisFilm;

            return(View(filmViewModel));
        }
Exemplo n.º 5
0
        // Get film details by film id, builds a viewmodel for return.
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Film film = db.Films.Find(id);

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

            // Get's an average review score from all Reviews for this film.
            var averageReview = db.Reviews.Where(x => x.FilmId == id)
                                .Average(x => (int?)x.Rating) ?? 0;


            // Get records for the view model.
            Genre genre = db.Genres.Where(x => x.GenreId == film.GenreId).Single();

            Certificate certificate = db.Certificates.Where(x => x.CertificateId == film.CertificateId).Single();

            FilmImage filmImage = db.FilmImages.Where(x => x.ImageId == film.ImageId).Single();

            List <FilmPersonRole> filmPersonRoles = db.FilmPersonRoles.ToList();

            List <Review> reviews = db.Reviews.ToList();


            // If reviews exist for this film, return a list of reviews for the viewmodel.
            List <ReviewViewModel> reviewsForThisFilm = new List <ReviewViewModel>();

            if (reviews.Count > 0)
            {
                foreach (Review r in reviews)
                {
                    if (r.FilmId == id)
                    {
                        ReviewViewModel reviewViewModel = new ReviewViewModel();
                        reviewViewModel.thisReview = r;

                        User user = db.Users.Where(x => x.UserId == r.UserId).Single();
                        reviewViewModel.thisUser = user;

                        reviewViewModel.thisFilm = film;

                        reviewsForThisFilm.Add(reviewViewModel);
                    }
                }
            }

            // If roles exist for this film, return a list of roles for the viewmodel.
            List <FilmPersonRoleViewModel> rolesForThisFilm = new List <FilmPersonRoleViewModel>();

            foreach (FilmPersonRole role in filmPersonRoles)
            {
                if (role.FilmId == id)
                {
                    Person person = db.Persons.Where(x => x.PersonId == role.PersonId).Single();

                    PersonImage personImage = new PersonImage();

                    if (person != null)
                    {
                        personImage = db.PersonImages.Where(x => x.ImageId == person.ImageId).Single();
                    }


                    FilmPersonRoleViewModel filmPersonRoleViewModel = new FilmPersonRoleViewModel();

                    filmPersonRoleViewModel.ThisFilm           = film;
                    filmPersonRoleViewModel.ThisPerson         = person;
                    filmPersonRoleViewModel.ThisPersonImage    = personImage;
                    filmPersonRoleViewModel.ThisFilmPersonRole = role;

                    rolesForThisFilm.Add(filmPersonRoleViewModel);
                }
            }

            FilmViewModel filmViewModel = new FilmViewModel();

            filmViewModel.ThisFilm                    = film;
            filmViewModel.ThisFilmImage               = filmImage;
            filmViewModel.ThisGenre                   = genre;
            filmViewModel.ThisCertificate             = certificate;
            filmViewModel.ThisFilmReviews             = reviewsForThisFilm;
            filmViewModel.ThisFilmPersonRoleViewModel = rolesForThisFilm;
            filmViewModel.averageReview               = (int)averageReview;

            return(View(filmViewModel));
        }
Exemplo n.º 6
0
        // GET: FilmPersonRoles/Create
        public ActionResult Create(int?FilmId, int?PersonId, bool isActor)
        {
            FilmPersonRoleViewModel filmPersonRoleViewModel = new FilmPersonRoleViewModel();
            FilmPersonRole          filmPersonRole          = new FilmPersonRole();
            Film        film        = new Film();
            FilmImage   filmImage   = new FilmImage();
            Person      person      = new Person();
            PersonImage personImage = new PersonImage();

            if (isActor == true)
            {
                filmPersonRole.IsActor    = true;
                filmPersonRole.IsDirector = false;
            }
            else
            {
                filmPersonRole.IsActor    = false;
                filmPersonRole.IsDirector = true;
            }

            //db.FilmImages.Where(x => x.ImageId == role.FilmId).Single();

            if (FilmId == null && PersonId == null)
            {
                //Return to home page as user is manipulating the url
                return(View("~/Views/Home/Index.cshtml"));
            }
            else if (FilmId == null || FilmId == 0)
            {
                filmPersonRole.PersonId = PersonId;
                person      = db.Persons.Where(x => x.PersonId == PersonId).Single();
                personImage = db.PersonImages.Where(x => x.ImageId == person.ImageId).Single();

                //If FilmId is null then link the existing person to a film choice

                //Get film collection for drop down box
                var filmQuery = from m in db.Films
                                orderby m.Title
                                select m;

                ViewBag.Films = new SelectList(filmQuery, "FilmId", "Title", null);
            }
            else if (PersonId == null || PersonId == 0)
            {
                filmPersonRole.FilmId = FilmId;
                film      = db.Films.Where(x => x.FilmId == FilmId).Single();
                filmImage = db.FilmImages.Where(x => x.ImageId == film.ImageId).Single();

                //If PersonId is null then link the existing film to a person choice

                if (isActor)
                {
                    //Get Actors collection for drop down box
                    var personQuery = from p in db.Persons
                                      where p.IsActor == true
                                      orderby p.LastName
                                      select new
                    {
                        FullName = p.FirstName + " " + p.LastName,
                        p.PersonId
                    };

                    ViewBag.Persons = new SelectList(personQuery, "PersonId", "FullName", null);
                }
                else
                {
                    //Get Directors collection for drop down box
                    var personQuery = from p in db.Persons
                                      where p.IsDirector == true
                                      orderby p.LastName
                                      select new
                    {
                        FullName = p.FirstName + " " + p.LastName,
                        p.PersonId
                    };

                    ViewBag.Persons = new SelectList(personQuery, "PersonId", "FullName", null);
                }
            }

            filmPersonRoleViewModel.ThisFilm           = film;
            filmPersonRoleViewModel.ThisFilmImage      = filmImage;
            filmPersonRoleViewModel.ThisFilmPersonRole = filmPersonRole;
            filmPersonRoleViewModel.ThisPerson         = person;
            filmPersonRoleViewModel.ThisPersonImage    = personImage;

            //generate the view
            return(View(filmPersonRoleViewModel));
        }