コード例 #1
0
        public AlbumDetailsContract GetAlbumDetails(int id, string hostname)
        {
            return(HandleQuery(session => {
                var album = session.Load <Album>(id);
                var contract = new AlbumDetailsContract(album, PermissionContext.LanguagePreference);

                var user = PermissionContext.LoggedUser;

                if (user != null)
                {
                    var albumForUser = session.Query <AlbumForUser>()
                                       .FirstOrDefault(a => a.Album.Id == id && a.User.Id == user.Id);

                    contract.AlbumForUser = (albumForUser != null ? new AlbumForUserContract(albumForUser, PermissionContext.LanguagePreference) : null);
                }

                contract.CommentCount = session.Query <AlbumComment>().Count(c => c.Album.Id == id);
                contract.LatestComments = session.Query <AlbumComment>()
                                          .Where(c => c.Album.Id == id)
                                          .OrderByDescending(c => c.Created).Take(3).ToArray()
                                          .Select(c => new CommentContract(c)).ToArray();
                contract.Hits = session.Query <AlbumHit>().Count(h => h.Album.Id == id);

                if (user != null || !string.IsNullOrEmpty(hostname))
                {
                    var agentNum = (user != null ? user.Id : hostname.GetHashCode());

                    using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted)) {
                        var isHit = session.Query <AlbumHit>().Any(h => h.Album.Id == id && h.Agent == agentNum);

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

                        tx.Commit();
                    }
                }


                return contract;
            }));
        }
コード例 #2
0
        /// <summary>
        /// Gets album details, and updates hit count if necessary.
        /// </summary>
        /// <param name="id">Id of the album to be retrieved.</param>
        /// <param name="hostname">
        /// Hostname of the user requestin the album. Used to hit counting when no user is logged in. If null or empty, and no user is logged in, hit count won't be updated.
        /// </param>
        /// <returns>Album details contract. Cannot be null.</returns>
        public AlbumDetailsContract GetAlbumDetails(int id, string hostname)
        {
            return(HandleQuery(session => {
                var album = session.Load <Album>(id);

                var stats = session.Query <Album>()
                            .Where(a => a.Id == id)
                            .Select(a => new {
                    OwnedCount = a.UserCollections.Count(au => au.PurchaseStatus == PurchaseStatus.Owned),
                    WishlistedCount = a.UserCollections.Count(au => au.PurchaseStatus == PurchaseStatus.Wishlisted),
                    CommentCount = a.Comments.Count,
                    Hits = a.Hits.Count
                })
                            .FirstOrDefault();

                if (stats == null)
                {
                    throw new ObjectNotFoundException(id, typeof(Album));
                }

                var contract = new AlbumDetailsContract(album, PermissionContext.LanguagePreference)
                {
                    OwnedCount = stats.OwnedCount,
                    WishlistCount = stats.WishlistedCount,
                    CommentCount = stats.CommentCount,
                    Hits = stats.Hits
                };

                var user = PermissionContext.LoggedUser;

                if (user != null)
                {
                    var albumForUser = session.Query <AlbumForUser>()
                                       .FirstOrDefault(a => a.Album.Id == id && a.User.Id == user.Id);

                    contract.AlbumForUser = (albumForUser != null ? new AlbumForUserContract(albumForUser, PermissionContext.LanguagePreference) : null);
                }

                contract.LatestComments = session.Query <AlbumComment>()
                                          .Where(c => c.Album.Id == id)
                                          .OrderByDescending(c => c.Created)
                                          .Take(3)
                                          .ToArray()
                                          .Select(c => new CommentContract(c))
                                          .ToArray();

                if (album.Deleted)
                {
                    var mergeEntry = GetMergeRecord(session, id);
                    contract.MergedTo = (mergeEntry != null ? new AlbumContract(mergeEntry.Target, LanguagePreference) : null);
                }

                if (user != null || !string.IsNullOrEmpty(hostname))
                {
                    var agentNum = (user != null ? user.Id : hostname.GetHashCode());

                    using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted)) {
                        var isHit = session.Query <AlbumHit>().Any(h => h.Album.Id == id && h.Agent == agentNum);

                        if (!isHit)
                        {
                            var hit = new AlbumHit(album, agentNum);
                            session.Save(hit);

                            try {
                                tx.Commit();
                            } catch (SqlException x) {
                                log.WarnException("Error while committing hit", x);
                            }
                        }
                    }
                }


                return contract;
            }));
        }
コード例 #3
0
        /// <summary>
        /// Gets album details, and updates hit count if necessary.
        /// </summary>
        /// <param name="id">Id of the album to be retrieved.</param>
        /// <param name="hostname">
        /// Hostname of the user requestin the album. Used to hit counting when no user is logged in. If null or empty, and no user is logged in, hit count won't be updated.
        /// </param>
        /// <returns>Album details contract. Cannot be null.</returns>
        public AlbumDetailsContract GetAlbumDetails(int id, string hostname)
        {
            return(HandleQuery(session => {
                var album = session.Load <Album>(id);

                var stats = session.Query <Album>()
                            .Where(a => a.Id == id)
                            .Select(a => new {
                    OwnedCount = a.UserCollections.Count(au => au.PurchaseStatus == PurchaseStatus.Owned),
                    WishlistedCount = a.UserCollections.Count(au => au.PurchaseStatus == PurchaseStatus.Wishlisted),
                    CommentCount = a.Comments.Count,
                    Hits = a.Hits.Count
                })
                            .FirstOrDefault();

                if (stats == null)
                {
                    throw new ObjectNotFoundException(id, typeof(Album));
                }

                var user = PermissionContext.LoggedUser;

                SongVoteRating?GetRatingFunc(Song song)
                {
                    return user != null && song != null ? (SongVoteRating?)session.Query <FavoriteSongForUser>().Where(s => s.Song.Id == song.Id && s.User.Id == user.Id).Select(r => r.Rating).FirstOrDefault() : null;
                }

                var contract = new AlbumDetailsContract(album, PermissionContext.LanguagePreference, PermissionContext, imagePersister, pictureFilePersister, GetRatingFunc)
                {
                    OwnedCount = stats.OwnedCount,
                    WishlistCount = stats.WishlistedCount,
                    CommentCount = stats.CommentCount,
                    Hits = stats.Hits
                };

                if (user != null)
                {
                    var albumForUser = session.Query <AlbumForUser>()
                                       .FirstOrDefault(a => a.Album.Id == id && a.User.Id == user.Id);

                    contract.AlbumForUser = (albumForUser != null ? new AlbumForUserContract(albumForUser, PermissionContext.LanguagePreference) : null);
                }

                contract.LatestComments = session.Query <AlbumComment>()
                                          .Where(c => c.EntryForComment.Id == id)
                                          .OrderByDescending(c => c.Created)
                                          .Take(3)
                                          .ToArray()
                                          .Select(c => new CommentForApiContract(c, userIconFactory))
                                          .ToArray();

                if (album.Deleted)
                {
                    var mergeEntry = GetMergeRecord(session, id);
                    contract.MergedTo = (mergeEntry != null ? new AlbumContract(mergeEntry.Target, LanguagePreference) : null);
                }

                if (user != null || !string.IsNullOrEmpty(hostname))
                {
                    var agentNum = (user != null ? user.Id : hostname.GetHashCode());

                    using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted)) {
                        var isHit = session.Query <AlbumHit>().Any(h => h.Entry.Id == id && h.Agent == agentNum);

                        if (!isHit)
                        {
                            var hit = new AlbumHit(album, agentNum);
                            session.Save(hit);

                            try {
                                tx.Commit();
                            } catch (SqlException x) {
                                session.AuditLogger.SysLog("Error while committing hit: " + x.Message);
                            }
                        }
                    }
                }

                return contract;
            }));
        }
コード例 #4
0
        public AlbumDetailsContract GetAlbumDetails(int id, string hostname)
        {
            return(HandleQuery(session =>
            {
                var album = session.Load <Album>(id);

                var user = PermissionContext.LoggedUser;

                SongVoteRating?GetRatingFunc(Song song)
                {
                    return user != null && song != null ? (SongVoteRating?)session.Query <FavoriteSongForUser>().Where(s => s.Song.Id == song.Id && s.User.Id == user.Id).Select(r => r.Rating).FirstOrDefault() : null;
                }

                var contract = new AlbumDetailsContract(album, PermissionContext.LanguagePreference, PermissionContext, _imageUrlFactory, GetRatingFunc,
                                                        discTypeTag: new EntryTypeTags(session).GetTag(EntryType.Album, album.DiscType))
                {
                    CommentCount = Comments(session).GetCount(id),
                    Hits = session.Query <AlbumHit>().Count(h => h.Entry.Id == id),
                    Stats = GetSharedAlbumStats(session, album)
                };

                if (user != null)
                {
                    var albumForUser = session.Query <AlbumForUser>()
                                       .FirstOrDefault(a => a.Album.Id == id && a.User.Id == user.Id);

                    contract.AlbumForUser = (albumForUser != null ? new AlbumForUserContract(albumForUser, PermissionContext.LanguagePreference) : null);
                }

                contract.LatestComments = session.Query <AlbumComment>()
                                          .WhereNotDeleted()
                                          .Where(c => c.EntryForComment.Id == id)
                                          .OrderByDescending(c => c.Created)
                                          .Take(3)
                                          .ToArray()
                                          .Select(c => new CommentForApiContract(c, _userIconFactory))
                                          .ToArray();

                if (album.Deleted)
                {
                    var mergeEntry = GetMergeRecord(session, id);
                    contract.MergedTo = (mergeEntry != null ? new AlbumContract(mergeEntry.Target, LanguagePreference) : null);
                }

                if (user != null || !string.IsNullOrEmpty(hostname))
                {
                    var agentNum = (user != null ? user.Id : hostname.GetHashCode());

                    using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted))
                    {
                        var isHit = session.Query <AlbumHit>().Any(h => h.Entry.Id == id && h.Agent == agentNum);

                        if (!isHit)
                        {
                            var hit = new AlbumHit(album, agentNum);
                            session.Save(hit);

                            try
                            {
                                tx.Commit();
                            }
                            catch (SqlException x)
                            {
                                session.AuditLogger.SysLog("Error while committing hit: " + x.Message);
                            }
                        }
                    }
                }

                return contract;
            }));
        }