Skip to content

Commit

Permalink
urls__user_type
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Feb 9, 2025
1 parent 25e2100 commit c28f70c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Enums\UserType;
use App\Http\Middleware\UrlHubLinkChecker;
use App\Http\Requests\StoreUrlRequest;
use App\Models\Url;
Expand All @@ -26,8 +27,11 @@ public static function middleware(): array
*/
public function create(StoreUrlRequest $request)
{
$userType = auth()->check() ? UserType::User->value : UserType::Guest->value;

$url = Url::create([
'user_id' => auth()->id(),
'user_type' => $userType,
'destination' => $request->long_url,
'title' => app(Url::class)->getWebTitle($request->long_url),
'keyword' => app(Url::class)->getKeyword($request),
Expand Down
17 changes: 15 additions & 2 deletions app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Enums\UserType;
use App\Http\Requests\StoreUrlRequest;
use App\Services\KeyGeneratorService;
use App\Settings\GeneralSettings;
Expand All @@ -12,6 +13,7 @@
/**
* @property int $id
* @property int|null $user_id
* @property UserType $user_type
* @property string $keyword
* @property bool $is_custom
* @property string $destination
Expand Down Expand Up @@ -41,6 +43,7 @@ class Url extends Model
*/
protected $fillable = [
'user_id',
'user_type',
'keyword',
'is_custom',
'destination',
Expand All @@ -50,13 +53,12 @@ class Url extends Model

/**
* Get the attributes that should be cast.
*
* @return array{user_id:'integer',is_custom:'boolean'}
*/
protected function casts(): array
{
return [
'user_id' => 'integer',
'user_type' => UserType::class,
'is_custom' => 'boolean',
];
}
Expand Down Expand Up @@ -136,6 +138,17 @@ protected function title(): Attribute
|--------------------------------------------------------------------------
*/

/**
* Scope a query to only include visits from specific user type.
*
* @param Builder<self> $query \Illuminate\Database\Eloquent\Builder
* @param UserType $type \App\Enums\UserType
*/
public function scopeUserType(Builder $query, UserType $type): void
{
$query->where('user_type', $type);
}

public function getKeyword(StoreUrlRequest $request): string
{
$keyGen = app(KeyGeneratorService::class);
Expand Down
2 changes: 2 additions & 0 deletions database/factories/UrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Enums\UserType;
use App\Models\User;
use App\Services\KeyGeneratorService;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand All @@ -20,6 +21,7 @@ public function definition(): array
{
return [
'user_id' => User::factory(),
'user_type' => UserType::User->value,
'destination' => 'https://github.com/realodix/urlhub',
'title' => 'No Title',
'keyword' => app(KeyGeneratorService::class)->randomString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('urls', function (Blueprint $table) {
$table->string('user_type', 10)->after('user_id')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('urls', function (Blueprint $table) {
$table->dropColumn('user_type');
});
}
};

0 comments on commit c28f70c

Please sign in to comment.