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); }
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); }
public async Task <IEnumerable <ShortenedUrl> > GetRecentShortenedUrls() { return(await ShortenedUrls.OrderByDescending(url => url.LastUseTime).Take(10).ToListAsync()); }