The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShortenerController.cs 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Logging;
  5. using System.Linq;
  6. using Teknik.Areas.Shortener.Models;
  7. using Teknik.Areas.Shortener.ViewModels;
  8. using Teknik.Areas.Users.Utility;
  9. using Teknik.Attributes;
  10. using Teknik.Configuration;
  11. using Teknik.Controllers;
  12. using Teknik.Data;
  13. using Teknik.Filters;
  14. using Teknik.Logging;
  15. using Teknik.Utilities;
  16. namespace Teknik.Areas.Shortener.Controllers
  17. {
  18. [TeknikAuthorize]
  19. [Area("Shortener")]
  20. public class ShortenerController : DefaultController
  21. {
  22. public ShortenerController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  23. [AllowAnonymous]
  24. public IActionResult Index()
  25. {
  26. ViewBag.Title = "Url Shortener - " + _config.Title;
  27. ShortenViewModel model = new ShortenViewModel();
  28. return View(model);
  29. }
  30. [AllowAnonymous]
  31. public IActionResult RedirectToUrl(string url)
  32. {
  33. ShortenedUrl shortUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == url).FirstOrDefault();
  34. if (shortUrl != null)
  35. {
  36. shortUrl.Views += 1;
  37. _dbContext.Entry(shortUrl).State = EntityState.Modified;
  38. _dbContext.SaveChanges();
  39. return Redirect(shortUrl.OriginalUrl);
  40. }
  41. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  42. }
  43. [HttpPost]
  44. [AllowAnonymous]
  45. public IActionResult ShortenUrl(string url)
  46. {
  47. if (url.IsValidUrl())
  48. {
  49. ShortenedUrl newUrl = ShortenerHelper.ShortenUrl(_dbContext, url, _config.ShortenerConfig.UrlLength);
  50. if (User.Identity.IsAuthenticated)
  51. {
  52. Users.Models.User foundUser = UserHelper.GetUser(_dbContext, User.Identity.Name);
  53. if (foundUser != null)
  54. {
  55. newUrl.UserId = foundUser.UserId;
  56. }
  57. }
  58. _dbContext.ShortenedUrls.Add(newUrl);
  59. _dbContext.SaveChanges();
  60. string shortUrl = string.Format("{0}://{1}/{2}", Request.Scheme, _config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
  61. if (_config.DevEnvironment)
  62. {
  63. shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
  64. }
  65. return Json(new { result = new { shortUrl = shortUrl, originalUrl = url } });
  66. }
  67. return Json(new { error = "Must be a valid Url" });
  68. }
  69. [AllowAnonymous]
  70. public IActionResult Verify()
  71. {
  72. ViewBag.Title = "Url Shortener Verification - " + _config.Title;
  73. return View();
  74. }
  75. }
  76. }