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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.Logging;
  6. using System.Linq;
  7. using Teknik.Areas.Shortener.Models;
  8. using Teknik.Areas.Shortener.ViewModels;
  9. using Teknik.Areas.Users.Utility;
  10. using Teknik.Attributes;
  11. using Teknik.Configuration;
  12. using Teknik.Controllers;
  13. using Teknik.Data;
  14. using Teknik.Filters;
  15. using Teknik.Logging;
  16. using Teknik.Utilities;
  17. namespace Teknik.Areas.Shortener.Controllers
  18. {
  19. [Authorize]
  20. [Area("Shortener")]
  21. public class ShortenerController : DefaultController
  22. {
  23. public ShortenerController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  24. [AllowAnonymous]
  25. [TrackPageView]
  26. public IActionResult Index()
  27. {
  28. ViewBag.Title = "Url Shortener";
  29. ShortenViewModel model = new ShortenViewModel();
  30. return View(model);
  31. }
  32. [AllowAnonymous]
  33. [TrackPageView]
  34. public IActionResult RedirectToUrl(string url)
  35. {
  36. ShortenedUrl shortUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == url).FirstOrDefault();
  37. if (shortUrl != null)
  38. {
  39. shortUrl.Views += 1;
  40. _dbContext.Entry(shortUrl).State = EntityState.Modified;
  41. _dbContext.SaveChanges();
  42. return Redirect(shortUrl.OriginalUrl);
  43. }
  44. return new StatusCodeResult(StatusCodes.Status404NotFound);
  45. }
  46. [HttpPost]
  47. [AllowAnonymous]
  48. public IActionResult ShortenUrl(string url)
  49. {
  50. if (url.IsValidUrl())
  51. {
  52. ShortenedUrl newUrl = ShortenerHelper.ShortenUrl(_dbContext, url, _config.ShortenerConfig.UrlLength);
  53. if (User.Identity.IsAuthenticated)
  54. {
  55. Users.Models.User foundUser = UserHelper.GetUser(_dbContext, User.Identity.Name);
  56. if (foundUser != null)
  57. {
  58. newUrl.UserId = foundUser.UserId;
  59. }
  60. }
  61. _dbContext.ShortenedUrls.Add(newUrl);
  62. _dbContext.SaveChanges();
  63. string shortUrl = string.Format("{0}://{1}/{2}", Request.Scheme, _config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
  64. if (_config.DevEnvironment)
  65. {
  66. shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
  67. }
  68. return Json(new { result = new { shortUrl = shortUrl, originalUrl = url } });
  69. }
  70. return Json(new { error = "Must be a valid Url" });
  71. }
  72. [HttpPost]
  73. [HttpOptions]
  74. public IActionResult Delete(string id)
  75. {
  76. ShortenedUrl shortenedUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == id).FirstOrDefault();
  77. if (shortenedUrl != null)
  78. {
  79. if (shortenedUrl.User.Username == User.Identity.Name)
  80. {
  81. _dbContext.ShortenedUrls.Remove(shortenedUrl);
  82. _dbContext.SaveChanges();
  83. return Json(new { result = true });
  84. }
  85. return Json(new { error = new { message = "You do not have permission to edit this Shortened URL" } });
  86. }
  87. return Json(new { error = new { message = "This Shortened URL does not exist" } });
  88. }
  89. [AllowAnonymous]
  90. public IActionResult Verify()
  91. {
  92. ViewBag.Title = "Url Shortener Verification";
  93. return View();
  94. }
  95. }
  96. }