public void CreateShortening(Shortening shortening) { if (String.IsNullOrEmpty(shortening.Slug) || String.IsNullOrEmpty(shortening.Link)) { throw new ArgumentException(nameof(shortening)); } _context.Add(shortening); _context.SaveChanges(); }
public void Save(Shortening shortening) { var client = new MongoClient("mongodb+srv://mlnky:865c9Mv%40bJ%5E-%[email protected]/test?retryWrites=true&w=majority"); var database = client.GetDatabase("test"); var collection = database.GetCollection <ShorteningModel>("Shortenings"); var model = new ShorteningModel(); model.LongUrl = shortening.LongUrl; model.ShortKey = shortening.ShortKey; collection.InsertOne(model); }
public Shortening FindOne(long shortKey) { var client = new MongoClient("mongodb+srv://mlnky:865c9Mv%40bJ%5E-%[email protected]/test?retryWrites=true&w=majority"); var database = client.GetDatabase("test"); var collection = database.GetCollection <ShorteningModel>("Shortenings"); ShorteningModel model = collection.Find(x => x.ShortKey == shortKey).FirstOrDefault(); var result = new Shortening(); result.ShortKey = model.ShortKey; result.LongUrl = model.LongUrl; return(result); }
public void DeleteShortening(Shortening shortening) { if (String.IsNullOrEmpty(shortening.Slug)) { throw new ArgumentException($"Value cannot be empty: {nameof(shortening.Slug)}"); } if (String.IsNullOrEmpty(shortening.Link)) { throw new ArgumentException($"Value cannot be empty: {nameof(shortening.Link)}"); } //Possible tracking issue, must be tested. shortening.IsDeleted = true; _context.SaveChanges(); }
public string Shorten(string longUrl, string baseUrl) { var nextId = _shorteningsRepository.GetNextId(); var converter = new Base62.Base62Converter(); var encode = converter.Encode(nextId.ToString()); var newShortening = new Shortening(); newShortening.LongUrl = longUrl; newShortening.ShortKey = nextId; _shorteningsRepository.Save(newShortening); var result = baseUrl + "/" + encode; return(result); }
public IActionResult CreateShortening(ShortenerModel model) { var newShortening = new Shortening { Link = model.Link, Slug = model.Slug }; try { _repo.CreateShortening(newShortening); } catch (ArgumentException ae) { ViewBag.Response = BadRequest(ae.Message); } catch (DbUpdateException) { ViewBag.Response = Conflict("Slug already exists"); } return(View("../Home/Index", model)); }
public IActionResult GetLinkFromSlug(string slug) { Shortening existing = _repo.GetShorteningBySlug(slug); if (existing != null) { string savedLink = existing.Link; if (!savedLink.ToLower().StartsWith("http://") & !savedLink.ToLower().StartsWith("https://")) { return(new RedirectResult($"http://{savedLink}")); } else { return(new RedirectResult(savedLink)); } } else { ViewBag.Response = BadRequest("No such slug"); return(View("../Home/Index")); } }
public void UpdateShortening(Shortening shortening) { //Not implemented }