//TODO:File upload public ActionResult CreateNewTorrent(TorrentViewModel torrentModel) { if (ModelState.IsValid && torrentModel != null) { Torrent torrentEntity = new Torrent() { Title = torrentModel.Title, FileLink = torrentModel.FileLink, CatalogueId = int.Parse(torrentModel.CatalogueName), Size = torrentModel.Size, DateCreated = DateTime.Now, Description = torrentModel.Description }; Data.Torrents.Add(torrentEntity); Data.SaveChanges(); return RedirectToAction("Index"); } else { var cataloges = from catalog in Data.Catalogues.All().ToList() select new SelectListItem() { Text = catalog.Name, Value = catalog.Id.ToString() }; ViewData["catalogs"] = cataloges.ToList(); return View("CreateTorrent"); } }
public ActionResult SaveEditedTorrent(TorrentViewModel torrentModel) { if (ModelState.IsValid && torrentModel != null) { Torrent torrentEntity = Data.Torrents.All().FirstOrDefault(t => t.Id == torrentModel.Id); torrentEntity.Title = torrentModel.Title; torrentEntity.FileLink = torrentModel.FileLink; torrentEntity.CatalogueId = int.Parse(torrentModel.CatalogueName); torrentEntity.Size = torrentModel.Size; torrentEntity.DateCreated = DateTime.Now; torrentEntity.Description = torrentModel.Description; if (torrentModel.CategoryToAdd!=null) { int categoryId = int.Parse(torrentModel.CategoryToAdd); var existingCategory = torrentEntity.Categories.FirstOrDefault(c => c.Id == categoryId); if (existingCategory == null) { var newCategory = Data.Categories.All().FirstOrDefault(c => c.Id == categoryId); torrentEntity.Categories.Add(newCategory); } } Data.SaveChanges(); return RedirectToAction("Index"); } else { var cataloges = from catalog in Data.Catalogues.All().ToList() select new SelectListItem() { Text = catalog.Name, Value = catalog.Id.ToString() }; var categories = (from catalog in Data.Categories.All().ToList() select new SelectListItem() { Text = catalog.Name, Value = catalog.Id.ToString() }).ToList(); categories.Add(new SelectListItem() { Text = null, Value = null, Selected = true }); ViewData["categories"] = categories.ToList(); ViewData["catalogs"] = cataloges.ToList(); return View("EditTorrent",torrentModel); } }