Пример #1
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"
                });
            }
        }