/// <summary>
        /// create new short url
        /// </summary>
        /// <param name="model">most important thing is in the model is the ActualURL</param>
        /// <returns></returns>
        public ShortUrlResponseModel Create(ShortURLRequestModel model)
        {
            //check if the url saved perviously
            ShortURLModel foundUrl = URLExist(model.ActualURL);

            if (foundUrl != null)
            {
                return(new ShortUrlResponseModel {
                    Model = foundUrl, Success = false, Message = "This url has been saved befor"
                });
            }

            //create new model to save in data base
            ShortURLModel shortURLModel = new ShortURLModel
            {
                DateOfCreation = DateTime.Now,
                ActualURL      = model.ActualURL,
                //try to create a random alphanumeric short url
                ShortenedURL = AutoURLGenerator.ShortUrlGenerator(8)
            };

            //save the model to  database
            var result = _shortUrlRepository.Create(shortURLModel);

            //return the result
            return(new ShortUrlResponseModel
            {
                Model = result,
                Success = true,
                Message = "Saved successfully"
            });
        }
Exemplo n.º 2
0
        public static ShortURLModel MapRequestModelToDBModel(ShortURLRequestModel requestModel)
        {
            ShortURLModel result = new ShortURLModel
            {
                LongURL = requestModel.LongURL
            };

            result.ShortURL = TokenGenerator.GenerateShortUrl();

            return(result);
        }
Exemplo n.º 3
0
        public static ShortURLModel MapRequestModelToDBModel(ShortURLRequestModel requestModel)
        {
            ShortURLModel result = new ShortURLModel
            {
                DateOfCreation = DateTime.Now,
                ActualURL      = requestModel.LongURL
            };

            result.ShortenedURL = TokenGenerator.GenerateShortUrl();

            return(result);
        }
Exemplo n.º 4
0
        public IActionResult Post([FromBody] ShortURLRequestModel model)
        {
            if (ModelState.IsValid)
            {
                ShortUrlResponseModel result = shortUrlService.SaveItemToDataStore(model);
                if (result != null)
                {
                    return(Ok(result));
                }
            }

            return(BadRequest(ModelState.Values));
        }
        public IActionResult Create([FromBody] ShortURLRequestModel model)
        {
            if (ModelState.IsValid)
            {
                ShortUrlResponseModel result = _shortUrlService.Create(model);
                if (result != null)
                {
                    return(Ok(result));
                }
            }

            return(BadRequest(ModelState.Values));
        }
Exemplo n.º 6
0
        public ShortUrlResponseModel SaveItemToDataStore(ShortURLRequestModel model)
        {
            ShortURLModel previouslySaved = shortUrlRepository.GetItemFromDataStoreByLongUrl(model.LongURL);

            if (previouslySaved != null)
            {
                return(new ShortUrlResponseModel {
                    Model = previouslySaved, Success = true, Message = "This url has been saved previously"
                });
            }
            else
            {
                ShortURLModel savedModel = shortUrlRepository.SaveItemToDataStore(ShortUrlModelMapper.MapRequestModelToDBModel(model));

                return(new ShortUrlResponseModel
                {
                    Model = savedModel,
                    Success = true,
                    Message = "Saved successfully"
                });
            }
        }