public IActionResult Upload(UploadTubeBindingModel model) { if (!this.User.IsAuthenticated) { return(new RedirectResult("/")); } if (!this.IsValidModel(model)) { this.Model["message"] = this.GetErrors(); return(this.View()); } var userId = this.Request.Session.Get <int>(SessionStore.CurrentUserIdKey); Tube tube = new Tube(); tube.Title = model.Title; tube.Author = model.Author; tube.YouTubeId = model.YouTubeId; tube.Description = model.Description; tube.UploaderId = userId; using (var db = new MyTubeDbContext()) { db.Tubes.Add(tube); db.SaveChanges(); } this.Model["message"] = "<p>The Tube Successfully added</p>"; return(this.View()); }
public IActionResult Upload(UploadTubeBindingModel model) { if (!this.IsValidModel(model)) { return(this.BuildErrorView()); } using (this.Context) { var user = this.Context.Users.FirstOrDefault(u => u.Username == this.User.Name); var youtubeId = GetTubeId(model.YouTubeLink); if (string.IsNullOrWhiteSpace(youtubeId)) { return(this.BuildErrorView()); } var tube = new Tube() { Author = model.Author, Title = model.Title, Description = model.Description, YoutubeId = youtubeId, UserId = user.Id, }; this.Context.Tubes.Add(tube); this.Context.SaveChanges(); return(this.RedirectToAction($"/tubes/details?id={tube.Id}")); } }
public IActionResult Upload(UploadTubeBindingModel model) { if (this.SessionUser.IsAuthenticated) { if (!this.IsValidModel(model)) { this.Model.Data[ErrorKey] = InvalidTubeInput; return(View()); } else { string username = this.SessionUser.Name; using (this.Context) { User uploader = this.Context.Users.FirstOrDefault(x => x.Username == username); int linkLength = model.YoutubeLink.Length; string youTubeId = model.YoutubeLink.Substring(linkLength - YouTubeIdLength); Tube tubeToAdd = new Tube() { Title = model.Title, Author = model.Author, YouTubeId = youTubeId, UploaderId = uploader.Id, Uploader = uploader, Description = model.Description }; this.Context.Tubes.Add(tubeToAdd); this.Context.SaveChanges(); } } return(RedirectToHome()); } return(RedirectToLogin()); }