/// <summary>
        /// Populates an instance of the wrapper class for creating/editing
        /// films with the appropriate information from the film to be edited.
        /// </summary>
        /// <param name="filmId">PK for the film to be edited</param>
        /// <returns>An instnace of the wrapper class for creating/editing films</returns>
        public AddFilmVM GetEditFilm(int filmId)
        {
            FilmVM editFilmVM = GetFilm(filmId);
            var    addFilmVM  = GetAddFilmVM();

            if (editFilmVM != null)
            {
                addFilmVM.FilmId      = editFilmVM.Id;
                addFilmVM.Title       = editFilmVM.Title;
                addFilmVM.Description = editFilmVM.Description;
                addFilmVM.ImgURL      = editFilmVM.ImgURL;
                addFilmVM.PriceId     = editFilmVM.PriceClassId;

                List <int> ids = new List <int>();
                for (int i = 0; i < editFilmVM.Genres.Count; i++)
                {
                    ids.Add(editFilmVM.Genres[i].Id);
                }
                addFilmVM.CurrFilmGenreIds = ids;
            }

            if (editFilmVM == null || addFilmVM == null)
            {
                return(null);
            }
            else
            {
                return(addFilmVM);
            }
        }
        /// <summary>
        /// Edits the row in the database table that corresponds to the provided input film
        /// with information encapsulated in the wrapper class object.
        /// </summary>
        /// <param name="editAddFilm">The wrapper class for creating/editing films</param>
        /// <param name="priceClassId">PK for the price class that is selected</param>
        /// <param name="genreIds">PKs for the selected genres</param>
        /// <returns>An empty string if successful, an error message if not</returns>
        public string EditFilm(AddFilmVM editAddFilm, int priceClassId, int[] genreIds)
        {
            var editFilm = new FilmVM()
            {
                Id          = editAddFilm.FilmId,
                Title       = editAddFilm.Title,
                Description = editAddFilm.Description,
                ImgURL      = editAddFilm.ImgURL
            };

            try
            {
                using (var db = new VideoDB())
                {
                    Film film = db.Films.Find(editFilm.Id);
                    film.Title       = editFilm.Title;
                    film.Description = editFilm.Description;
                    film.ImgURL      = editFilm.ImgURL;

                    PriceClass findPriceClass = db.PriceClasses.Find(priceClassId);
                    film.PriceClasses = findPriceClass;
                    film.PriceClassId = priceClassId;

                    //Bug i EF? Må iterere gjennom listen for å nullstille
                    foreach (Genre g in film.Genres)
                    {
                    }

                    if (genreIds != null)
                    {
                        film.Genres.RemoveRange(0, film.Genres.Count);
                        foreach (int genreId in genreIds)
                        {
                            film.Genres.Add(db.Genres.Find(genreId));
                        }
                    }

                    string gIDs = "";

                    if (genreIds.Length > 0)
                    {
                        gIDs = string.Join(" ", genreIds);
                    }
                    film.GenreIds = gIDs;

                    db.SaveChanges();
                    return("");
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.EditFilm(FilmVM newFilm)", editFilm.ToString(), e);
                return("Kunne ikke lagre filmen i databasen, vennligst kontakt kundeservice!");
            }
        }
        /// <summary>
        /// Creates a new film from the provided data and initializes a new row in the film
        /// database table with the required information returned from the view.
        /// </summary>
        /// <param name="newAddFilm">The wrapper class for creating/editing films</param>
        /// <param name="priceClassId">PK for the selected price class</param>
        /// <param name="genreIds">PKs for the selected genres</param>
        /// <returns>An empty string if the operations were successful, otherwise an error message</returns>
        public string CreateFilm(AddFilmVM newAddFilm, int priceClassId, int[] genreIds)
        {
            var newFilm = new FilmVM()
            {
                Title       = newAddFilm.Title,
                Description = newAddFilm.Description,
                ImgURL      = newAddFilm.ImgURL
            };

            try
            {
                using (var db = new VideoDB())
                {
                    var newDBFilm = new Film
                    {
                        Title       = newFilm.Title,
                        Description = newFilm.Description,
                        ImgURL      = newFilm.ImgURL
                    };

                    PriceClass findPriceClass = db.PriceClasses.Find(priceClassId);
                    newDBFilm.PriceClasses = findPriceClass;

                    newDBFilm.PriceClassId = priceClassId;

                    List <Genre> genreList   = new List <Genre>();
                    List <int>   genreIdList = new List <int>();
                    foreach (int genreId in genreIds)
                    {
                        genreList.Add(db.Genres.Find(genreId));
                        genreIdList.Add(genreId);
                    }

                    newDBFilm.Genres = genreList;
                    string gIDs = "";

                    if (genreIds.Length > 0)
                    {
                        gIDs = string.Join(" ", genreIds);
                    }

                    newDBFilm.GenreIds = gIDs;
                    db.Films.Add(newDBFilm);
                    db.SaveChanges();
                    return("");
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.CustomerDAL.CreateFilm(FilmVM newFilm)", newFilm.ToString(), e);
                return("Kunne ikke lagre filmen i databasen, vennligst kontakt kundeservice!");
            }
        }
Exemple #4
0
        public ActionResult Create(FilmVM FilmVM)
        {
            Film f = new Film()
            {
                Id          = FilmVM.Id,
                Description = FilmVM.Description,
                Genre       = FilmVM.Genre,
                ImageUrl    = FilmVM.ImageUrl,
                OutDate     = FilmVM.OutDate,
                Title       = FilmVM.Title,
            };

            Service.Add(f);
            Service.Commit();
            return(RedirectToAction("Index"));
        }
        public ActionResult Create(FilmVM filmVM)
        {
            Film F = new Film()
            {
                Id          = filmVM.Id,
                Description = filmVM.Description,
                Genre       = filmVM.Genre,
                ImageUrl    = filmVM.ImageUrl,
                OutDate     = filmVM.OutDate,
                Title       = filmVM.Title
            };

            service.Add(F);
            service.Commit();
            return(RedirectToAction("Index"));
        }
Exemple #6
0
 public ActionResult FilmEkle(FilmVM film)
 {
     if (ModelState.IsValid)
     {
         service.FilmService.Insert(new Film
         {
             Adi         = film.Adi,
             UcBoyutluMu = film.UcBoyutluMu,
             Sure        = film.Sure,
             YerliMi     = film.YerliMi,
             TurID       = film.TurID
         });
         return(Redirect("/Film/Index"));
     }
     TempData["Error"] = "*Tüm alanlar dolu olmalıdır";
     return(View());
 }
        /// <summary>
        /// Retrieves a film from the database and then converts it to a
        /// View-Model representation of a film and then returns it to the
        /// caller.
        /// </summary>
        /// <param name="filmId">PK of the film to return</param>
        /// <returns>The FilmVM if successful, null if not successful</returns>
        public FilmVM GetFilm(int filmId)
        {
            try
            {
                var filmVM = new FilmVM();

                using (var db = new VideoDB())
                {
                    var film = db.Films.Find(filmId);
                    filmVM = ConvertFilm(film);
                }

                return(filmVM);
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.GetFilm(int  filmId)", filmId.ToString(), e);
                return(null);
            }
        }
        /// <summary>
        /// Helper method that converts a DB-Model film into a DomainModel equivalent.
        /// </summary>
        /// <param name="film"></param>
        /// <returns>FilmVM</returns>
        private FilmVM ConvertFilm(Film film)
        {
            var filmVM = new FilmVM()
            {
                Id          = film.Id,
                Title       = film.Title,
                Description = film.Description,
                ImgURL      = film.ImgURL,
                CreatedDate = TimeZoneInfo.ConvertTimeFromUtc(film.CreatedDate, tZone).ToString("dd.MM.yyyy HH:mm:ss"),
                //Makes a list of all the genres the film is in.
                Genres = film.Genres.Select(s => new GenreVM()
                {
                    Id   = s.Id,
                    Name = s.Name
                }).ToList(),
                Price        = film.PriceClasses.Price,
                Active       = film.Active,
                PriceClassId = film.PriceClasses.Id
            };

            filmVM.JsonSerialize = JsonConvert.SerializeObject(filmVM);
            return(filmVM);
        }
 public string EditFilm(FilmVM newFilm, int priceClassId, int[] genreIds)
 {
     throw new NotImplementedException();
 }