private mam_info EntityMapping(mam_info newModel, mam_info oldModel)
 {
     newModel.Description = newModel.Description ?? oldModel.Description;
     newModel.Manufacturers = newModel.Manufacturers ?? oldModel.Manufacturers;
     newModel.Director = newModel.Director ?? oldModel.Director;
     newModel.ReleaseYear = newModel.ReleaseYear ?? oldModel.ReleaseYear;
     newModel.ExpiryYear = newModel.ExpiryYear ?? oldModel.ExpiryYear;
     newModel.Episodes = newModel.Episodes ?? oldModel.Episodes;
     newModel.Price = newModel.Price ?? oldModel.Price;
     newModel.NumberOfDisplays = newModel.NumberOfDisplays != newModel.NumberOfDisplays ? newModel.NumberOfDisplays : oldModel.NumberOfDisplays;
     newModel.PackageIds = newModel.PackageIds ?? string.Empty;
     newModel.Cast = newModel.Cast ?? oldModel.Cast;
     return newModel;
 }
        public ActionResult Create(mam_info model, HttpPostedFileBase PreviewFile, HttpPostedFileBase ThumbnailFile)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var id = int.Parse(this.vodpointsdb.mam_info.Select(a => a.Id).ToList().OrderByDescending(x => int.Parse(x)).First()) + 1;
                    model.Id = id.ToString();
                    model.mam.mam_id = id.ToString();
                    model.Genre = string.Empty;
                    model.SupplierId = 0;
                    model.mam.directory_path = "/thumbnails/";
                    model.mam.video_directory_path = "/videoclips/";
                    model.mam.file_name = string.Empty;
                    model.PackageIds = "0";
                    model.mam.video_name = Request.Form["filename"];

                    var genres = new List<string>();
                    foreach (var genre in model.GenreCheckBox)
                    {
                        if (genre.Checked)
                        {
                            genres.Add(genre.Text);
                        }
                    }

                    var packages = new List<string>();
                    foreach (var package in model.PackageCheckBox)
                    {
                        if (package.Checked)
                        {
                            var pkg = this.vodpointsdb.packages.Where(a => a.Name == package.Text).FirstOrDefault();
                            if (pkg != null)
                            {
                                packages.Add(pkg.Id.ToString());
                            }
                        }
                    }

                    var supplier = this.vodpointsdb.suppliers.Where(a => a.Name == model.SupplierName).FirstOrDefault();
                    if (supplier != null)
                    {
                        model.SupplierId = supplier.Id;
                    }

                    model.PackageIds = string.Join(",", packages.ToArray());
                    model.Genre = string.Join(",", genres.ToArray());

                    var previewFilePath = string.Empty;
                    var thumbnailFilePath = string.Empty;

                    if (PreviewFile != null && PreviewFile.ContentLength > 0)
                    {
                        var videoclipsDirectory = new DirectoryInfo(string.Format("{0}videoclips\\", Server.MapPath(@"\")));
                        previewFilePath = string.Format("{0}\\{1}", videoclipsDirectory, PreviewFile.FileName);
                        PreviewFile.SaveAs(previewFilePath);
                        //model.mam.video_name = PreviewFile.FileName;
                    }

                    if (ThumbnailFile != null && ThumbnailFile.ContentLength > 0)
                    {
                        var thumbnailDirectory = new DirectoryInfo(string.Format("{0}thumbnails\\", Server.MapPath(@"\")));
                        thumbnailFilePath = string.Format("{0}\\{1}", thumbnailDirectory, ThumbnailFile.FileName);
                        ThumbnailFile.SaveAs(thumbnailFilePath);
                        model.mam.file_name = ThumbnailFile.FileName;
                    }

                    if (string.IsNullOrEmpty(model.PackageIds))
                    {
                        model.PackageIds = "0";
                    }

                    this.vodpointsdb.mam_info.Add(model);
                    this.vodpointsdb.SaveChanges();
                }
                catch (Exception ex)
                {
                    System.IO.File.AppendAllText(@"C:\temp\log.txt", ex.Message + Environment.NewLine);
                    System.IO.File.AppendAllText(@"C:\temp\log.txt", ex.StackTrace + Environment.NewLine);
                }
            }

            return RedirectToAction("Index", "Videos");
        }
        public ActionResult Edit(mam_info model, HttpPostedFileBase PreviewFile, HttpPostedFileBase ThumbnailFile)
        {
            if (ModelState.IsValid)
            {
                var previewFilePath = string.Empty;
                var thumbnailFilePath = string.Empty;

                if (PreviewFile != null && PreviewFile.ContentLength > 0)
                {
                    var videoclipsDirectory = new DirectoryInfo(string.Format("{0}videoclips\\", Server.MapPath(@"\")));
                    previewFilePath = string.Format("{0}\\{1}", videoclipsDirectory, PreviewFile.FileName);
                    PreviewFile.SaveAs(previewFilePath);
                }

                if (ThumbnailFile != null && ThumbnailFile.ContentLength > 0)
                {
                    var thumbnailDirectory = new DirectoryInfo(string.Format("{0}thumbnails\\", Server.MapPath(@"\")));
                    thumbnailFilePath = string.Format("{0}\\{1}", thumbnailDirectory, ThumbnailFile.FileName);
                    ThumbnailFile.SaveAs(thumbnailFilePath);
                }

                var mam = this.vodpointsdb.mams.Find(model.mam.mam_id);
                var oldModel = this.vodpointsdb.mam_info.Find(model.Id);
                mam.length = model.mam.length;

                if (PreviewFile == null)
                {
                    if (string.IsNullOrEmpty(mam.video_name))
                    {
                        mam.video_name = string.Empty;
                    }
                }
                else
                {
                    mam.video_name = PreviewFile.FileName;
                }

                if (ThumbnailFile == null)
                {
                    if (string.IsNullOrEmpty(mam.file_name))
                    {
                        mam.file_name = string.Empty;
                    }
                }
                else
                {
                    mam.file_name = ThumbnailFile.FileName;
                }

                model.mam = mam;
                model.MamId = mam.mam_id;
                model = this.EntityMapping(model, oldModel);

                model.Genre = string.Join(",", model.GenreCheckBox.Where(a => a.Checked == true).Select(a => a.Text).ToArray());
                var packageNameArray = model.PackageCheckBox.Where(a => a.Checked == true).Select(a => a.Text);
                var packageIds = new List<string>();
                foreach (var name in packageNameArray)
                {
                    var isFound = this.vodpointsdb.packages.Where(a => a.Name == name).FirstOrDefault();
                    if (isFound != null)
                    {
                        packageIds.Add(isFound.Id.ToString());
                    }
                }

                model.PackageIds = string.Join(",", packageIds.ToArray());

                var supplier = this.vodpointsdb.suppliers.Where(a => a.Name == model.SupplierName).FirstOrDefault();
                if (supplier != null)
                {
                    model.SupplierId = supplier.Id;
                }

                this.vodpointsdb.Entry(oldModel).CurrentValues.SetValues(model);
                this.vodpointsdb.SaveChanges();
            }

            var redirectUrl = Request.Form["UrlReferrer"];
            return Redirect(redirectUrl);
        }