Exemple #1
0
        private void AddSongHit(IDatabaseContext <Song> session, Song song, string hostname)
        {
            if (!PermissionContext.IsLoggedIn && string.IsNullOrEmpty(hostname))
            {
                return;
            }

            var agentNum = PermissionContext.IsLoggedIn ? PermissionContext.LoggedUserId : hostname.GetHashCode();

            if (agentNum == 0)
            {
                return;
            }

            using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted)) {
                var songId = song.Id;
                var isHit  = session.Query <SongHit>().Any(h => h.Song.Id == songId && h.Agent == agentNum);

                if (!isHit)
                {
                    var hit = new SongHit(song, agentNum);
                    session.Save(hit);
                }

                try {
                    tx.Commit();
                } catch (TransactionException x) {
                    log.Error(x, "Unable to save song hit");
                }
            }
        }
Exemple #2
0
        public SongDetailsContract GetSongDetails(int songId, int albumId, string hostname)
        {
            return(HandleQuery(session => {
                var song = session.Load <Song>(songId);
                var contract = new SongDetailsContract(song, PermissionContext.LanguagePreference);
                int agentNum = 0;
                var user = PermissionContext.LoggedUser;

                if (user != null)
                {
                    var rating = session.Query <FavoriteSongForUser>()
                                 .FirstOrDefault(s => s.Song.Id == songId && s.User.Id == user.Id);

                    contract.UserRating = (rating != null ? rating.Rating : SongVoteRating.Nothing);

                    agentNum = user.Id;
                }
                else if (!string.IsNullOrEmpty(hostname))
                {
                    agentNum = hostname.GetHashCode();
                }

                contract.CommentCount = session.Query <SongComment>().Count(c => c.Song.Id == songId);
                contract.LatestComments = session.Query <SongComment>()
                                          .Where(c => c.Song.Id == songId)
                                          .OrderByDescending(c => c.Created).Take(3).ToArray()
                                          .Select(c => new CommentContract(c)).ToArray();
                contract.Hits = session.Query <SongHit>().Count(h => h.Song.Id == songId);

                if (albumId != 0)
                {
                    var album = session.Load <Album>(albumId);

                    var track = album.Songs.FirstOrDefault(s => song.Equals(s.Song));

                    if (track != null)
                    {
                        contract.AlbumId = albumId;

                        var previousIndex = album.PreviousTrackIndex(track.Index);
                        var previous = album.Songs.FirstOrDefault(s => s.Index == previousIndex);
                        contract.PreviousSong = previous != null && previous.Song != null ? new SongInAlbumContract(previous, LanguagePreference, false) : null;

                        var nextIndex = album.NextTrackIndex(track.Index);
                        var next = album.Songs.FirstOrDefault(s => s.Index == nextIndex);
                        contract.NextSong = next != null && next.Song != null ? new SongInAlbumContract(next, LanguagePreference, false) : null;
                    }
                }

                if (song.Deleted)
                {
                    var mergeEntry = GetMergeRecord(session, songId);
                    contract.MergedTo = (mergeEntry != null ? new SongContract(mergeEntry.Target, LanguagePreference) : null);
                }

                if (agentNum != 0)
                {
                    using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted)) {
                        var isHit = session.Query <SongHit>().Any(h => h.Song.Id == songId && h.Agent == agentNum);

                        if (!isHit)
                        {
                            var hit = new SongHit(song, agentNum);
                            session.Save(hit);
                        }

                        tx.Commit();
                    }
                }

                return contract;
            }));
        }