diff --git a/app/Console/Commands/CreateCV.php b/app/Console/Commands/CreateCV.php index 84c8f14..084fd04 100644 --- a/app/Console/Commands/CreateCV.php +++ b/app/Console/Commands/CreateCV.php @@ -28,13 +28,15 @@ class CreateCV extends Command CV::query() ->create([ - // 'slug' => Str::ra, + 'token' => Str::random(50), 'recipient' => $recipient, 'email' => $email, - 'phone-number' => $phone, + 'phone_number' => $phone, 'locations' => $locations, ]); + $this->info('Created!'); + return CommandAlias::SUCCESS; } } diff --git a/app/Console/Commands/ListCV.php b/app/Console/Commands/ListCV.php new file mode 100644 index 0000000..01d3314 --- /dev/null +++ b/app/Console/Commands/ListCV.php @@ -0,0 +1,35 @@ +line('ID: '. $resource->id); + $this->line('Token: '. $resource->token); + $this->line('Company: '. $resource->recipient); + $this->line('Phone: '. $resource->formattedPhoneNumber .', '. $resource->PhoneNumber); + $this->line('Locations: '. implode(' / ', $resource->locations)); + $this->line('Views: '. $resource->views); + $this->line(''); + } + + return Command::SUCCESS; + } +} diff --git a/app/Console/Commands/UpdateCV.php b/app/Console/Commands/UpdateCV.php new file mode 100644 index 0000000..ec550ff --- /dev/null +++ b/app/Console/Commands/UpdateCV.php @@ -0,0 +1,67 @@ +argument('id')) || $id <= 0) { + $this->error('Incorrect id'); + return Command::FAILURE; + } + + $cv = CV::find($id); + + if ($company = $this->option('company')) { + $cv->recipient = $company; + } + if ($phone = $this->option('phone')) { + $cv->phone_number = $phone; + } + if (count($addLocations = $this->option('remove-location')) > 0) { + $locations = $cv->locations; + $locations = array_diff($locations, $addLocations); + $cv->locations = $locations; + } + if (count($addLocations = $this->option('add-location')) > 0) { + $locations = $cv->locations; + + $clearLocations = []; + foreach ($addLocations as $location) { + if (in_array($location, $locations)) { + $this->warn('"'. $location .'" exists! This value was not added.'); + $clearLocations[] = $location; + } + } + $addLocations = array_diff($addLocations, $clearLocations); + + if ($this->option('begin-location')) + $locations = array_merge($addLocations, $locations); + else + $locations = array_merge($locations, $addLocations); + $cv->locations = $locations; + } + + $cv->save(); + + $this->info('Updated!'); + + return Command::SUCCESS; + } +} diff --git a/app/Http/Controllers/Api/CVController.php b/app/Http/Controllers/Api/CVController.php new file mode 100644 index 0000000..1ca5a8c --- /dev/null +++ b/app/Http/Controllers/Api/CVController.php @@ -0,0 +1,20 @@ +update(['views' => $cv->views+=1]); + + return new CVResource($cv); + } +} diff --git a/app/Http/Resources/CVResource.php b/app/Http/Resources/CVResource.php new file mode 100644 index 0000000..da8e86b --- /dev/null +++ b/app/Http/Resources/CVResource.php @@ -0,0 +1,22 @@ + $this->token, + 'email' => $this->email, + 'phoneNumber' => $this->phoneNumber, + 'formattedPhoneNumber' => $this->formattedPhoneNumber, + ]; + } +} diff --git a/app/Models/CV.php b/app/Models/CV.php index 5dd4c32..42a8504 100644 --- a/app/Models/CV.php +++ b/app/Models/CV.php @@ -4,11 +4,13 @@ declare(strict_types=1); namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; /** * @property int $id + * @property string $token * @property string $recipient * @property string $email * @property string $phoneNumber @@ -25,4 +27,23 @@ class CV extends Model 'locations' => 'array', 'views' => 'integer', ]; + + protected function phoneNumber(): Attribute + { + return Attribute::make( + get: fn (mixed $value): ?string => str_replace(' ', '', $value ?? ''), + ); + } + + protected function formattedPhoneNumber(): Attribute + { + return Attribute::make( + get: fn (mixed $value, array $attributes): ?string => $attributes['phone_number'] ?? '', + ); + } + + public function getRouteKeyName(): string + { + return 'token'; + } } diff --git a/database/migrations/2023_06_15_220540_create_cvs_table.php b/database/migrations/2023_06_15_220540_create_cvs_table.php index a7560d9..0593181 100644 --- a/database/migrations/2023_06_15_220540_create_cvs_table.php +++ b/database/migrations/2023_06_15_220540_create_cvs_table.php @@ -12,9 +12,10 @@ return new class extends Migration { Schema::create('cvs', function (Blueprint $table) { $table->id(); + $table->string('token', 50); $table->string('recipient', 255); $table->string('email', 255); - $table->string('phone-number', 15); + $table->string('phone_number', 15); $table->json('locations'); $table->integer('views')->nullable()->default(0); $table->timestamps(); diff --git a/routes/api.php b/routes/api.php index 831d986..06d1a68 100644 --- a/routes/api.php +++ b/routes/api.php @@ -17,3 +17,5 @@ Route::get('projects', 'ProjectController@index'); Route::prefix('project')->group(function() { Route::get('{project}', 'ProjectController@show'); }); + +Route::get('cv/{cv}', 'CVController@show');