Skip to content

Commit

Permalink
Rename database column of long_url to destination
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Dec 16, 2022
1 parent fe9c439 commit c36125e
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 72 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function store(StoreUrl $request)

return response([
'id' => $url->id,
'long_url' => $url->long_url,
'long_url' => $url->destination,
'short_url' => url($url->keyword),
], Response::HTTP_CREATED);
}
Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function view()
}

/**
* Show the long url edit page.
* Show shortened url details page
*
* @param mixed $key
* @return \Illuminate\View\View
Expand All @@ -39,7 +39,7 @@ public function edit($key)
}

/**
* Update the long url that was previously set to the new long url.
* Update the destination URL
*
* @param Request $request \Illuminate\Http\Request
* @param mixed $url
Expand All @@ -49,7 +49,7 @@ public function edit($key)
*/
public function update(Request $request, $url)
{
$url->long_url = $request->long_url;
$url->destination = $request->long_url;
$url->title = $request->title;
$url->save();

Expand All @@ -58,7 +58,7 @@ public function update(Request $request, $url)
}

/**
* Delete a shortened URL on user request.
* Delete shortened URLs
*
* @param mixed $url
* @return \Illuminate\Http\RedirectResponse
Expand All @@ -76,10 +76,6 @@ public function delete($url)
}

/**
* UrlHub only allows users (registered & unregistered) to have a unique
* link. You can duplicate it and it will generated a new unique random
* key.
*
* @param mixed $key
* @return \Illuminate\Http\RedirectResponse
*/
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Livewire/Table/AllUlrTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ public function addColumns(): PowerGridEloquent
'<a href="'.$url->short_url.'" target="_blank" class="font-light text-indigo-700">'.$url->keyword.'</a>'
.Blade::render('@svg(\'icon-open-in-new\', \'!h-[0.7em] ml-1\')');
})
->addColumn('long_url', function (Url $url) {
->addColumn('destination', function (Url $url) {
return
'<span title="'.$url->title.'">'
.Str::limit($url->title, self::STR_LIMIT).
'</span>
<br>
<a href="'.$url->long_url.'" target="_blank" title="'.$url->long_url.'" rel="noopener noreferrer"
<a href="'.$url->destination.'" target="_blank" title="'.$url->destination.'" rel="noopener noreferrer"
class="text-slate-500"
>'
.Helper::urlDisplay($url->long_url, self::STR_LIMIT)
.Helper::urlDisplay($url->destination, self::STR_LIMIT)
.Blade::render('@svg(\'icon-open-in-new\', \'!h-[0.7em] ml-1\')').
'</a>';
})
Expand Down Expand Up @@ -171,7 +171,7 @@ public function columns(): array

Column::add()
->title('Destination URL')
->field('long_url')
->field('destination')
->sortable()
->searchable(),

Expand Down
8 changes: 4 additions & 4 deletions app/Http/Livewire/Table/MyUrlTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public function addColumns(): PowerGridEloquent
'<a href="'.$url->short_url.'" target="_blank" class="font-light text-indigo-700">'.$url->keyword.'</a>'
.Blade::render('@svg(\'icon-open-in-new\', \'!h-[0.7em] ml-1\')');
})
->addColumn('long_url', function (Url $url) {
->addColumn('destination', function (Url $url) {
return
'<span title="'.$url->title.'">'
.Str::limit($url->title, self::STR_LIMIT).
'</span>
<br>
<a href="'.$url->long_url.'" target="_blank" title="'.$url->long_url.'" rel="noopener noreferrer" class="text-slate-500">'
.Helper::urlDisplay($url->long_url, self::STR_LIMIT)
<a href="'.$url->destination.'" target="_blank" title="'.$url->destination.'" rel="noopener noreferrer" class="text-slate-500">'
.Helper::urlDisplay($url->destination, self::STR_LIMIT)
.Blade::render('@svg(\'icon-open-in-new\', \'!h-[0.7em] ml-1\')').
'</a>';
})
Expand Down Expand Up @@ -161,7 +161,7 @@ public function columns(): array

Column::add()
->title('Destination URL')
->field('long_url')
->field('destination')
->sortable()
->searchable(),

Expand Down
6 changes: 3 additions & 3 deletions app/Http/Middleware/UrlHubLinkChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function canGeneratingUniqueRandomKey(): bool
}

/**
* Check if a long URL already exists in the database.
* Check if a destination URL already exists in the database.
*
* @param \Illuminate\Http\Request $request
*/
Expand All @@ -92,10 +92,10 @@ private function destinationUrlAlreadyExists($request): Url|null

if (Auth::check()) {
$s_url = Url::whereUserId(Auth::id())
->whereLongUrl($longUrl)
->whereDestination($longUrl)
->first();
} else {
$s_url = Url::whereLongUrl($longUrl)
$s_url = Url::whereDestination($longUrl)
->whereNull('user_id')
->first();
}
Expand Down
20 changes: 10 additions & 10 deletions app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* @property int|null $user_id
* @property string $short_url
* @property string $long_url
* @property string $destination
* @property string $title
*/
class Url extends Model
Expand All @@ -34,7 +34,7 @@ class Url extends Model
'user_id',
'keyword',
'is_custom',
'long_url',
'destination',
'title',
'click',
'ip',
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function userId(): Attribute
);
}

protected function longUrl(): Attribute
protected function destination(): Attribute
{
return Attribute::make(
set: fn ($value) => rtrim($value, '/'),
Expand Down Expand Up @@ -135,15 +135,15 @@ protected function shortUrl(): Attribute
*/
public function shortenUrl(StoreUrl $request, $userId)
{
$key = $request['custom_key'] ?? $this->urlKey($request['long_url']);
$key = $request->custom_key ?? $this->urlKey($request->long_url);

return Url::create([
'user_id' => $userId,
'long_url' => $request['long_url'],
'title' => $request['long_url'],
'keyword' => $key,
'is_custom' => $request['custom_key'] ? true : false,
'ip' => Helper::anonymizeIp($request->ip()),
'user_id' => $userId,
'destination' => $request->long_url,
'title' => $request->long_url,
'keyword' => $key,
'is_custom' => $request->custom_key ? true : false,
'ip' => Helper::anonymizeIp($request->ip()),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Services/UrlRedirectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handleHttpRedirect(Url $url)
'Cache-Control' => sprintf('private,max-age=%s', (int) config('urlhub.redirect_cache_lifetime')),
];

return redirect()->away($url->long_url, (int) config('urlhub.redirect_status_code'), $headers);
return redirect()->away($url->destination, (int) config('urlhub.redirect_status_code'), $headers);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions database/factories/UrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class UrlFactory extends Factory
public function definition()
{
return [
'user_id' => User::factory(),
'long_url' => 'https://github.com/realodix/urlhub',
'title' => 'No Title',
'keyword' => (new Url)->randomString(),
'is_custom' => false,
'click' => mt_rand(10000, 999999999),
'ip' => $this->faker->ipv4(),
'user_id' => User::factory(),
'destination' => 'https://github.com/realodix/urlhub',
'title' => 'No Title',
'keyword' => (new Url)->randomString(),
'is_custom' => false,
'click' => mt_rand(10000, 999999999),
'ip' => $this->faker->ipv4(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
$table->unsignedBigInteger('user_id')->nullable($value = true);
$table->string('keyword')->unique();
$table->boolean('is_custom');
$table->longText('long_url');
$table->longText('destination');
$table->string('title');
$table->unsignedInteger('click')->default(0);
$table->ipAddress('ip');
Expand Down
6 changes: 3 additions & 3 deletions resources/views/backend/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
</div>

<div class="col-span-6">
<label for="long-url" class="block font-medium text-sm text-slate-700">{{__('DestinationURL')}}</label>
<input id="long-url" type="text" name="long_url" placeholder="{{__('Enter your long url')}}"
required value="{{$url->long_url}}" class="form-input">
<label for="long-url" class="block font-medium text-sm text-slate-700">{{__('Destination URL')}}</label>
<input id="long-url" type="text" name="long_url" placeholder="http://www.my_long_url.com"
required value="{{$url->destination}}" class="form-input">
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/frontend/short.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class="btn-clipboard btn-icon-detail"
</span>

<div class="break-all max-w-2xl mt-2">
@svg('arrow-turn-right') <a href="{{ $url->long_url }}" target="_blank" rel="noopener noreferrer" class="redirect-anchor">{{ urlDisplay($url->long_url, limit: 80) }}</a>
@svg('arrow-turn-right') <a href="{{ $url->destination }}" target="_blank" rel="noopener noreferrer" class="redirect-anchor">{{ urlDisplay($url->destination, limit: 80) }}</a>
</div>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/API/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function canCreateUrl()
'short_url',
]);

$this->assertDatabaseHas('urls', $data);
$this->assertDatabaseHas('urls', [
'destination' => 'http://example.com',
]);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/ShortenUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function shortenUrl()
'long_url' => $longUrl,
]);

$url = Url::whereLongUrl($longUrl)->first();
$url = Url::whereDestination($longUrl)->first();

$response->assertRedirectToRoute('su_detail', $url->keyword);
$this->assertFalse($url->is_custom);
Expand All @@ -53,7 +53,7 @@ public function shortenUrlWithCustomKeyword()
]);
$response->assertRedirectToRoute('su_detail', $customKey);

$url = Url::whereLongUrl($longUrl)->first();
$url = Url::whereDestination($longUrl)->first();
$this->assertTrue($url->is_custom);
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public function guestCannotDelete()
]);

$this->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$response = $this->from(route('su_detail', $url->keyword))
Expand All @@ -118,7 +118,7 @@ public function duplicate()
]);

$this->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$this->from(route('su_detail', $url->keyword))
Expand All @@ -136,7 +136,7 @@ public function duplicateUrlCreatedByGuest()

$this->actingAs($this->admin());
$this->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$this->from(route('su_detail', $url->keyword))
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/ShortenUrlWithLongUrlAlreadyExistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function longUrlAlreadyExist()
]);

$response = $this->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$response
Expand All @@ -30,7 +30,7 @@ public function longUrlAlreadyExist()
}

/**
* Memastikan long url dengan atau tanpa trailing slashes adalah sama.
* Memastikan long URL dengan atau tanpa trailing slashes adalah sama.
*
* @test
*/
Expand All @@ -40,8 +40,8 @@ public function ensuresLongUrlsWithOrWithoutSlashesAreTheSameUrl()
$longUrl_2 = 'https://example.com';

$url = Url::factory()->create([
'user_id' => null,
'long_url' => $longUrl_1,
'user_id' => null,
'destination' => $longUrl_1,
]);

$response = $this->post(route('su_create'), [
Expand Down Expand Up @@ -70,7 +70,7 @@ public function longUrlAlreadyExist2()

$response = $this->actingAs($this->admin())
->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$response
Expand All @@ -90,7 +90,7 @@ public function longUrlAlreadyExistsButStillAccepted()
$url = Url::factory()->create();

$response = $this->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$url = Url::whereUserId(null)->first();
Expand All @@ -115,7 +115,7 @@ public function longUrlAlreadyExistsButStillAccepted2()

$response = $this->actingAs($this->admin())
->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$url = Url::whereUserId($user->id)->first();
Expand All @@ -139,7 +139,7 @@ public function longUrlAlreadyExistsButStillAccepted3()

$response = $this->actingAs($this->admin())
->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
]);

$url = Url::whereUserId($user->id)->first();
Expand Down Expand Up @@ -178,7 +178,7 @@ public function cstLongUrlAlreadyExist()
$customKey = 'laravel';

$response = $this->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
'custom_key' => $customKey,
]);
$response->assertRedirectToRoute('su_detail', $url->keyword);
Expand All @@ -198,14 +198,14 @@ public function cstLongUrlAlreadyExist2()

$response = $this->actingAs($this->nonAdmin())
->post(route('su_create'), [
'long_url' => $url->long_url,
'long_url' => $url->destination,
'custom_key' => $customKey,
]);

$response->assertRedirectToRoute('su_detail', $customKey);

$response2 = $this->get(route('home').'/'.$customKey);
$response2->assertRedirect($url->long_url);
$response2->assertRedirect($url->destination);

$this->assertCount(2, Url::all());
}
Expand Down

0 comments on commit c36125e

Please sign in to comment.