Skip to content

Commit

Permalink
Merge pull request #12 from teamq-ec/DM2-2646-desarrollo-del-sistema-…
Browse files Browse the repository at this point in the history
…de-registro-de-usuarios-con-angular-y-laravel

Dm2 2646 desarrollo del sistema de registro de usuarios con angular y laravel
  • Loading branch information
fe222004 authored Jul 31, 2024
2 parents cd3e62b + 10ff4c8 commit 884f12a
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/ApplianceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function getUserAppliances($userId)
{
$appliances = QueryBuilder::for(ServiceRequest::class)
->where('user_id', $userId)
->where('state', 'pending')
->with('user')
->allowedFilters([
AllowedFilter::partial('appliance_type'),
Expand Down
43 changes: 26 additions & 17 deletions app/Http/Controllers/PaymentRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace App\Http\Controllers;

use App\Http\Requests\PaymentRequest;
use App\Http\Resources\PaymentResource;
use App\Models\PaymentRecord;
use App\Models\ServiceRequest;
use App\Models\User;
use Illuminate\Http\Request;

class PaymentRecordController extends Controller
Expand All @@ -14,20 +19,31 @@ public function index()
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
public function store(PaymentRequest $request)
{
//
// Valida los datos del pedido
$validatedData = $request->validated();

// Crea un nuevo registro de pago con la información validada
$payment = PaymentRecord::create($validatedData);

// Desactiva el producto (borrado lógico) después del pago
if (isset($validatedData['product_id'])) {
$serviceRequest = ServiceRequest::find($validatedData['product_id']);
if ($serviceRequest) {
$serviceRequest->update(['state' => 'paid']); // O el estado que definas para indicar que el producto ha sido pagado
}
}

// Devuelve una respuesta indicando que el registro se ha creado correctamente
return new PaymentResource([
'success' => true,
'message' => 'Pago registrado correctamente',
'data' => $payment
]);
}

/**
Expand All @@ -38,13 +54,6 @@ public function show(string $id)
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}

/**
* Update the specified resource in storage.
Expand Down
17 changes: 16 additions & 1 deletion app/Http/Controllers/ServiceRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,23 @@ public function store(ServiceRequestRequest $request)
*/
public function show($id)
{
// Encuentra la solicitud de servicio con su usuario asociado, o lanza una excepción si no existe.
$serviceRequest = ServiceRequest::with('user')->findOrFail($id);
return new ServiceRequestResource($serviceRequest);

// Obtiene todas las solicitudes de servicio que están en estado 'pending'.
$pendingRequests = ServiceRequest::where('state', 'pending')->get();

// Crea un recurso de la solicitud de servicio actual.
$serviceRequestResource = new ServiceRequestResource($serviceRequest);

// Crea una colección de recursos para las solicitudes de servicio pendientes.
$pendingRequestsResource = ServiceRequestResource::collection($pendingRequests);

// Devuelve ambas colecciones de recursos en la respuesta.
return response()->json([
'current_request' => $serviceRequestResource,
'pending_requests' => $pendingRequestsResource
]);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions app/Http/Requests/PaymentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PaymentRequest 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 [
'user_id' =>['required'],
'full_name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'telephone_number' => 'required|string|max:20',
'card_type' => 'required|string|max:50',
'card_number' => 'required|string|max:20',
'security_code' => 'required|string|max:4',
'amount_payable' => 'required|numeric|min:0',
'product_id' =>['required'],
];
}
}
22 changes: 22 additions & 0 deletions app/Http/Resources/PaymentResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PaymentResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'success' => $this->resource['success'],
'message' => $this->resource['message'],
];
}
}
2 changes: 1 addition & 1 deletion app/Models/PaymentRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PaymentRecord extends Model

protected $fillable = [
'user_id', 'full_name', 'email', 'telephone_number',
'card_type', 'card_number', 'security_code', 'amount_payable'
'card_type', 'card_number', 'security_code', 'amount_payable','product_id',
];

public function user()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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()
{
Schema::table('payment_records', function (Blueprint $table) {
// Asegúrate de que la tabla de referencia es 'service_requests'
$table->foreignId('product_id')->constrained('service_requests')->after('user_id');
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('payment_records', function (Blueprint $table) {
$table->dropForeign(['product_id']);
$table->dropColumn('product_id');
});
}
};
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\ApplianceController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\PaymentRecordController;
use App\Http\Controllers\ServiceRequestController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -29,3 +30,4 @@
Route::get('product/{product}', [ServiceRequestController::class, 'show']);
// routes/api.php
Route::get('/appliances/{userId}', [ApplianceController::class, 'getUserAppliances']);
Route::post('/payments', [PaymentRecordController::class, 'store']);

0 comments on commit 884f12a

Please sign in to comment.