Exemple #1
0
        public async Task <VisitorResponseDto> RegisterVisitor(VisitorRequestDto visitorRequest)
        {
            Domain.Entities.ShortUrl shortUrl = _shortUrlRepository.GetByFilter(x => x.ShortURL == visitorRequest.ShortUrl).Result.FirstOrDefault();

            var newVisitor = new Domain.Entities.Visitor
            {
                Date      = DateTime.Now,
                IsDeleted = false,
                Ip        = visitorRequest.Ip,
                UserAgent = visitorRequest.UserAgent,
                ShortUrl  = shortUrl
            };

            await _visitorRepository.Create(newVisitor);

            try
            {
                _visitorRepository.Commit();

                return(new VisitorResponseDto
                {
                    Message = "Visitor registered",
                    Success = true
                });
            }
            catch (Exception ex)
            {
                _visitorRepository.Rollback();
                throw ex;
            }
        }
Exemple #2
0
        public ShortUrlResponseDto SaveItemToDataStore(ShortUrlRequestDto shortUrlRequest)
        {
            Domain.Entities.ShortUrl previouslySaved = _shortUrlRepository.GetItemFromDataStoreByLongUrl(shortUrlRequest.LongURL);
            if (previouslySaved != null)
            {
                return(new ShortUrlResponseDto {
                    ShortURL = previouslySaved.ShortURL, Success = true, Message = "This url has been saved previously"
                });
            }
            else
            {
                //if (LongUrlExists(shortUrlRequest.LongURL))
                //{
                //    string url = _shortUrlRepository.GetByFilter(x => x.LongURL == shortUrlRequest.LongURL).Result.FirstOrDefault().ShortURL;

                //    return new ShortUrlResponseDto
                //    {
                //        Message = "URL already exists",
                //        Success = true,
                //        ShortURL = url
                //    };
                //}

                Domain.Entities.ShortUrl shortUrl = ShortUrlMapper.MapRequestDtoToEntity(shortUrlRequest);

                var shorturl = GenerateShortUrl();

                if (ShortUrlExists(shorturl))
                {
                    while (!ShortUrlExists(shorturl))
                    {
                        shorturl = GenerateShortUrl();
                    }
                }

                shortUrl.ShortURL = shorturl;

                Domain.Entities.ShortUrl savedModel = _shortUrlRepository.SaveItemToDataStore(shortUrl).Result;

                try
                {
                    _shortUrlRepository.Commit();
                }
                catch (Exception ex)
                {
                    _shortUrlRepository.Rollback();
                    throw ex;
                }

                return(new ShortUrlResponseDto
                {
                    ShortURL = savedModel.ShortURL,
                    Success = true,
                    Message = "Saved successfully"
                });
            }
        }
Exemple #3
0
        public ShortUrlResponseDto SaveItemToDataStore(ShortUrlRequestDto shortUrlRequest, string shortUrl)
        {
            Domain.Entities.ShortUrl previouslySaved = _shortUrlRepository.GetItemFromDataStoreByLongUrl(shortUrlRequest.LongURL);
            if (previouslySaved != null)
            {
                return(new ShortUrlResponseDto {
                    ShortURL = previouslySaved.ShortURL, Success = true, Message = "This url has been saved previously"
                });
            }
            else
            {
                Domain.Entities.ShortUrl shorturl = ShortUrlMapper.MapRequestDtoToEntity(shortUrlRequest);

                if (ShortUrlExists(shortUrl))
                {
                    return(new ShortUrlResponseDto
                    {
                        Message = "This short URL already exists, please pick another different.",
                        Success = false,
                        ShortURL = shortUrl
                    });
                }

                shorturl.ShortURL = shortUrl;

                Domain.Entities.ShortUrl savedModel = _shortUrlRepository.SaveItemToDataStore(shorturl).Result;

                try
                {
                    _shortUrlRepository.Commit();
                }
                catch (Exception)
                {
                    _shortUrlRepository.Rollback();
                    throw;
                }

                return(new ShortUrlResponseDto
                {
                    ShortURL = savedModel.ShortURL,
                    Success = true,
                    Message = "Saved successfully"
                });
            }
        }