/// <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!"); } }