public async Task <IActionResult> Create(SongsCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var    youtube   = YouTube.Default;
                var    video     = youtube.GetVideo(model.Url);
                string videoId   = getVideoId(model);
                string videoName = video.Title.Substring(0, video.Title.Length - 10);
                string videoLink = "https://www.youtube.com/embed/" + videoId;
                Song   song      = new Song
                {
                    Title  = videoName,
                    Rating = 0,
                    Url    = videoLink,
                };



                if (!SongExists(song.Url))
                {
                    _context.Add(song);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    TempData["Fail"] = "Song already exist";
                    return(RedirectToAction(nameof(Create)));
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public IActionResult Create(Song song)
        {
            if (ModelState.IsValid)
            {
                Db.Add(song);
                Db.SaveChanges();
            }

            return(View("Index"));
        }