public async Task <IActionResult> Create(ShortUrlRequestDto requestDto) { var apiResult = await new ApiHelper <ApplicationResponse <ShortUrlResponseDto> >(_httpClient, _securityTokenHandler) .Post(ApiUrl.AddShortly(_apiUrl), requestDto); return(Json(apiResult)); }
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" }); } }
public static ShortUrl MapRequestDtoToEntity(ShortUrlRequestDto shortUrlRequestDto) { ShortUrl result = new ShortUrl { LongURL = shortUrlRequestDto.LongURL, CreationDate = DateTime.Now }; return(result); }
public async Task <IActionResult> Create([FromBody] ShortUrlRequestDto shortUrlRequestDto) { if (ModelState.IsValid) { var result = await _shortUrlService.Add(shortUrlRequestDto); return(Ok(result)); } return(BadRequest(ModelState.Values)); }
public IActionResult GenerateShortUrl([FromBody] ShortUrlRequestDto request) { if (ModelState.IsValid) { ShortUrlResponseDto result = _shortUrlService.SaveItemToDataStore(request); if (result != null) { return(Ok(result)); } //return CreatedAtAction(nameof(Get), new {result.ShortURL}, result); } return(BadRequest(ModelState.Values)); }
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" }); } }
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)); }