Skip to content

Instantly share code, notes, and snippets.

@phinett
Created April 21, 2012 09:02
Show Gist options
  • Save phinett/2435957 to your computer and use it in GitHub Desktop.
Save phinett/2435957 to your computer and use it in GitHub Desktop.
AudioList
[HttpGet]
public ActionResult Index(string sortBy, int page, string genreCode)
{
Genre genreOutputFromQuery = new Genre();
const int pageSize = 25;
RavenQueryStatistics stats;
IList<Core.Documents.Audio> audios;
if (page < 1)
page = 1;
// cache the media list for 3 minutes to increase performance, 3 mins i would consider acceptable
using (_documentSession.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(3)))
{
var audioQuery = _documentSession.Query<Core.Documents.Audio, Audio_Index>()
.Statistics(out stats)
.Include(x => x.AccountId)
.Where(x => x.Status == Core.Enums.Audio.AudioActiveStatus.Active)
.Where(x => x.Visible)
.Where(x => x.AudioType == Audio.AudioType.Mix);
if (!String.IsNullOrEmpty(genreCode))
audioQuery = audioQuery.Where(x => x.Genre.Code == genreCode);
audioQuery = AudioSortByHelpers.AddOrderByToQueryable(audioQuery, sortBy);
audios = audioQuery
.Skip((page - 1)*pageSize)
.Take(pageSize)
.ToList();
}
// load genre if required
using (_documentSession.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromHours(3)))
{
if (!String.IsNullOrEmpty(genreCode))
genreOutputFromQuery = _documentSession.Query<Genre, Genre_Index>().Where(x => x.Code == genreCode).SingleOrDefault();
}
// convert to AudioDto
var audioViewModels = audios.MapTo<AudioDto>();
// sets our has played state
audioViewModels.UpdateAudioDtosWithHasPlayedState(_documentSession, CurrentUserId);
var accounts = _documentSession.Load<Account>(audios.Select(a => a.AccountId).Distinct());
audioViewModels.UpdateAudioDtosWithAccountImage(accounts.ToList());
// fetch sponsored audios if we need to (only show sponsored on specific genre pages and only on page 1)
IList<AudioDto> sponsoredAudioViewModels = new List<AudioDto>();
if (!String.IsNullOrEmpty(genreCode) && page == 1 && sortBy.ToLower() == "latest")
{
using (_documentSession.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(3)))
{
sponsoredAudioViewModels = _documentSession.GetSponsoredAudioViewModels(Audio.AudioType.Mix,
genreCode,
CurrentUserId);
}
}
// create page view model
var viewModel = new MediaIndexViewModel()
{
GenreName = genreOutputFromQuery != null ? genreOutputFromQuery.Name : "",
GenreCode = genreCode,
SortBy = sortBy,
FriendlySortBy = AudioSortByHelpers.SortByToFriendlyString(sortBy),
Page = page,
Audios = audioViewModels.ToPagedList(page - 1, pageSize, stats.TotalResults),
SponsoredAudios = sponsoredAudioViewModels
};
return View(viewModel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment