Ejemplo n.º 1
0
        public async Task TestAddVisitAndCount()
        {
            MiniLinkContext                  context;
            IBaseRepository <LinkEntry>      linkRepo;
            IBaseRepository <LinkEntryVisit> linkVisitRepo;

            MemoryCacheOptions options;
            IMemoryCache       cache;

            async Task <ILinkEntryService> SetupAsync()
            {
                context       = GetContext();
                linkRepo      = new BaseRepository <LinkEntry>(context);
                linkVisitRepo = new BaseRepository <LinkEntryVisit>(context);

                var          options = new MemoryCacheOptions();
                IMemoryCache cache   = new MemoryCache(options);


                return(new LinkEntryService(linkRepo, linkVisitRepo, cache));
            }

            ILinkEntryService linkEntryService = await SetupAsync();


            var url = "https://www.google.com/";

            var add = await linkEntryService.AddLinkEntry(url, "");

            var addedEntry = await linkEntryService.GetLinkEntryByBase64Id(add.Entry.Base64Id, true);

            var addVisit = await linkEntryService.AddVisit(addedEntry, "");

            Assert.True(addVisit.Success);

            var entry = await linkEntryService.GetLinkEntryByBase64Id(add.Entry.Base64Id, true);

            // first visit
            Assert.Equal(1, entry.Visits);

            for (var i = 0; i < 100; i++)
            {
                await linkEntryService.AddVisit(addedEntry, "");
            }


            //first visit is shown since we cache on read
            var cachedEntry = await linkEntryService.GetLinkEntryByBase64Id(add.Entry.Base64Id);


            Assert.Equal(1, cachedEntry.Visits);

            // after setting ignore cache true we should get the actual count.
            var updatedEntry = await linkEntryService.GetLinkEntryByBase64Id(add.Entry.Base64Id, true);

            Assert.Equal(101, updatedEntry.Visits);

            context.Dispose();
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Index(string id)
        {
            if (string.IsNullOrEmpty(id) || id.Length < 22)
            {
                return(NotFound());
            }

            // base 64 does not alway produce url safe text therefore we need to decode it


            var entry = await _linkService.GetLinkEntryByBase64Id(id);

            if (entry is null)
            {
                return(NotFound());
            }

            await _linkService.AddVisit(entry, Request.HttpContext.Connection.RemoteIpAddress.ToString());

            return(Redirect(entry.URL));
        }