public async Task <IActionResult> Edit(int id, [Bind("id,longUrl,key")] ShortUrl shortUrl) { if (!loggedIn()) { return(View("Login")); } if (id != shortUrl.id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(shortUrl); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShortUrlExists(shortUrl.id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(shortUrl)); }
public async Task <ActionResult <string> > SaveUrl([FromBody] string longUrl) { var urlObj = new Url(); urlObj.LongUrl = longUrl; urlObj.ShortKey = GenerateShortKey(6);// Max 6 urlObj.CreateDate = DateTime.Now; _context.Urls.Add(urlObj); await _context.SaveChangesAsync(); return(urlObj.ShortKey); }
public async Task <IActionResult> Submit(string longUrl) { string newUrl = tidyUrl(longUrl); if (!validateUrlInput(newUrl)) { ViewData["ErrorText"] = "Invalid url " + newUrl; return(View("UrlError")); } //get existing or create ShortUrl urlReturn = await _context.ShortUrl .FirstOrDefaultAsync(m => m.longUrl == newUrl); if (urlReturn == null) { urlReturn = new ShortUrl(newUrl); while (!keyUnique(urlReturn.key)) { urlReturn = new ShortUrl(newUrl); } if (ModelState.IsValid) { _context.Add(urlReturn); await _context.SaveChangesAsync(); } else { ViewData["ErrorText"] = "Fatal Error"; return(View("UrlError")); } } ViewData["UrlReturn"] = String.Format("{0}://{1}{2}", Request.Scheme, Request.Host, Request.PathBase) + "/" + urlReturn.key; return(View()); }
public async Task <IActionResult> Create([Bind("Id,LongUrl,ShortUrl,CreationDate,Counter")] Url url) { if (ModelState.IsValid) { var d = from _ in _context.Url where _.LongUrl == url.LongUrl select _; if (await d.AnyAsync()) { return(View("Success", await d.FirstOrDefaultAsync())); } var hash = ShortUrl.Models.Logic.Hash.Hasher.GetShortHash(url.LongUrl); url.ShortUrl = hash; url.Counter = 0; _context.Add(url); await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index)); return(View("Success", url)); } return(View(url)); }