Пример #1
0
        public string GetTargetUrl(string shortenedPath)
        {
            var result = ShortenedUrls.FirstOrDefault(u => u.ShortenedPath == shortenedPath);

            if (result == null)
            {
                return(null);
            }

            var today       = GetToday();
            var urlUseCount = UrlUseCounts.FirstOrDefault(c => c.ShortenedUrlId == result.Id && c.Date == today);

            if (urlUseCount == null)
            {
                UrlUseCounts.Add(new UrlUseCount {
                    ShortenedUrlId = result.Id, Date = today, Count = 1
                });
            }
            else
            {
                urlUseCount.Count++;
            }

            result.LastUseTime = GetNowTime();
            this.SaveChanges();

            return(result.TargetUrl);
        }
Пример #2
0
        public bool CreateShortenedUrl(string shortenedPath, string targetUrl)
        {
            if (ShortenedUrls.Any(u => u.ShortenedPath == shortenedPath))
            {
                return(false);
            }

            ShortenedUrls.Add(new ShortenedUrl()
            {
                ShortenedPath = shortenedPath, TargetUrl = targetUrl
            });
            this.SaveChanges();
            return(true);
        }
Пример #3
0
 public async Task <IEnumerable <ShortenedUrl> > GetRecentShortenedUrls()
 {
     return(await ShortenedUrls.OrderByDescending(url => url.LastUseTime).Take(10).ToListAsync());
 }