Example #1
0
        public static async Task <ShrunkUrl> ShrinkUrlAsync(ShrinkRequest req)
        {
            var db = DatabaseAccessService.OpenOrCreateDefault();
            // Return existing URL if it exists
            var existingUrl = await RetrieveShrunkUrlByTargetAsync(req.Url);

            if (existingUrl != null)
            {
                return(existingUrl);
            }
            var storedUrls   = db.GetCollection <ShrunkUrl>(DatabaseAccessService.ShrunkUrlCollectionDatabaseKey);
            var newShrunkUrl = new ShrunkUrl
            {
                Target           = req.Url,
                ShrunkPath       = StringUtils.SecureRandomString(7),
                CreatedTimestamp = DateTime.Now
            };
            await Task.Run(() =>
            {
                using (var trans = db.BeginTrans())
                {
                    // save new url

                    storedUrls.Insert(newShrunkUrl);
                    storedUrls.EnsureIndex(x => x.ShrunkPath);
                    storedUrls.EnsureIndex(x => x.Target);

                    trans.Commit();
                }
            });

            return(newShrunkUrl);
        }
 public static async Task <IEnumerable <UrlRedirectEvent> > GetRedirectHistory(ShrunkUrl shrunkUrl, DateTime earliestDate)
 {
     return(await Task.Run(() =>
     {
         var db = DatabaseAccessService.OpenOrCreateDefault();
         var storedUrls = db.GetCollection <UrlRedirectEvent>(DatabaseAccessService.ShrunkUrlCollectionDatabaseKey);
         return storedUrls.Find(Query.Where(nameof(UrlRedirectEvent.Timestamp), x => x.AsDateTime > earliestDate));
     }));
 }