Esempio n. 1
0
        public async Task <UrlMapOutput> Update(UrlMapInput input)
        {
            var existingUrlMap = await _urlMapRepository.GetAll()
                                 .FirstOrDefaultAsync(x => x.RawUrl == input.RawUrl);

            if (existingUrlMap != null)
            {
                existingUrlMap.Title       = input.Title;
                existingUrlMap.Description = input.Description;

                existingUrlMap = await _urlMapRepository.UpdateAsync(existingUrlMap);

                return(new UrlMapOutput(existingUrlMap));
            }


            var urlMap = await _urlMapRepository.GetAsync(input.Id);

            urlMap.Title       = input.Title;
            urlMap.Description = input.Description;
            urlMap.DeviceInfo  = UserAgent;
            urlMap.ShortenUrl  = ShortenUrlHelper.Encode(urlMap.Identity);

            urlMap = await _urlMapRepository.UpdateAsync(urlMap);

            return(new UrlMapOutput(urlMap));
        }
Esempio n. 2
0
        public async Task <UrlMapOutput> Create(UrlMapInput input)
        {
            var existingUrlMap = await _urlMapRepository.GetAll().AsNoTracking()
                                 .FirstOrDefaultAsync(x => x.RawUrl == input.RawUrl);

            if (existingUrlMap != null)
            {
                //throw new Exception($"An existing item with same url already exist");
                return(new UrlMapOutput(existingUrlMap));
            }

            var urlMap = await _urlMapRepository.CreateAsync(new UrlMap()
            {
                Id          = Guid.NewGuid().ToString(),
                Title       = input.Title,
                Description = input.Description,
                RawUrl      = input.RawUrl,
                DeviceInfo  = UserAgent
            });

            urlMap.ShortenUrl = ShortenUrlHelper.Encode(urlMap.Identity);
            urlMap            = await _urlMapRepository.UpdateAsync(urlMap);

            return(new UrlMapOutput(urlMap));
        }
Esempio n. 3
0
        public async Task <ShortenResponseContract> HandleAsync(ShortenRequestContract contract, string sessionId)
        {
            var shortUrl = ShortenUrlHelper.GetRandomString();

            while (await _shortenerService.GetByShortUrl(shortUrl) != null)
            {
                shortUrl = ShortenUrlHelper.GetRandomString();
            }

            var doc = new UrlDocument
            {
                Created   = DateTime.UtcNow,
                SessionId = sessionId,
                LongUrl   = contract.LongUrl,
                ShortUrl  = shortUrl,
            };

            _shortenerService.SaveNewUrl(doc);

            return(new ShortenResponseContract
            {
                ShortUrl = shortUrl,
            });
        }