Skip to content
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
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
namespace App\Http\Controllers;

use App\Http\Requests\LoginRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Resources\UserResource;
use App\Models\User;
use App\Services\CreditCardService;
use App\Services\UserService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Log;
use Symfony\Component\HttpFoundation\Response;

class AuthController extends Controller
{
public function __construct(protected UserService $userService, protected CreditCardService $creditCardService) {}

public function register(Request $request): JsonResponse
{
$fields = $request->validate([
Expand Down Expand Up @@ -67,4 +72,15 @@ public function login(LoginRequest $request): JsonResponse
'message' => 'Login successful',
], Response::HTTP_OK);
}

public function show(User $user): UserResource
{
return new UserResource($this->userService->show($user));
}

public function update(UpdateUserRequest $request, User $user): UserResource
{
return new UserResource($this->userService->update($request->updateUser(), $user));

}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/CreditCardController.php
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));
}
}
29 changes: 29 additions & 0 deletions app/Http/Requests/UpdateCreditCardRequest.php
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();
}
}
41 changes: 41 additions & 0 deletions app/Http/Requests/UpdateUserRequest.php
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();
}
}
36 changes: 36 additions & 0 deletions app/Http/Resources/CreditCardResource.php
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));
}
Comment on lines +32 to +35
Copy link
Contributor

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

}
19 changes: 19 additions & 0 deletions app/Http/Resources/UserResource.php
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);
}
}
36 changes: 36 additions & 0 deletions app/Models/CreditCard.php
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);
}
}
14 changes: 14 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
Expand All @@ -21,8 +22,16 @@ class User extends Authenticatable
*/
protected $fillable = [
'name',
'last_name',
'email',
'password',
'phone_number',
'voivodship',
'city',
'zip_code',
'street',
'house_number',

];

/**
Expand All @@ -47,4 +56,9 @@ protected function casts(): array
'password' => 'hashed',
];
}

public function creditCard(): HasOne
{
return $this->hasOne(CreditCard::class);
}
}
21 changes: 21 additions & 0 deletions app/Services/CreditCardService.php
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);
}
}
20 changes: 20 additions & 0 deletions app/Services/UserService.php
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;
}
}
29 changes: 29 additions & 0 deletions database/factories/CreditCardFactory.php
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,
];
}
}
9 changes: 9 additions & 0 deletions database/migrations/0001_01_01_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ public function up(): void
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('last_name')->nullable();
$table->string('email')->unique();
$table->string('role')->nullable();
$table->string('phone_number')->nullable();
$table->string('voivodship')->nullable();
$table->string('city')->nullable();
$table->string('zip_code')->nullable();
$table->string('street')->nullable();
$table->string('house_number')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');

$table->rememberToken();
$table->timestamps();
});
Expand Down
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');
}
};
Loading
Loading