public async Task <ActionResult> GetSongInfo(IFormCollection collection, string pk = "", string rk = "")
        {
            var newSong = new NewSong();

            if (!string.IsNullOrWhiteSpace(collection["YTLink"]))
            {
                var ytLink = collection["YTLink"].ToString();
                var song   = await YouTubeApiService.GetSongInfo(configuration, ytLink);

                var songExists = false;

                var youTubeLink = collection["YTLink"].ToString();
                var videoID     = YouTubeApiService.GetYouTubeVideoID(youTubeLink);

                var table = TableStorageService.ConnectToTable(Constants.AllSongsTableName);
                TableContinuationToken tableContinuationToken = null;

                do
                {
                    var tableQuerySegment = await table.ExecuteQuerySegmentedAsync(new TableQuery <AllSongs>(), tableContinuationToken);

                    var songs = tableQuerySegment.Results;
                    songExists             = songs.Any(x => x.YouTubeLink.Contains(videoID) && x.RowKey != rk);
                    tableContinuationToken = tableQuerySegment.ContinuationToken;
                } while (tableContinuationToken != null && !songExists);

                if (!songExists)
                {
                    newSong.OriginalGame  = song.OriginalGame;
                    newSong.OriginalTitle = song.OriginalTitle;
                    newSong.YouTubeLink   = song.YouTubeLink;
                    newSong.Channel       = song.Channel;
                    newSong.Duration      = song.Duration;
                    newSong.YTLink        = ytLink;
                    newSong.PartitionKey  = pk;
                    newSong.RowKey        = rk;

                    //TODO: tags para dos casos: new y edit
                }
                else
                {
                    return(RedirectToAction("Index", "Playlist", new { ac = "The YouTube video URL already exists in the playlist!", type = "danger" }));
                }
            }

            if (string.IsNullOrWhiteSpace(pk) || string.IsNullOrWhiteSpace(rk))
            {
                return(View("Create", newSong));
            }
            else
            {
                return(View("Edit", newSong));
            }
        }
Exemple #2
0
        public async Task <ActionResult> RemoveRequest(string pk, string rk)
        {
            var song = await TableStorageService.RetrieveEntity <AzureTableSong>(pk, rk, Constants.QueueTableName);

            if (song != null)
            {
                await TableStorageService.DeleteEntity(song, Constants.QueueTableName);

                var updateSong = await TableStorageService.RetrieveEntity <AllSongs>(pk, rk, Constants.AllSongsTableName);

                updateSong.PartitionKey      = song.PartitionKey;
                updateSong.RowKey            = song.RowKey;
                updateSong.Counter           = song.Counter - 1;
                updateSong.LastTimeRequested = DateTime.UtcNow.AddHours(-2);

                await TableStorageService.MergeEntity(updateSong, Constants.AllSongsTableName);

                var queueSongs = await TableStorageService.RetrieveAllEntities <AzureTableSong>(Constants.QueueTableName);

                var table = TableStorageService.ConnectToTable(Constants.QueueTableName);

                if (queueSongs.Count > 0)
                {
                    queueSongs = queueSongs.OrderBy(x => x.Position).ToList();
                    var position = 1;

                    foreach (var queueSong in queueSongs)
                    {
                        queueSong.Position = position;
                        queueSong.Counter  = queueSong.Counter - 1;

                        var replace = TableOperation.Merge(queueSong);
                        await table.ExecuteAsync(replace);

                        position++;
                    }
                }

                return(RedirectToAction("Index", "SongsQueue", new { ac = "The song was removed from the queue.", type = "success" }));
            }
            else
            {
                return(RedirectToAction("Index", "SongsQueue", new { ac = "Error. The song does not exist.", type = "danger" }));
            }
        }
Exemple #3
0
        public async Task <ActionResult> Save(IFormCollection collection, int items, string sortOrder, string currentFilter, string searchString, int?page, int?pageSize, string currentSort, bool?checkNew)
        {
            var ac   = string.Empty;
            var type = string.Empty;

            try
            {
                var table = TableStorageService.ConnectToTable(Constants.AllSongsTableName);

                for (int i = 0; i < items; i++)
                {
                    string pk    = collection[$"pk_{i}"];
                    string rk    = collection[$"rk_{i}"];
                    string title = collection[$"Title_{i}"];
                    string game  = collection[$"Game_{i}"];

                    var operation = TableOperation.Retrieve <AllSongs>(pk, rk);

                    var song = (AllSongs)(await table.ExecuteAsync(operation)).Result;
                    song.ETag          = "*";
                    song.OriginalTitle = title;
                    song.Title         = StringFunctions.ReplaceChars(title.ToLower());
                    song.OriginalGame  = game;
                    song.Game          = game.ToLower();

                    var replace = TableOperation.Merge(song);
                    await table.ExecuteAsync(replace);
                }

                ac   = "The playlist was successfully edited!";
                type = "success";
            }
            catch (Exception ex)
            {
                ac   = "There was an error updating the entries!";
                type = "danger";
            }

            // falta mandar los parametros
            //It is recommended you return the view is ModelState is not valid so that the user can correct any errors.
            return(RedirectToAction("Index", "QuickEdit", new { ac, type, sortOrder, currentFilter, searchString, page, pageSize, currentSort, checkNew }));
        }
Exemple #4
0
        // GET: SongsInfo
        public async Task <ActionResult> Index()
        {
            var songsInfo = new SongsInfo();
            var table     = TableStorageService.ConnectToTable(Constants.CurrentSongTableName);
            var queryCS   = await table.ExecuteQuerySegmentedAsync(new TableQuery <AzureTableSong>(), null);

            songsInfo.CurrentSong = queryCS.Results.FirstOrDefault();

            var bytes = await ImageService.DownloadImage(songsInfo.CurrentSong.OriginalGame);

            if (bytes == null)
            {
                bytes = await ImageService.DownloadImage(Constants.DefaultBlobImage);
            }

            songsInfo.GamePicture = bytes;

            var songs = await TableStorageService.RetrieveAllEntities <AzureTableSong>(Constants.QueueTableName);

            songsInfo.SongsQueue = songs.OrderBy(x => x.Position);
            return(View(songsInfo));
        }
        public async Task <ActionResult> RequestSong(string pk, string rk)
        {
            var tableSong = TableStorageService.ConnectToTable("CurrentSong");
            var queryCS   = await tableSong.ExecuteQuerySegmentedAsync(new TableQuery <AzureTableSong>(), null);

            var currentSong = queryCS.Results.FirstOrDefault();

            if (currentSong.RowKey != rk)
            {
                var song = await TableStorageService.RetrieveEntity <AllSongs>(pk, rk, Constants.AllSongsTableName);

                if (song != null)
                {
                    var position = 1;

                    var channel = CookieService.Get(Request, Constants.ChannelCookieName);

                    var user = await userManager.GetUserAsync(User);

                    var userName = user.UserName;

                    var isModOwner = await userManager.IsInRoleAsync(user, "Moderator") ||
                                     await userManager.IsInRoleAsync(user, "Owner");

                    var queueSongs = await TableStorageService.RetrieveAllEntities <AzureTableSong>(Constants.QueueTableName);

                    if (queueSongs.Count > 0)
                    {
                        if (!isModOwner)
                        {
                            var friend = await TableStorageService.RetrieveEntity <Friends>(channel, "1", Constants.FriendsTableName);

                            if (friend != null)
                            {
                                var friendRequests = queueSongs.Count(x => x.RequestedById == channel);
                                if (friendRequests > 1)
                                {
                                    return(RedirectToAction("Index", "Playlist", new { ac = "Error. You already have two song requests on the queue.", type = "danger" }));
                                }
                            }
                            else
                            {
                                var userExists = queueSongs.Any(x => x.RequestedById == channel);
                                if (userExists)
                                {
                                    return(RedirectToAction("Index", "Playlist", new { ac = "Error. You already have a song request on the queue.", type = "danger" }));
                                }
                            }
                        }

                        var songExists = queueSongs.Any(x => x.RowKey == rk);
                        if (songExists)
                        {
                            return(RedirectToAction("Index", "Playlist", new { ac = $"Error. That song is already on the queue.", type = "danger" }));
                        }

                        queueSongs = queueSongs.OrderBy(x => x.Position).ToList();
                        position   = queueSongs.Last().Position + 1;
                    }

                    var lastRequest = (song.LastTimeRequested.HasValue)
                        ? MathFunctions.GetCooldownMinutes(song.LastTimeRequested.Value) : 100;

                    if (isModOwner || lastRequest > 60)
                    {
                        var updateSong = new AllSongs()
                        {
                            PartitionKey      = song.PartitionKey,
                            RowKey            = song.RowKey,
                            Counter           = song.Counter0 + 1,
                            LastTimeRequested = DateTime.UtcNow
                        };

                        await TableStorageService.MergeEntity(updateSong, Constants.AllSongsTableName);

                        var newSongQueue = new AzureTableSong()
                        {
                            PartitionKey      = song.PartitionKey,
                            RowKey            = song.RowKey,
                            Title             = song.OriginalTitle,
                            OriginalGame      = song.OriginalGame,
                            LowerCaseTitle    = song.Title,
                            Channel           = song.Channel,
                            VideoId           = song.YouTubeLink.Replace("https://youtu.be/", string.Empty),
                            YouTubeLink       = song.YouTubeLink,
                            Duration          = song.Duration,
                            TotalTime         = 0,
                            RecentlyAdded     = song.RecentlyAdded,
                            RequestedBy       = userName,
                            RequestedById     = channel,
                            Counter           = song.Counter0 + 1,
                            Likes             = song.Likes0,
                            Position          = position,
                            LastTimeRequested = song.LastTimeRequested
                        };

                        await TableStorageService.InsertEntity(newSongQueue, Constants.QueueTableName);

                        return(RedirectToAction("Index", "SongsQueue", new { ac = $"The song was added to the queue in position {position}.", type = "success" }));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Playlist", new { ac = $"Error. That song is in cooldown. Please wait {60 - lastRequest} minutes to request it again.", type = "danger" }));
                    }
                }

                return(RedirectToAction("Index", "Playlist", new { ac = "Error. The song does not exist.", type = "danger" }));
            }
            else
            {
                return(RedirectToAction("Index", "Playlist", new { ac = "Error. The song is currently playing.", type = "danger" }));
            }
        }
        public async Task <ActionResult> Create(IFormCollection collection, AllSongs song)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var youTubeLink = collection["YouTubeLink"].ToString();

                    if (!string.IsNullOrWhiteSpace(youTubeLink))
                    {
                        var table = TableStorageService.ConnectToTable(Constants.AllSongsTableName);
                        TableContinuationToken tableContinuationToken = null;
                        int maxKey     = 0;
                        var songExists = false;

                        do
                        {
                            var tableQuerySegment = await table.ExecuteQuerySegmentedAsync(new TableQuery <AllSongs>(), tableContinuationToken);

                            var songs = tableQuerySegment.Results;

                            var max = songs.Max(x => x.RowKey);
                            maxKey = Math.Max(maxKey, int.Parse(max));

                            tableContinuationToken = tableQuerySegment.ContinuationToken;
                            songExists             = songs.Any(x => x.YouTubeLink == youTubeLink);
                        }while (tableContinuationToken != null && !songExists);

                        if (!songExists)
                        {
                            var    i     = (maxKey + 1).ToString("D4");
                            string title = collection["OriginalTitle"];
                            string game  = collection["OriginalGame"];

                            var newSong = new AllSongs()
                            {
                                RowKey            = i,
                                OriginalGame      = game,
                                Game              = game.ToLower(),
                                OriginalTitle     = title,
                                PartitionKey      = StringFunctions.ReplaceChars(game.ToLower()),
                                Title             = StringFunctions.ReplaceChars(title.ToLower()),
                                Channel           = collection["Channel"],
                                Duration          = collection["Duration"],
                                YouTubeLink       = youTubeLink,
                                LastTimeRequested = null,
                                RecentlyAdded     = "✓",
                                Counter           = 0,
                                Likes             = 0
                            };

                            await TableStorageService.InsertEntity(newSong, Constants.AllSongsTableName);

                            //TODO: add tags from webpage

                            return(RedirectToAction("Index", "Playlist", new { ac = "The song was successfully added!", type = "success" }));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Playlist", new { ac = "The YouTube video URL already exists in the playlist!", type = "danger" }));
                        }
                    }
                    else
                    {
                        return(View());
                    }
                }
                else
                {
                    return(View());
                }
            }
            catch (Exception)
            {
                return(View());
            }
        }
Exemple #7
0
        public async Task <ViewResult> Index(string sortOrder, string currentFilter, string searchString, int?page, int?pageSize, string currentSort)
        {
            ViewBag.CurrentSort = sortOrder ?? currentSort;
            sortOrder           = ViewBag.CurrentSort;

            ViewBag.SortTitle    = (string.IsNullOrEmpty(sortOrder) ? "title_desc" : "");
            ViewBag.SortDuration = ((sortOrder == "duration") ? "duration_desc" : "duration");
            ViewBag.SortArtist   = ((sortOrder == "artist") ? "artist_desc" : "artist");

            var currentPageSize = pageSize.HasValue ? pageSize.Value : 10;

            ViewBag.CurrentPageSize = currentPageSize;

            if (!string.IsNullOrWhiteSpace(searchString))
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var playlist = new List <MySongs>();

            var table = TableStorageService.ConnectToTable(Constants.MySongsTableName);
            TableContinuationToken tableContinuationToken = null;

            var channel = CookieService.Get(Request, Constants.ChannelCookieName);

            var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, channel);
            var query  = new TableQuery <MySongs>().Where(filter);

            do
            {
                var tableQuerySegment = await table.ExecuteQuerySegmentedAsync(query, tableContinuationToken);

                playlist.AddRange(tableQuerySegment.Results);
                tableContinuationToken = tableQuerySegment.ContinuationToken;
            }while (tableContinuationToken != null);

            var source = string.IsNullOrWhiteSpace(searchString)
                ? playlist
                         //: playlist.Where(x => x.Title.Contains(searchString));
                : EasyCustomSearch.SearchSong(searchString, playlist);

            switch (sortOrder)
            {
            case "title_desc":
                source = source.OrderByDescending(x => x.Title);
                break;

            case "duration":
                source = source.OrderBy(x => x.Duration).ThenBy(x => x.Title);
                break;

            case "duration_desc":
                source = source.OrderByDescending(x => x.Duration).ThenBy(x => x.Title);
                break;

            case "artist":
                source = source.OrderBy(x => x.Channel).ThenBy(x => x.Title);
                break;

            case "artist_desc":
                source = source.OrderByDescending(x => x.Channel).ThenBy(x => x.Title);
                break;

            default:
                source = source.OrderBy(x => x.Title);
                break;
            }

            var pageNumber = page ?? 1;

            return(View(new MyPageSongs()
            {
                MySongs = source.ToPagedList(pageNumber, currentPageSize)
            }));
        }