public ActionResult CreateClip(ClipRequestViewModel clipRequest) { if (ModelState.IsValid) { var clip = clipRequest.Clip; Translation translation; Request req = null; // Check if the clip the user is trying to create a request for already exists. var clipToCheckFor = _unitOfWork.ClipRepository.Get() .Where(c => c.Title == clip.Title) .Where(c => c.ReleaseYear == clip.ReleaseYear) .SingleOrDefault(); // If the clip doesn't exists, insert the clip and request to the database. if (clipToCheckFor == null) { clipRequest.Clip.Link = "//www.youtube.com/embed/" + YoutubeParser.parseLink(clipRequest.Clip.Link); _unitOfWork.ClipRepository.Insert(clipRequest.Clip); _unitOfWork.RequestRepository.Insert(clipRequest.Request); _unitOfWork.Save(); TempData["UserMessage"] = "Beiðnin var stofnuð"; return(RedirectToAction("Index", "Request")); } // If the clip exists we need to check if it already has the translation or the reques else if ((translation = _unitOfWork.TranslationRepository.Get() .Where(t => t.MediaID == clipToCheckFor.ID) .Where(t => t.LanguageID == clipRequest.Request.LanguageID) .SingleOrDefault()) == null && (req = _unitOfWork.RequestRepository.Get() .Where(r => r.MediaID == clipToCheckFor.ID) .Where(r => r.LanguageID == clipRequest.Request.LanguageID) .SingleOrDefault()) == null) { // If not we insert the request. var request = clipRequest.Request; request.MediaID = clipToCheckFor.ID; _unitOfWork.RequestRepository.Insert(request); _unitOfWork.Save(); TempData["UserMessage"] = "Beiðnin var stofnuð"; return(RedirectToAction("Index", "Request")); } clipRequest.Clip = clipToCheckFor; if (req != null) { ViewBag.Errormsg = "Þessi beiðni er nú þegar til."; ViewBag.ReqExist = true; } else { ViewBag.Errormsg = "Þessi þýðing er nú þegar til."; ViewBag.ReqExist = false; ViewBag.TranslationID = translation.ID; } } ViewBag.SubCategories = new SelectList(_unitOfWork.CategoryRepository.Get(), "ID", "Name", clipRequest.Clip.CategoryID); ViewBag.Languages = new SelectList(_unitOfWork.LanguageRepository.Get(), "ID", "Name"); return(View("RequestClip")); }
public ActionResult CreateClip(ClipTranslationViewModel clipTranslation, HttpPostedFileBase file) { if (ModelState.IsValid) { var clip = clipTranslation.Clip; var clipToCheckFor = _unitOfWork.ClipRepository.Get() .Where(c => c.Title == clip.Title) .Where(c => c.ReleaseYear == clip.ReleaseYear) .SingleOrDefault(); if (clipToCheckFor == null) { clip.Translations = new List <Translation>(); if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); var translation = SubtitleParser.Parse(path, "srt"); translation.LanguageID = clipTranslation.LanguageID; clip.Translations.Add(translation); } else { clip.Translations.Add(new Translation { LanguageID = clipTranslation.LanguageID }); } clip.Link = "//www.youtube.com/embed/" + YoutubeParser.parseLink(clip.Link); _unitOfWork.ClipRepository.Insert(clip); var userid = User.Identity.GetUserId(); var user = _unitOfWork.UserRepository.GetByID(userid); user.NewTranslations++; _unitOfWork.Save(); var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First(); return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID })); } else if (_unitOfWork.TranslationRepository.Get() .Where(t => t.MediaID == clipToCheckFor.ID) .Where(t => t.LanguageID == clipTranslation.LanguageID) .SingleOrDefault() == null) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); var translation = SubtitleParser.Parse(path, "srt"); translation.LanguageID = clipTranslation.LanguageID; clipToCheckFor.Translations.Add(translation); } else { clipToCheckFor.Translations.Add(new Translation { LanguageID = clipTranslation.LanguageID }); } _unitOfWork.ClipRepository.Update(clipToCheckFor); var userid = User.Identity.GetUserId(); var user = _unitOfWork.UserRepository.GetByID(userid); user.NewTranslations++; _unitOfWork.Save(); HasRequest(clipToCheckFor.ID, clipTranslation.LanguageID); var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First(); return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID })); } ViewBag.Errormsg = "Þessi þýðing er nú þegar til."; } ViewBag.CategoryID = new SelectList(_unitOfWork.CategoryRepository.Get(), "ID", "Name", clipTranslation.Clip.CategoryID); ViewBag.LanguageID = new SelectList(_unitOfWork.LanguageRepository.Get(), "ID", "Name"); return(View("CreateClip")); }