diff --git a/ChatGPT/Default value for 'description' b/ChatGPT/Default value for 'description' new file mode 100644 index 0000000..7293e0a --- /dev/null +++ b/ChatGPT/Default value for 'description' @@ -0,0 +1,42 @@ +Route::post('/import', function (Request $request) { + $data = $request->json()->all(); + $devices = $data['devices']; + foreach ($devices as $device) { + Device::create([ + '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'], + ]); + } + +but 'description' is not always given. How do i solv it in php? + +You can use the null coalescing operator (??) in PHP to provide a default value when the 'description' field is not present in the input data. + +Here's an example of how you can modify the code to use the null coalescing operator: + +php + +Route::post('/import', function (Request $request) { + $data = $request->json()->all(); + $devices = $data['devices']; + foreach ($devices as $device) { + Device::create([ + 'device_id' => $device['device_id'], + 'title' => $device['title'], + 'device_type' => $device['device_type'], + 'description' => $device['description'] ?? '', // provide an empty string as the default value + 'accessories' => $device['accessories'], + 'rz_username_buyer' => $device['rz_username_buyer'], + 'serial_number' => $device['serial_number'], + 'image_url' => $device['image_url'], + ]); + } +}); + +In the example above, the null coalescing operator is used to check whether the 'description' field exists in the input data. If it does not exist, an empty string is provided as the default value. This way, even if the 'description' field is not present in the input data, the code will still run without errors. diff --git a/README.md b/README.md index 741bb04..acf89c6 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,8 @@ latest: 15.04.2023 Laravel is a full feature framework ### security -coss-side-scripting disabled +- coss-side-scripting disabled +- SQL injection attacks ## Database setup diff --git a/device-app/database/factories/DeviceFactory.php b/device-app/database/factories/DeviceFactory.php index 1b44aad..7d0ed7f 100644 --- a/device-app/database/factories/DeviceFactory.php +++ b/device-app/database/factories/DeviceFactory.php @@ -16,10 +16,11 @@ class DeviceFactory extends Factory */ public function definition(): array { + $device_type = $this->faker->randomElement(['projektor','computer','laptop','mikrofon','whiteboard','unbekannt']); return [ 'device_id' => $this->faker->uuid(), 'title' => $this->faker->word(), - 'device_type' => $this->faker->domainWord(), + 'device_type' => $device_type, 'description' => $this->faker->sentence(), 'accessories' => $this->faker->word(), 'rz_username_buyer' => $this->faker->name(), diff --git a/device-app/routes/api.php b/device-app/routes/api.php index c6f4572..5d7419b 100644 --- a/device-app/routes/api.php +++ b/device-app/routes/api.php @@ -5,6 +5,7 @@ use App\Models\OwnerTransaction; use App\Models\PurchasingInformation; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Route; /* @@ -29,16 +30,69 @@ }); Route::post('/import', function (Request $request) { - $data = $request->json()->all(); - $devices = $data['Devices']; + $data = $request->json()->all(); + $devices = $data['devices']; + $purchasingInformations = $data['purchasing_information']; + $ownerTransactions = $data['owner_transactions']; + $locationTransactions = $data['location_transactions']; + //$response = {}; foreach ($devices as $device) { - Device::create([ - 'device_id' => $device->device_id, + $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'] ]); + // insertOrIgnore returns 0 if statement was ignored + if ($deviceInsert != 0) { + // save insert for later + } } - $purchasingInformations = $data['PurchasingInformations']; - $ownerTransactions = $data['OwnerTransactions']; - $locationTransactions = $data['LocationTransactions']; + 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(); }); Route::middleware('auth:sanctum')->get('/user', function (Request $request) {