コード例 #1
0
        public LittleUrl CheckUrl(string longUrl)
        {
            try
            {
                if (!string.IsNullOrEmpty(longUrl))
                {
                    LittleUrl retUrl = _myMongoDb.GetCollection <LittleUrl>(_mongoCollection)
                                       .Find <LittleUrl>(url => url.LongUrl == longUrl).FirstOrDefault();
                    if (retUrl != null)
                    {
                        // If the Url is deleted, undelete it before returning
                        if (retUrl.IsDeleted)
                        {
                            retUrl = ToggleDelete(retUrl.ShortUrl, false);
                        }

                        return(retUrl);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(null);
        }
コード例 #2
0
 public void InsertUrl(LittleUrl url)
 {
     try
     {
         _myMongoDb.GetCollection <LittleUrl>(_mongoCollection).InsertOne(url);
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return;
 }
コード例 #3
0
        public LittleUrl GetUrl(string shortUrl, bool deleted)
        {
            try
            {
                // Call purge when a fetch operation is called
                //   Purge runs just once a day
                if (_lastPurgeDate.AddDays(1) < DateTime.UtcNow)
                {
                    PurgeUrls();
                }

                if (!string.IsNullOrEmpty(shortUrl))
                {
                    // Find matching and undeleted URL
                    var query = Builders <LittleUrl> .Filter.And(
                        Builders <LittleUrl> .Filter.Where(url => url.ShortUrl == shortUrl),
                        Builders <LittleUrl> .Filter.Or(
                            Builders <LittleUrl> .Filter.Exists(url => url.IsDeleted, false),
                            Builders <LittleUrl> .Filter.Where(url => url.IsDeleted == deleted)
                            )
                        );

                    LittleUrl retUrl = _myMongoDb.GetCollection <LittleUrl>(_mongoCollection)
                                       .Find <LittleUrl>(query)
                                       .FirstOrDefault();

                    if (retUrl != null)
                    {
                        // Update last accessed time
                        TouchUrl(shortUrl);
                        return(retUrl);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(null);
        }
コード例 #4
0
        public LittleUrl ToggleDelete(string shortUrl, bool deleteFlag)
        {
            LittleUrl returnUrl = null;

            try
            {
                DateTime purgeDate = DateTime.UtcNow.AddDays(_retentionDays);

                // Logical Delete or Undelete
                if (deleteFlag)
                {
                    // Delete
                    var urlUpdates = Builders <LittleUrl> .Update
                                     //  Set IsDeleted = True
                                     .Set(url => url.IsDeleted, deleteFlag)
                                     //  Set DeletedDate = Now
                                     .Set(url => url.DeletedTime, DateTime.UtcNow)
                                     //  Set PurgeDate = 90 days out (at midnight)
                                     .Set(url => url.PurgeDate, new DateTime(purgeDate.Year,
                                                                             purgeDate.Month,
                                                                             purgeDate.Day,
                                                                             23,
                                                                             59,
                                                                             59,
                                                                             999,
                                                                             DateTimeKind.Utc));

                    // Update if found
                    var options = new FindOneAndUpdateOptions <LittleUrl>
                    {
                        IsUpsert       = false,
                        ReturnDocument = ReturnDocument.After
                    };

                    returnUrl = _myMongoDb.GetCollection <LittleUrl>(_mongoCollection)
                                .FindOneAndUpdate <LittleUrl>(
                        url => url.ShortUrl == shortUrl,
                        urlUpdates,
                        options);
                }
                else
                {
                    // Undelete
                    var urlUpdates = Builders <LittleUrl> .Update
                                     //  Set IsDeleted = False
                                     .Set(url => url.IsDeleted, deleteFlag)
                                     //  Set DeletedDate = Reset
                                     .Set(url => url.DeletedTime, DateTime.MinValue)
                                     //  Set PurgeDate = Reset
                                     .Set(url => url.PurgeDate, DateTime.MinValue);

                    // Update if found
                    var options = new FindOneAndUpdateOptions <LittleUrl>
                    {
                        IsUpsert       = false,
                        ReturnDocument = ReturnDocument.After
                    };

                    returnUrl = _myMongoDb.GetCollection <LittleUrl>(_mongoCollection)
                                .FindOneAndUpdate <LittleUrl>(
                        url => url.ShortUrl == shortUrl,
                        urlUpdates,
                        options);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(returnUrl);
        }