public async Task <ApplicationResponse <ShortUrlResponseDto> > Add(ShortUrlRequestDto shortUrlRequestDto)
        {
            var currentUser = await GetCurrentUser();

            var mongoShortUrlEntity = new ShortUrl
            {
                Id            = ObjectId.GenerateNewId().ToString(),
                LongUrlValue  = WebUtility.UrlEncode(shortUrlRequestDto.LongURL),
                ShortUrlValue = await GenerateShortUrl(),
                User          = currentUser,
                ExpireDate    = shortUrlRequestDto.ExpireDate
            };

            await _shortUrlRepository.AddAsync(mongoShortUrlEntity);

            var mapObject = UrlShortMapper.Map(mongoShortUrlEntity);

            return(new ApplicationResponse <ShortUrlResponseDto>(mapObject));
        }
示例#2
0
        public async Task <ActionResult> Post([FromBody] AddShortUrl urlRequest)
        {
            if (ModelState.IsValid)
            {
                var shortUrl = new ShortUrl
                {
                    FullUrl  = urlRequest.Url.Trim(),
                    ShortKey = Convert
                               .ToBase64String(Guid.NewGuid().ToByteArray())
                               .Replace("+", "")
                               .Replace("/", "")
                               .Replace("==", "")
                };
                await urlRepository.AddAsync(shortUrl);

                return(Ok(shortUrl));
            }

            return(BadRequest(ModelState));
        }