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();
        }
예제 #2
0
        public async Task <IActionResult> Create(CreateLinkDTO input)
        {
            var entry = await _linkService.AddLinkEntry(input.URL, Request.HttpContext.Connection.RemoteIpAddress.ToString());

            if (!entry.Success)
            {
                foreach (var error in entry.Errors)
                {
                    ModelState.AddModelError(nameof(input.URL), error);
                }
                return(BadRequest(ModelState));
            }



            return(CreatedAtAction(nameof(Create), LinkDTOPreparer.PrepareDTOWithCount(entry.Entry, HostAddress).ShortenedUrl));
        }
        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();
        }