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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public IActionResult Index()
  26. {
  27. ViewBag.Title = "Url Shortener";
  28. ShortenViewModel model = new ShortenViewModel();
  29. return View(model);
  30. }
  31. [AllowAnonymous]
  32. public IActionResult RedirectToUrl(string url)
  33. {
  34. ShortenedUrl shortUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == url).FirstOrDefault();
  35. if (shortUrl != null)
  36. {
  37. shortUrl.Views += 1;
  38. _dbContext.Entry(shortUrl).State = EntityState.Modified;
  39. _dbContext.SaveChanges();
  40. return Redirect(shortUrl.OriginalUrl);
  41. }
  42. return new StatusCodeResult(StatusCodes.Status404NotFound);
  43. }
  44. [HttpPost]
  45. [AllowAnonymous]
  46. public IActionResult ShortenUrl(string url)
  47. {
  48. if (url.IsValidUrl())
  49. {
  50. ShortenedUrl newUrl = ShortenerHelper.ShortenUrl(_dbContext, url, _config.ShortenerConfig.UrlLength);
  51. if (User.Identity.IsAuthenticated)
  52. {
  53. Users.Models.User foundUser = UserHelper.GetUser(_dbContext, User.Identity.Name);
  54. if (foundUser != null)
  55. {
  56. newUrl.UserId = foundUser.UserId;
  57. }
  58. }
  59. _dbContext.ShortenedUrls.Add(newUrl);
  60. _dbContext.SaveChanges();
  61. string shortUrl = string.Format("{0}://{1}/{2}", Request.Scheme, _config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
  62. if (_config.DevEnvironment)
  63. {
  64. shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
  65. }
  66. return Json(new { result = new { shortUrl = shortUrl, originalUrl = url } });
  67. }
  68. return Json(new { error = "Must be a valid Url" });
  69. }
  70. [HttpPost]
  71. public IActionResult Delete(string id)
  72. {
  73. ShortenedUrl shortenedUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == id).FirstOrDefault();
  74. if (shortenedUrl != null)
  75. {
  76. if (shortenedUrl.User.Username == User.Identity.Name)
  77. {
  78. _dbContext.ShortenedUrls.Remove(shortenedUrl);
  79. _dbContext.SaveChanges();
  80. return Json(new { result = true });
  81. }
  82. return Json(new { error = new { message = "You do not have permission to edit this Shortened URL" } });
  83. }
  84. return Json(new { error = new { message = "This Shortened URL does not exist" } });
  85. }
  86. [AllowAnonymous]
  87. public IActionResult Verify()
  88. {
  89. ViewBag.Title = "Url Shortener Verification";
  90. return View();
  91. }
  92. }
  93. }