Пример #1
0
        public static void GenerateShortUrl(ref UrlInfo model, string urlPrefix, UrlInfoContext context, int daysUntilExpire, ILogger logger)
        {
            var hash      = new Hash();
            var result    = hash.CalculateMD5Hash(model.LongUrl);
            var hashArray = SplitList(result, SHORTENED_URL_LENGTH).ToList();

            //Check if hash is already in the table
            //  If not
            //      Insert new record
            //  Else, check if the longIrl matches what is in the DB already
            //      If yes, return the short url
            //      Else, we have a collision and start over with the next entry in the list

            var options = new DbContextOptions <UrlInfoContext>();

            try
            {
                using (UnitOfWork uow = new UnitOfWork(context))
                {
                    foreach (var item in hashArray)
                    {
                        var urlCheck = uow.UrlInfo.Find(x => x.Suffix == item &&
                                                        x.ExpirationDate > DateTime.Now)
                                       .FirstOrDefault();                  //Check if shortUrl exists and has not expired.
                        if (urlCheck == null)
                        {
                            model.Suffix         = item;
                            model.ShortUrl       = urlPrefix + item;
                            model.UpdatedDate    = DateTime.Now;
                            model.CreatedDate    = DateTime.Now;
                            model.ExpirationDate = DateTime.Now.AddDays(daysUntilExpire);
                            uow.UrlInfo.Add(model);
                            uow.Complete();
                            return;
                        }
                        else
                        {
                            //return only if the LongUrl matches the current LongUrl else we have a hash collision,
                            //so we loop again to get the next hash value.
                            if (model.LongUrl == urlCheck.LongUrl)
                            {
                                model.Suffix         = urlCheck.Suffix;
                                model.ShortUrl       = urlCheck.ShortUrl;
                                model.UpdatedDate    = urlCheck.UpdatedDate;
                                model.CreatedDate    = urlCheck.CreatedDate;
                                model.ExpirationDate = urlCheck.ExpirationDate;
                                return;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Handle Exception here
                logger.LogError("An error occured while Generating the short Url", ex);
            }
        }
Пример #2
0
        public static IEnumerable <UrlInfo> GetUrlListing(UrlInfoContext context, ILogger logger)
        {
            IEnumerable <UrlInfo> resultList = new List <UrlInfo>();

            try
            {
                using (UnitOfWork uow = new UnitOfWork(context))
                {
                    resultList = uow.UrlInfo.GetAll();
                }
            }
            catch (Exception ex)
            {
                logger.LogError("An error occured while getting the list of URLs", ex);
            }
            return(resultList);
        }
 public UrlInfoController(UrlInfoContext context, IOptions <UrlConfig> config, ILogger <UrlInfoController> logger)
 {
     _context     = context;
     this._config = config;
     this._logger = logger;
 }
Пример #4
0
 public static void Initialize(UrlInfoContext context)
 {
     //Add seeding data here
 }
Пример #5
0
 public UrlInfoRepository(UrlInfoContext context)
     : base(context)
 {
 }
Пример #6
0
 public UnitOfWork(UrlInfoContext context)
 {
     _context = context;
     UrlInfo  = new UrlInfoRepository(_context);
 }