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));
        }
Ejemplo n.º 3
0
        public async Task TestInsertAndGetByBase64Id()
        {
            MiniLinkContext                  context = GetContext();
            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, "");

            Assert.True(add.Success);

            // reset context to ensue ef doesnt keep the results
            context.Dispose();

            linkEntryService = await SetupAsync();

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

            Assert.Equal(fetch.URL, url);

            context.Dispose();
        }