Пример #1
0
        public async Task <List <Track> > GrabAndSaveTracks(TracksCriteria criteria)
        {
            TracksSource tracksSource = await _tracksSourceRepository.GetRandomTracksSource(criteria);

            List <Track> tracks = await _grabber.GrabRandomTracks(tracksSource);

            await _tracksRepository.SaveTracks(tracks);

            return(tracks);
        }
Пример #2
0
        public async Task <List <Track> > GetTracks(TracksCriteria criteria, int lastId = 0)
        {
            List <Track> tracks = await _tracksRepository.GetTracks(criteria, 5, lastId);

            if (tracks.Count == 0)
            {
                return(await GrabAndSaveTracks(criteria));
            }
            return(tracks);
        }
Пример #3
0
        public async Task <TracksSource> GetRandomTracksSource(TracksCriteria criteria)
        {
            var query = from ts in _db.TracksSources select ts;

            foreach (string[] tagsGroup in criteria.TagsGroups)
            {
                query = query.Where(ts => ts.Tags.Any(t => tagsGroup.Contains(t)));
            }

            int count = await query.CountAsync();

            int offset = RandomUtils.GetRandomFromInterval(0, count);

            query = query.OrderBy(ts => ts.Id).Skip(offset);

            return(await query.FirstOrDefaultAsync());
        }
Пример #4
0
        public async Task <List <Track> > GetTracks(string tempos, string genres, int lastId = 0)
        {
            TracksCriteria criteria = new TracksCriteria(tempos, genres);

            // todo: use something like middleware for it
            string clientId = Request.Cookies["clientId"];

            if (clientId == null)
            {
                clientId = Guid.NewGuid().ToString();
                CookieOptions options = new CookieOptions();
                options.Expires = DateTime.Now.AddYears(100);
                Response.Cookies.Append("clientId", clientId, options);
            }

            _backgroundTracksLoadingList.AddCriteria(clientId, criteria);

            return(await _tracksProvider.GetTracks(criteria, lastId));
        }
Пример #5
0
        public async Task <List <Track> > GetTracks(TracksCriteria tracksCriteria, int maxCount = 5, int lastId = 0)
        {
            var now = DateTime.Now;

            var query = _db.Tracks.Where(t => t.ExpirationDatetime > now);

            if (lastId > 0)
            {
                query = query.Where(t => t.Id > lastId).OrderBy(t => t.Id);
            }
            else
            {
                query = query.OrderByDescending(t => t.Id);
            }

            foreach (string[] tagsGroup in tracksCriteria.TagsGroups)
            {
                query = query.Where(t => t.TracksSource.Tags.Any(t => tagsGroup.Contains(t)));
            }

            query = query.Take(maxCount);
            return(await query.ToListAsync());
        }
 public void AddCriteria(string clientId, TracksCriteria criteria)
 {
     _criteriasByClients[clientId] = criteria.GetRandomMinimalCriteria().ToString();
 }