fix: generate slug by user + check existing link on update
This commit is contained in:
+13
-2
@@ -27,7 +27,7 @@ module App::Controllers::Link
|
|||||||
link.id = UUID.v4.to_s
|
link.id = UUID.v4.to_s
|
||||||
link.url = url
|
link.url = url
|
||||||
link.user = user
|
link.user = user
|
||||||
link.slug = SlugService.shorten_url(url)
|
link.slug = SlugService.shorten_url(url, user.id.to_s)
|
||||||
|
|
||||||
changeset = Database.insert(link)
|
changeset = Database.insert(link)
|
||||||
if !changeset.valid?
|
if !changeset.valid?
|
||||||
@@ -118,6 +118,7 @@ module App::Controllers::Link
|
|||||||
class Update < App::Lib::BaseController
|
class Update < App::Lib::BaseController
|
||||||
include App::Models
|
include App::Models
|
||||||
include App::Lib
|
include App::Lib
|
||||||
|
include App::Services
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
user = env.get("user").as(User)
|
user = env.get("user").as(User)
|
||||||
@@ -130,7 +131,17 @@ module App::Controllers::Link
|
|||||||
raise App::NotFoundException.new(env) if link.nil?
|
raise App::NotFoundException.new(env) if link.nil?
|
||||||
raise App::ForbiddenException.new(env) if link.user_id != user.id
|
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)
|
changeset = Database.update(link)
|
||||||
if !changeset.valid?
|
if !changeset.valid?
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ require "digest"
|
|||||||
require "base64"
|
require "base64"
|
||||||
|
|
||||||
module App::Services::SlugService
|
module App::Services::SlugService
|
||||||
def self.shorten_url(url : String) : String
|
def self.shorten_url(url : String, user_id : String) : String
|
||||||
crc32_hash = Digest::CRC32.digest(url)
|
combined = "#{user_id}-#{url}"
|
||||||
|
crc32_hash = Digest::CRC32.digest(combined)
|
||||||
base62_encoded = Base64.urlsafe_encode(crc32_hash).strip.tr("-_=", "")
|
base62_encoded = Base64.urlsafe_encode(crc32_hash).strip.tr("-_=", "")
|
||||||
|
|
||||||
base62_encoded
|
base62_encoded
|
||||||
|
|||||||
+1
-1
@@ -40,7 +40,7 @@ end
|
|||||||
def create_test_link(user, url)
|
def create_test_link(user, url)
|
||||||
link = App::Models::Link.new
|
link = App::Models::Link.new
|
||||||
link.id = UUID.v4.to_s
|
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.url = url
|
||||||
link.user = user
|
link.user = user
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user