public ActionResult Create([Bind(Include = "Track,Artists,Genres")] TrackEditTemplate template)
 {
     if (ModelState.IsValid)
     {
         trackRepository.CreateTrack(template.Track);
         trackRepository.UpdateArtistsOfTrack(template.Track.Id, template.Artists);
         trackRepository.UpdateGenresOfTrack(template.Track.Id, template.Genres);
         return(RedirectToAction("Index"));
     }
     ViewBag.Artists = new MultiSelectList(artistRepository.GetAllArtists(), "Id", "Name", template.Artists);
     ViewBag.Genres  = new MultiSelectList(genreRepository.GetAllGenres(), "Id", "Name", template.Genres);
     return(View(template));
 }
        /// <summary>
        /// saveTrack button action
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// Save inscribed data as a new Track in database
        /// </remarks>
        private void saveTrack_Click(object sender, RoutedEventArgs e)
        {
            var resultSearch = new Api();

            Api.SearchApiResult result;
            if (trackNameBox.Text.Length != 0 && artistSelect.SelectedItem != null)
            {
                if (TrackAlreadyExist(trackNameBox.Text, artistSelect.Text))
                {
                    trackStatusLabel.Content = "Track already exist";
                }
                else
                {
                    IArtistRepository artistRepo = new ArtistRepository();
                    Track             track      = new Track()
                    {
                        Name = trackNameBox.Text, ArtistId = artistRepo.GetArtist(artistSelect.Text).ArtistId
                    };
                    try
                    {
                        result = resultSearch.FindID(trackNameBox.Text, "video");
                        youtubeTrackPathBox.Text = "https://www.youtube.com/watch?v=" + result.ID;
                        track.Views = result.Count;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    if (youtubeTrackPathBox.Text.Length != 0)
                    {
                        track.YoutubePath = youtubeTrackPathBox.Text;
                    }
                    ITrackRepository repo = new TrackRepository();
                    repo.CreateTrack(track);
                    trackStatusLabel.Content = "Saved";
                }
            }
            else
            {
                trackStatusLabel.Content = "Databox empty";
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var track = new TrackAuth(user.Id, Guid.NewGuid().ToString())
            {
                description = Input.Description,
                is_private  = Input.IsPrivate,
                name        = Input.Name,
                tags        = string.IsNullOrWhiteSpace(Input.Tags) ? null : string.Join(",", Tools.ValidateTags(Input.Tags.Split(',').ToList()))
            };

            // generate small thumb
            if (Input.UploadTrackImage != null)
            {
                using (MemoryStream uploadStream = new MemoryStream())
                {
                    await Input.UploadTrackImage.CopyToAsync(uploadStream);

                    byte[] data = uploadStream.ToArray();

                    using (MemoryStream input = new MemoryStream(data))
                    {
                        using (MemoryStream output = new MemoryStream())
                        {
                            Images.CropSquare(150, input, output);

                            await BlobRepository.UploadFileAsync(BlobRepository.TracksContainer, output.ToArray(), track.RowKey + "/thumb");
                        }
                    }

                    using (MemoryStream input = new MemoryStream(data))
                    {
                        using (MemoryStream output = new MemoryStream())
                        {
                            Images.CropSquare(32, input, output);

                            await BlobRepository.UploadFileAsync(BlobRepository.TracksContainer, output.ToArray(), track.RowKey + "/thumb_mini");
                        }
                    }

                    track.has_image = true;
                }
            }

            // create track
            TrackAuth createdTrack = await TrackRepository.CreateTrack(track);

            FollowRepository.InsertOrReplaceTrackFollow(new TrackFollow()
            {
                track_id                  = createdTrack.RowKey,
                user_id                   = user.Id,
                feed_follow_type          = "none",
                notifications_follow_type = "none"
            });

            _logger.LogInformation($"Track with ID '{createdTrack.RowKey}' has been created by '{user.Id}'.");
            return(RedirectToPage("./Index"));
        }