2023-03-21 19:54:30 +01:00
|
|
|
<?php
|
|
|
|
|
2023-04-01 20:34:39 +02:00
|
|
|
use App\Models\Device;
|
|
|
|
use App\Models\LocationTransaction;
|
|
|
|
use App\Models\OwnerTransaction;
|
|
|
|
use App\Models\PurchasingInformation;
|
2023-04-03 14:44:33 +02:00
|
|
|
use App\Models\User;
|
2023-03-21 19:54:30 +01:00
|
|
|
use Illuminate\Http\Request;
|
2023-04-03 14:44:33 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-04-03 11:54:41 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2023-04-03 14:44:33 +02:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2023-03-21 19:54:30 +01:00
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| API Routes
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| Here is where you can register API routes for your application. These
|
|
|
|
| routes are loaded by the RouteServiceProvider and all of them will
|
|
|
|
| be assigned to the "api" middleware group. Make something great!
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-04-03 14:44:33 +02:00
|
|
|
Route::post('/login', function (Request $request) {
|
|
|
|
$fields = $request->validate(
|
|
|
|
[
|
|
|
|
'user' => 'required',
|
|
|
|
'password' => 'required'
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
$user = User::where('rz_username', $fields['user'])->first();
|
|
|
|
if (!$user || !Hash::check($fields['password'], $user->hashed_password)) {
|
|
|
|
return response([
|
|
|
|
'message' => 'Bad login'
|
|
|
|
], 401);
|
|
|
|
}
|
|
|
|
$token = $user->createToken('token');
|
|
|
|
return ['token' => $token->plainTextToken];
|
|
|
|
});
|
|
|
|
|
|
|
|
Route::middleware('auth:sanctum')->get('/export', function () {
|
2023-04-01 20:34:39 +02:00
|
|
|
|
2023-03-23 12:25:56 +01:00
|
|
|
return response()->json([
|
2023-04-01 20:34:39 +02:00
|
|
|
'Devices' => Device::all(),
|
|
|
|
'PurchasingInformations' => PurchasingInformation::all(),
|
|
|
|
'OwnerTransactions' => OwnerTransaction::all(),
|
|
|
|
'LocationTransactions' => LocationTransaction::all()
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2023-04-03 14:44:33 +02:00
|
|
|
Route::middleware('auth:sanctum')->post('/import', function (Request $request) {
|
2023-04-03 11:54:41 +02:00
|
|
|
$data = $request->json()->all();
|
|
|
|
$devices = $data['devices'];
|
|
|
|
$purchasingInformations = $data['purchasing_information'];
|
|
|
|
$ownerTransactions = $data['owner_transactions'];
|
|
|
|
$locationTransactions = $data['location_transactions'];
|
|
|
|
//$response = {};
|
2023-04-01 20:34:39 +02:00
|
|
|
foreach ($devices as $device) {
|
2023-04-03 11:54:41 +02:00
|
|
|
$deviceInsert = DB::table('devices')->insertOrIgnore([
|
|
|
|
'device_id' => $device['device_id'],
|
|
|
|
'title' => $device['title'],
|
|
|
|
'device_type' => $device['device_type'],
|
|
|
|
'description' => $device['description'] ?? '',
|
|
|
|
'accessories' => $device['accessories'] ?? '',
|
|
|
|
'rz_username_buyer' => $device['rz_username_buyer'],
|
|
|
|
'serial_number' => $device['serial_number'],
|
|
|
|
'image_url' => $device['image_url']
|
2023-04-01 20:34:39 +02:00
|
|
|
]);
|
2023-04-03 11:54:41 +02:00
|
|
|
// insertOrIgnore returns 0 if statement was ignored
|
|
|
|
if ($deviceInsert != 0) {
|
|
|
|
// save insert for later
|
|
|
|
}
|
2023-04-01 20:34:39 +02:00
|
|
|
}
|
2023-04-03 11:54:41 +02:00
|
|
|
foreach ($purchasingInformations as $purchasing) {
|
|
|
|
$purchasingInsert = DB::table('purchasing_information')->insertOrIgnore([
|
|
|
|
'purchasing_information_id' => $purchasing['purchasing_information_id'],
|
|
|
|
'price' => $purchasing['price'],
|
|
|
|
'timestamp_warranty_end' => $purchasing['timestamp_warranty_end'],
|
|
|
|
'timestamp_purchase' => $purchasing['timestamp_purchase'],
|
|
|
|
'cost_centre' => $purchasing['cost_centre'],
|
|
|
|
'seller' => $purchasing['seller'] ?? '',
|
|
|
|
'device_id' => $purchasing['device_id']
|
|
|
|
]);
|
|
|
|
// insertOrIgnore returns 0 if statement was ignored
|
|
|
|
if ($purchasingInsert != 0) {
|
|
|
|
// save insert for later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($ownerTransactions as $owner) {
|
|
|
|
$ownerInsert = DB::table('owner_transactions')->insertOrIgnore([
|
|
|
|
'owner_transaction_id' => $owner['owner_transaction_id'],
|
|
|
|
'rz_username' => $owner['rz_username'],
|
|
|
|
'timestamp_owner_since' => $owner['timestamp_owner_since'],
|
|
|
|
'device_id' => $owner['device_id']
|
|
|
|
]);
|
|
|
|
// insertOrIgnore returns 0 if statement was ignored
|
|
|
|
if ($ownerInsert != 0) {
|
|
|
|
// save insert for later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($locationTransactions as $location) {
|
|
|
|
$locationInsert = DB::table('location_transactions')->insertOrIgnore([
|
|
|
|
'location_transaction_id' => $location['location_transaction_id'],
|
|
|
|
'room_code' => $location['room_code'],
|
|
|
|
'timestamp_located_since' => $location['timestamp_located_since'],
|
|
|
|
'device_id' => $location['device_id']
|
|
|
|
]);
|
|
|
|
// insertOrIgnore returns 0 if statement was ignored
|
|
|
|
if ($locationInsert != 0) {
|
|
|
|
// save insert for later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ToDo: return only inserted
|
|
|
|
return $request->json()->all();
|
2023-03-23 12:25:56 +01:00
|
|
|
});
|
|
|
|
|
2023-03-21 19:54:30 +01:00
|
|
|
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
|
|
|
return $request->user();
|
|
|
|
});
|