Skip to content

Commit

Permalink
Big changeings and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Darknessly1 committed Jun 6, 2024
1 parent bc81d5c commit f7bdcfb
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 55 deletions.
29 changes: 25 additions & 4 deletions app/Http/Controllers/DashboardTicketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@
use App\Models\DashboardTicket;
use App\Models\Ticket;

class DashboardTicketController extends Controller {
class DashboardTicketController extends Controller
{
public function getAllTickets()
{
$dashboardTickets = DashboardTicket::with('ticket')->get();
return response()->json($dashboardTickets);
}

public function addTicketToDashboard(Request $request)
{
$dashboardTicket = new DashboardTicket();
$dashboardTicket->user_id = Auth::id();
$dashboardTicket->ticket_id = $request->ticket_id;
$dashboardTicket->save();

public function getAllTickets() {
$dashboardTickets = DashboardTicket::all();
return response()->json($dashboardTickets );
return response()->json(['message' => 'Ticket added to dashboard successfully']);
}

public function deleteTicketFromDashboard($id)
{
$dashboardTicket = DashboardTicket::find($id);
if ($dashboardTicket) {
$dashboardTicket->delete();
return response()->json(['message' => 'Ticket deleted from dashboard successfully']);
}

return response()->json(['message' => 'Ticket not found'], 404);
}
}
64 changes: 64 additions & 0 deletions app/Http/Controllers/PurchaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Http\Controllers;

use App\Models\Purchase;
use App\Models\Ticket;
use App\Models\NewTicket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PurchaseController extends Controller
{
public function create(Request $request)
{
$validatedData = $request->validate([
'ticket_id' => 'required|integer',
'ticket_type' => 'required|string|in:tickets,new_tickets',
]);

$ticketType = $validatedData['ticket_type'];
$ticketId = $validatedData['ticket_id'];

$ticket = $ticketType == 'tickets' ? Ticket::find($ticketId) : NewTicket::find($ticketId);

if (!$ticket) {
return redirect()->back()->withErrors(['ticket_not_found' => 'Ticket not found']);
}

return view('purchase.confirm', compact('ticket', 'ticketType'));
}

public function store(Request $request)
{
$validatedData = $request->validate([
'ticket_id' => 'required|integer',
'ticket_type' => 'required|string|in:tickets,new_tickets',
'num_travelers' => 'required|integer|min=1',
'travel_purpose' => 'required|string',
'has_children' => 'required|boolean',
'luggage_type' => 'required|string',
]);

$ticketType = $validatedData['ticket_type'];
$ticketId = $validatedData['ticket_id'];

$ticket = $ticketType == 'tickets' ? Ticket::find($ticketId) : NewTicket::find($ticketId);

if (!$ticket) {
return response()->json(['error' => 'Ticket not found'], 404);
}

$purchase = Purchase::create([
'user_id' => Auth::id(),
'ticket_id' => $ticketId,
'ticket_type' => $ticketType,
'num_travelers' => $validatedData['num_travelers'],
'travel_purpose' => $validatedData['travel_purpose'],
'has_children' => $validatedData['has_children'],
'luggage_type' => $validatedData['luggage_type'],
]);

return response()->json(['message' => 'Purchase confirmed successfully'], 200);
}
}
42 changes: 32 additions & 10 deletions app/Http/Controllers/TicketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,50 @@
class TicketController extends Controller
{

public function index() {
public function index()
{
$tickets = Ticket::all();
return response()->json($tickets);
}

// public function addToDashboard(Request $request)
// {
// $request->validate([
// 'ticket_id' => 'required|exists:tickets,TicketID',
// ]);

// DashboardTicket::create([
// 'ticket_id' => $request->ticket_id,
// ]);

// return response()->json(['message' => 'Ticket added to dashboard successfully']);
// }

public function getDashboardTickets()
{
$tickets = DashboardTicket::with('ticket')->get();

return response()->json($tickets);
}

public function addToDashboard(Request $request)
{
$request->validate([
'ticket_id' => 'required|exists:tickets,TicketID',
]);

DashboardTicket::create([
'ticket_id' => $request->ticket_id,
]);
$dashboardTicket = new DashboardTicket();
$dashboardTicket->ticket_id = $request->ticket_id;
$dashboardTicket->save();

return response()->json(['message' => 'Ticket added to dashboard successfully']);
return response()->json(['message' => 'Ticket added to dashboard successfully'], 200);
}

public function getDashboardTickets()
public function show($id)
{
$tickets = DashboardTicket::with('ticket')->get();

return response()->json($tickets);
$ticket = Ticket::find($id);
if (!$ticket) {
return response()->json(['message' => 'Ticket not found'], 404);
}
return response()->json($ticket);
}
}
31 changes: 31 additions & 0 deletions app/Models/Purchase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
use HasFactory;

protected $fillable = [
'user_id',
'ticket_id',
'ticket_type',
'num_travelers',
'travel_purpose',
'has_children',
'luggage_type',
];

public function user()
{
return $this->belongsTo(User::class);
}

public function ticket()
{
return $this->morphTo();
}
}
3 changes: 2 additions & 1 deletion app/Models/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
class Ticket extends Model
{
use HasFactory;
protected $primaryKey = 'TicketID';

protected $fillable = [
'PassengerName', 'FlightNumber', 'DepartureAirport', 'ArrivalAirport',
'DepartureDateTime', 'ArrivalDateTime', 'SeatNumber', 'Price', 'Airline'
'DepartureDateTime', 'ArrivalDateTime', 'SeatNumber', 'Price', 'Airline'
];

public function users() {
Expand Down
34 changes: 34 additions & 0 deletions database/migrations/2024_06_06_093146_create_purchases_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePurchasesTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('purchases', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('ticket_id')->constrained()->onDelete('cascade');
$table->string('ticket_type'); // "new_tickets" or "tickets"
$table->integer('num_travelers');
$table->string('travel_purpose');
$table->boolean('has_children');
$table->string('luggage_type');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('purchases');
}
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"devDependencies": {
"@headlessui/react": "^1.4.2",
"@inertiajs/react": "^1.0.0",
"@inertiajs/react": "^1.1.0",
"@tailwindcss/forms": "^0.5.3",
"@vitejs/plugin-react": "^4.2.0",
"autoprefixer": "^10.4.12",
Expand Down
5 changes: 1 addition & 4 deletions resources/js/Components/Panes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,4 @@ const Panes = () => {
);
};

export default Panes;



export default Panes;
39 changes: 25 additions & 14 deletions resources/js/Pages/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect } from 'react';
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head } from '@inertiajs/react';
import { Inertia } from '@inertiajs/inertia';
import axios from 'axios';
import { TicketIcon } from '@heroicons/react/solid';

Expand All @@ -16,10 +17,18 @@ export default function Dashboard({ auth }) {
fetchDashboardTickets();
}, []);

const handleDelete = () => {

}
const handleShowConfirmPurchase = (ticketId) => {
Inertia.get('/confirm-purchase', { ticketId });
};

const handleDelete = async (id) => {
try {
await axios.delete(`/dashboard-tickets/${id}`);
setDashboardTickets(dashboardTickets.filter(ticket => ticket.id !== id));
} catch (error) {
console.error('Error deleting ticket from dashboard:', error);
}
};

return (
<AuthenticatedLayout
Expand Down Expand Up @@ -50,16 +59,19 @@ export default function Dashboard({ auth }) {
<p className="my-2 text-gray-600"><strong>Seat Number:</strong> {dashboardTicket.ticket.SeatNumber}</p>
<p className="my-2 text-gray-600"><strong>Price:</strong> ${dashboardTicket.ticket.Price}</p>
<p className="my-2 text-gray-600"><strong>Airline:</strong> {dashboardTicket.ticket.Airline}</p>
<div class="flex flex-wrap justify-center gap-6">
<a class="relative" href="#">
<span class="absolute top-0 left-0 mt-1 ml-1 h-full w-full rounded bg-red-500"></span>
<span
class="fold-bold relative inline-block h-full w-full rounded border-2 border-black bg-white px-3 py-1 text-base font-bold text-black transition duration-100 hover:bg-red-400 hover:text-gray-900"
onClick={() => handleDelete()}
>
Delete From Dashboard
</span>
</a>
<div className="flex flex-wrap justify-center gap-6">
<button
onClick={() => handleShowConfirmPurchase(dashboardTicket.ticket.TicketID)}
className="px-4 py-2 bg-green-500 text-white rounded-lg"
>
Confirm Purchase
</button>
<button
onClick={() => handleDelete(dashboardTicket.ticket.TicketID)}
className="px-4 py-2 bg-red-500 text-white rounded-lg"
>
Delete
</button>
</div>
</div>
</div>
Expand All @@ -70,7 +82,6 @@ export default function Dashboard({ auth }) {
</div>
</div>
</div>

</AuthenticatedLayout>
);
}
Loading

0 comments on commit f7bdcfb

Please sign in to comment.