Example #1
0
        public async Task <IActionResult> Create(SongsCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                int seconds = (model.DurationMinutes * 60 + model.DurationSeconds);

                if (seconds <= 0)
                {
                    model.ErrorMessages.Add("Duration should be at least 1 second");
                    CreateSelectListForAlbums(model.AlbumId);
                    return(View(model));
                }

                var song = new Song()
                {
                    Id              = Guid.NewGuid().ToString(),
                    Name            = model.Name,
                    AlbumId         = model.AlbumId,
                    DurationSeconds = (model.DurationMinutes * 60 + model.DurationSeconds),
                    PerformerId     = _dbContext.Albums.Find(model.AlbumId).PerformerId
                };

                await _dbContext.Songs.AddAsync(song);

                await _dbContext.SaveChangesAsync();

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

            CreateSelectListForAlbums(model.AlbumId);

            return(View(model));
        }
Example #2
0
        public IActionResult Create()
        {
            var model = new SongsCreateViewModel();

            CreateSelectListForAlbums();
            return(View(model));
        }
        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));
        }
        private string getVideoId(SongsCreateViewModel model)
        {
            var    linkArray = model.Url.ToCharArray();
            string videoId   = "";

            for (int i = linkArray.Length - 1; i >= linkArray.Length - 11; i--)
            {
                videoId += linkArray[i];
            }
            string videoIdReversed = "";

            for (int i = videoId.Length - 1; i >= 0; i--)
            {
                videoIdReversed += videoId[i];
            }


            return(videoIdReversed);
        }
        // GET: Songs/Create
        public IActionResult Create()
        {
            SongsCreateViewModel model = new SongsCreateViewModel();

            return(View(model));
        }