-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from skni-kod/feat/profile_page
feat: add endpoints for fetching and updating user data, add validati…
- Loading branch information
Showing
14 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\UpdateCreditCardRequest; | ||
use App\Http\Resources\CreditCardResource; | ||
use App\Models\User; | ||
use App\Services\CreditCardService; | ||
|
||
class CreditCardController extends Controller | ||
{ | ||
public function __construct(protected CreditCardService $creditCardService) {} | ||
|
||
public function show(User $user): CreditCardResource | ||
{ | ||
return new CreditCardResource($this->creditCardService->show($user)); | ||
} | ||
|
||
public function update(UpdateCreditCardRequest $request, User $user): CreditCardResource | ||
{ | ||
return new CreditCardResource($this->creditCardService->updateOrCreate($request->updateCard(), $user)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateCreditCardRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
return [ | ||
'card_first_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/', | ||
'card_last_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/', | ||
'card_number' => 'string|regex:/^\d{16}$/', | ||
'card_expiry_date' => 'string|regex:/^\d{2}\/\d{2}$/', | ||
'card_cvv' => 'string|regex:/^\d{3,4}$/', | ||
]; | ||
} | ||
|
||
public function updateCard(): array | ||
{ | ||
return $this->validated(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateUserRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/', | ||
'last_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/', | ||
'email' => 'email|max:255|unique:users,email,'.$this->user->id, | ||
'phone_number' => 'string|regex:/^\+?\d{9,15}$/', | ||
'voivodship' => 'string|min:1|max:30', | ||
'city' => 'string|min:1|max:30', | ||
'zip_code' => 'string|regex:/^\d{2}-\d{3}$/', | ||
'street' => 'string|min:1|max:30', | ||
'house_number' => 'string|regex:/^\d+[a-zA-Z]?$/', | ||
]; | ||
} | ||
|
||
public function updateUser(): array | ||
{ | ||
return $this->validated(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class CreditCardResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'card_first_name' => $this->card_first_name, | ||
'card_last_name' => $this->card_last_name, | ||
'card_number' => $this->maskCardNumber($this->card_number), | ||
'card_expiry_date' => $this->card_expiry_date, | ||
'card_cvv' => $this->maskCvv($this->card_cvv), | ||
]; | ||
} | ||
|
||
private function maskCardNumber(string $cardNumber): string | ||
{ | ||
// ############1234 | ||
return str_repeat('#', strlen($cardNumber) - 4).substr($cardNumber, -4); | ||
} | ||
|
||
private function maskCvv(string $cvv): string | ||
{ | ||
return str_repeat('*', strlen($cvv)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class UserResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Database\Factories\CreditCardFactory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class CreditCard extends Model | ||
{ | ||
/** @use HasFactory<CreditCardFactory> */ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'user_id', | ||
'card_first_name', | ||
'card_last_name', | ||
'card_number', | ||
'card_expiry_date', | ||
'card_cvv', | ||
]; | ||
|
||
protected function casts(): array | ||
{ | ||
return [ | ||
'card_number' => 'hashed', | ||
'card_cvv' => 'hashed', | ||
]; | ||
} | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\CreditCard; | ||
use App\Models\User; | ||
|
||
class CreditCardService | ||
{ | ||
public function show(User $user): CreditCard | ||
{ | ||
return $user->creditCard; | ||
} | ||
|
||
public function updateOrCreate(array $data, User $user): CreditCard | ||
{ | ||
$creditCard = $user->creditCard(); | ||
|
||
return $creditCard->updateOrCreate(['user_id' => $user->id], $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\User; | ||
|
||
class UserService | ||
{ | ||
public function show(User $user): User | ||
{ | ||
return $user; | ||
} | ||
|
||
public function update(array $data, User $user): User | ||
{ | ||
$user->update($data); | ||
|
||
return $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\CreditCard> | ||
*/ | ||
class CreditCardFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'user_id' => User::factory(), | ||
'card_first_name' => fake()->card_first_name, | ||
'card_last_name' => fake()->card_last_name, | ||
'card_number' => fake()->card_number, | ||
'card_expiry_date' => fake()->card_expiry_date, | ||
'card_cvv' => fake()->card_cvv, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
database/migrations/2024_12_18_090401_create_credit_cards_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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::create('credit_cards', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('user_id')->constrained(); | ||
$table->string('card_first_name')->nullable(); | ||
$table->string('card_last_name')->nullable(); | ||
$table->string('card_number')->nullable(); | ||
$table->string('card_expiry_date')->nullable(); | ||
$table->string('card_cvv')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('credit_cards'); | ||
} | ||
}; |
Oops, something went wrong.