-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add endpoints for fetching and updating user data, add validati… #6
Open
frooooooo7
wants to merge
8
commits into
dev
Choose a base branch
from
feat/profile_page
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d616ed
feat: add endpoints for fetching and updating user data, add validati…
frooooooo7 afff290
Merge branch 'dev' into feat/profile_page
marioooo0o d1e7bb2
PHP Linting (Pint)
marioooo0o be9cd8e
refactor:
frooooooo7 825e276
PHP Linting (Pint)
frooooooo7 30aa0aa
fix: in UpdateUserRequest unique email
frooooooo7 2b1ae38
PHP Linting (Pint)
frooooooo7 a081a7e
fix:
frooooooo7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Czy cvv nie powinien zachowywać się podobnie jak hasło, tzn ze nie jest dostępne w resource, a przy potwierdzaniu transakcji podawany jest cvv i sprawdzany hash