fix: generate slug by user + check existing link on update

This commit is contained in:
Juan Rodriguez
2024-07-31 22:07:57 +02:00
parent afa9b33568
commit ea71d3825e
3 changed files with 17 additions and 5 deletions
+13 -2
View File
@@ -27,7 +27,7 @@ module App::Controllers::Link
link.id = UUID.v4.to_s
link.url = url
link.user = user
link.slug = SlugService.shorten_url(url)
link.slug = SlugService.shorten_url(url, user.id.to_s)
changeset = Database.insert(link)
if !changeset.valid?
@@ -118,6 +118,7 @@ module App::Controllers::Link
class Update < App::Lib::BaseController
include App::Models
include App::Lib
include App::Services
def call(env)
user = env.get("user").as(User)
@@ -130,7 +131,17 @@ module App::Controllers::Link
raise App::NotFoundException.new(env) if link.nil?
raise App::ForbiddenException.new(env) if link.user_id != user.id
link.url = body["url"].to_s
new_url = body["url"].to_s
existing_query = Database::Query.where(url: new_url, user_id: user.id.to_s).limit(1)
existing_link = Database.all(Link, existing_query).first?
if existing_link
raise App::UnprocessableEntityException.new(env, { "url" => ["URL already exists"] })
end
link.url = new_url
link.slug = SlugService.shorten_url(new_url, user.id.to_s)
changeset = Database.update(link)
if !changeset.valid?
+3 -2
View File
@@ -2,8 +2,9 @@ require "digest"
require "base64"
module App::Services::SlugService
def self.shorten_url(url : String) : String
crc32_hash = Digest::CRC32.digest(url)
def self.shorten_url(url : String, user_id : String) : String
combined = "#{user_id}-#{url}"
crc32_hash = Digest::CRC32.digest(combined)
base62_encoded = Base64.urlsafe_encode(crc32_hash).strip.tr("-_=", "")
base62_encoded
+1 -1
View File
@@ -40,7 +40,7 @@ end
def create_test_link(user, url)
link = App::Models::Link.new
link.id = UUID.v4.to_s
link.slug = App::Services::SlugService.shorten_url(url)
link.slug = App::Services::SlugService.shorten_url(url, user.id.to_s)
link.url = url
link.user = user