public ArtistDetailsContract GetDetails(int id) { return(HandleQuery(session => { var artist = session.Load(id); var stats = session.Query() .Where(a => a.Id == id) .Select(a => new { CommentCount = a.Comments.Count, FollowCount = a.Users.Count }) .FirstOrDefault(); if (stats == null) { EntityNotFoundException.Throw <Artist>(id); } var contract = new ArtistDetailsContract(artist, LanguagePreference) { CommentCount = stats.CommentCount, FollowCount = stats.FollowCount, SharedStats = GetSharedArtistStats(session, artist), PersonalStats = GetPersonalArtistStats(session, artist) }; if (PermissionContext.LoggedUser != null) { var subscription = session.OfType <ArtistForUser>().Query().FirstOrDefault(s => s.Artist.Id == id && s.User.Id == PermissionContext.LoggedUser.Id); if (subscription != null) { contract.IsAdded = true; contract.EmailNotifications = subscription.EmailNotifications; contract.SiteNotifications = subscription.SiteNotifications; } } var relations = (new ArtistRelationsQuery(session, LanguagePreference)).GetRelations(artist, ArtistRelationsFields.All); contract.LatestAlbums = relations.LatestAlbums; contract.TopAlbums = relations.PopularAlbums; contract.LatestSongs = relations.LatestSongs; contract.TopSongs = relations.PopularSongs; contract.LatestComments = session.OfType <ArtistComment>().Query() .Where(c => c.Artist.Id == id) .OrderByDescending(c => c.Created) .Take(3) .ToArray() .Select(c => new CommentContract(c)).ToArray(); if (artist.Deleted) { var mergeEntry = GetMergeRecord(session, id); contract.MergedTo = (mergeEntry != null ? new ArtistContract(mergeEntry.Target, LanguagePreference) : null); } return contract; })); }
public ArtistDetailsContract GetDetails(int id) { return(HandleQuery(session => { var artist = session.Load(id); var stats = session.Query() .Where(a => a.Id == id) .Select(a => new { CommentCount = a.Comments.Count, }) .FirstOrDefault(); if (stats == null) { EntityNotFoundException.Throw <Artist>(id); } var contract = new ArtistDetailsContract(artist, LanguagePreference) { CommentCount = stats.CommentCount, SharedStats = GetSharedArtistStats(session, artist), PersonalStats = GetPersonalArtistStats(session, artist), AdvancedStats = GetAdvancedStats(session, artist) }; if (PermissionContext.IsLoggedIn) { var subscription = session.OfType <ArtistForUser>() .Query() .FirstOrDefault(s => s.Artist.Id == id && s.User.Id == PermissionContext.LoggedUserId); if (subscription != null) { contract.IsAdded = true; contract.EmailNotifications = subscription.EmailNotifications; contract.SiteNotifications = subscription.SiteNotifications; } } var relations = (new ArtistRelationsQuery(session, LanguagePreference)).GetRelations(artist, ArtistRelationsFields.All); contract.LatestAlbums = relations.LatestAlbums; contract.TopAlbums = relations.PopularAlbums; contract.LatestSongs = relations.LatestSongs; contract.TopSongs = relations.PopularSongs; // If song and album counts are out of date and we know there's more albums/songs than that, update counts. contract.SharedStats.AlbumCount = Math.Max(contract.SharedStats.AlbumCount, contract.LatestAlbums.Length + contract.TopAlbums.Length); contract.SharedStats.SongCount = Math.Max(contract.SharedStats.SongCount, contract.LatestSongs.Length + contract.TopSongs.Length); contract.LatestComments = Comments(session).GetList(id, 3); if (artist.Deleted) { var mergeEntry = GetMergeRecord(session, id); contract.MergedTo = (mergeEntry != null ? new ArtistContract(mergeEntry.Target, LanguagePreference) : null); } return contract; })); }
public async Task <CompletedGameModel> CompleteGameAsync(Guid gameId, CancellationToken token) { var game = await this.context.Games .Include(g => g.Players) .Include(g => g.Rounds) .Where(g => g.Id == gameId && g.State == GameState.Running) .OrderBy(g => g.Rounds.Select(r => r.Number)) .SingleAsync(token) .Capture(); if (game != null) { game.State = GameState.Completed; await this.context.SaveChangesAsync(token).Capture(); return(this.mapping.Map <CompletedGameModel>(game)); } throw EntityNotFoundException.Throw(gameId.ToString()); }