public async Task <JsonResult> GetTinyUrl(string url) { TinyUrl provider = new TinyUrl(url); ShortUrlClient client = ShortUrlClientFactory.Create(provider); return(Json(await client.ReceiveAsync(), JsonRequestBehavior.DenyGet)); }
public JsonResult Create(string OriginalUrl) { try { if (TinyUrlHelper.ValidateUrl(OriginalUrl)) { var tinyUrl = new TinyUrl() { OriginalUrl = OriginalUrl }; TryValidateModel(tinyUrl); if (ModelState.IsValid) { tinyUrl.Id = _service.Save(tinyUrl); var JsonResults = new TinyUrlViewModel(tinyUrl, TinyUrlHelper.GetFullUrl(tinyUrl.Id, Request)); return(Json(JsonResults)); } } throw new UriFormatException("URL format is invalid!"); } catch (Exception ex) { Response.StatusCode = 500; return(Json("Exception was thrown - " + ex.Message)); } }
/// <summary> /// TinyUrls ViewModel constructor which initializes all the properties required for the response. /// </summary> /// <param name="url">Tiny url object.</param> /// <param name="encodedUrl">Encoded url.</param> public TinyUrlViewModel(TinyUrl url, string encodedUrl) { if (url != null && !string.IsNullOrEmpty(encodedUrl)) { this.Id = url.Id; this.OriginalUrl = url.OriginalUrl; this.EncodedUrl = encodedUrl; } }
static void Main(string[] args) { string url = "http://www.nuget.org"; TinyUrl provider = new TinyUrl(url); ShortUrlFacade facade = new ShortUrlFacade(provider); ShortUrlReceive receive = facade.Receive(); System.Console.WriteLine(receive.ToJson()); }
public async Task <IActionResult> Index(string code) { var model = new TinyUrl(); //evaluamos que venga un parametro en la URL if (!String.IsNullOrEmpty(code)) { //obtenemos la conexion desdea el archivo appsetting.json string connString = this.Configuration.GetConnectionString("conexion"); //instanciamos la conexion SqlConnection conexion = new SqlConnection(connString); try { //abrimos la conexion a la base de datos conexion.Open(); //creamos la query string sqlQuery = "select* from redirect.TinyUrl where Codigo=@codigo"; // creamos el comando para la base de datos SqlCommand comando = new SqlCommand(sqlQuery, conexion); //parametrizamos la consulta para evitar inyeccion SQL comando.Parameters.Add("@codigo", System.Data.SqlDbType.VarChar); comando.Parameters["@codigo"].Value = code; SqlDataReader registro = comando.ExecuteReader(); //si al ejecutar la sentencia se obtuvo al menos un registro if (registro.Read()) { //se crea un ubjeto de la tabla para luego evaluar las propiedades model.Codigo = registro["Codigo"].ToString(); model.DestUrl = registro["DestUrl"].ToString(); model.IsEnabled = bool.Parse(registro["IsEnabled"].ToString()); } else { model.Mensaje = "Aviso!No se encontró ningun registro con el parametro indicado"; } } finally { conexion.Close(); } } else { model.Mensaje = "No se encontraron parametros en la URL. Ej. https://misitio.com?page=0002"; } //si el registro existe y está activo en la base de datos if (model.Codigo != null && model.IsEnabled) { return(Redirect(model.DestUrl)); }//si el registro no está activo en la base de datos else if (model.Codigo != null && !model.IsEnabled) { model.Mensaje = "El parametro brindado no está activo"; } //en caso de que haya que redirigir a la vista donde se muestra la imagen se envia el model el cual lleva un mensaje a mostrar return(View(model)); }
public async Task <ActionResult> Index() { TinyUrl turl = new TinyUrl("http://www.gmail.com"); ShortUrlClient client = ShortUrlClientFactory.Create(turl); ShortUrlReceive response = await client.ReceiveAsync(); client.Dispose(); ViewBag.Url = response.ShortUrl; return(View()); }
public TinyUrl CreateUrl(string url, string tinyUrl, string userID) { var user = DataContext.AspNetUsers.Single(u => u.Id == userID); var submittedUrl = new TinyUrl() { AspNetUser = user, AspNetUsersID = user.Id, TinyUrlString = tinyUrl, UrlString = url, }; DataContext.TinyUrls.InsertOnSubmit(submittedUrl); DataContext.SubmitChanges(); return(DataContext.TinyUrls.Single(u => u.TinyUrlString == tinyUrl)); }
public TinyUrl CreateTinyUrl(UrlInfo urlInfo) { var shortUrl = ShortUrlGenerator.RandomString(8); while (_urlContext.TinyUrl.Where(v => v.ShortUrl == shortUrl).Any()) { shortUrl = ShortUrlGenerator.RandomString(8); } var tinyUrl = new TinyUrl { OriginalUrl = urlInfo.OriginalUrl, ShortUrl = shortUrl, Expiry = urlInfo.Expiry }; _urlContext.SaveUrl(tinyUrl); _logger.LogInformation("Tiny url generated"); return(tinyUrl); }
/// <summary> /// Saves the new url into the database, if already present then returns its id. /// </summary> /// <param name="shortUrl">TinyUrl object which needs to be inserted into the DB.</param> ///<returns>Returns id of the newly added url object.</returns> public int Save(TinyUrl shortUrl) { var CheckForDuplicates = AllTinyUrls.FirstOrDefault(x => x.OriginalUrl == shortUrl.OriginalUrl); if (CheckForDuplicates != null) { return(CheckForDuplicates.Id); } else { _context.TinyUrls.Add(shortUrl); _context.SaveChanges(); GetOrSetCache(true); return(shortUrl.Id); } }
public TinyUrlViewModelTests() { mockData = new TinyUrlRepositoryMock(); tinyUrlModel = mockData.GetTinyUrl(); hash = mockData.hash; }
public void SaveUrl(TinyUrl tinyUrl) { TinyUrl.Add(tinyUrl); SaveChanges(); }