abschlussprojekt-device-man.../ChatGPT/API Swagger document generated.txt

264 lines
7.8 KiB
Plaintext
Raw Normal View History

Can you generate me a swagger document for the following api?
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 () {
return response()->json([
'Devices' => Device::all(),
'PurchasingInformations' => PurchasingInformation::all(),
'OwnerTransactions' => OwnerTransaction::all(),
'LocationTransactions' => LocationTransaction::all()
]);
});
Route::middleware('auth:sanctum')->post('/import', function (Request $request) {
$data = $request->json()->all();
$devices = $data['devices'];
$purchasingInformations = $data['purchasing_information'];
$ownerTransactions = $data['owner_transactions'];
$locationTransactions = $data['location_transactions'];
//$response = {};
foreach ($devices as $device) {
$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
}
}
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
}
}
return $request->json()->all();
});
Sure! Here's a Swagger document for the provided API:
yaml
swagger: '2.0'
info:
title: My API
version: '1.0'
basePath: /
schemes:
- http
consumes:
- application/json
produces:
- application/json
paths:
/login:
post:
summary: Authenticate user
description: Authenticate user with username and password
parameters:
- in: body
name: body
description: User credentials
required: true
schema:
type: object
properties:
user:
type: string
password:
type: string
responses:
'200':
description: Authentication successful
schema:
type: object
properties:
token:
type: string
'401':
description: Authentication failed
schema:
type: object
properties:
message:
type: string
/export:
get:
summary: Export data
description: Get all devices, purchasing information, owner transactions, and location transactions
responses:
'200':
description: Data retrieved successfully
schema:
type: object
properties:
Devices:
type: array
items:
$ref: '#/definitions/Device'
PurchasingInformations:
type: array
items:
$ref: '#/definitions/PurchasingInformation'
OwnerTransactions:
type: array
items:
$ref: '#/definitions/OwnerTransaction'
LocationTransactions:
type: array
items:
$ref: '#/definitions/LocationTransaction'
'401':
description: Authentication failed
schema:
type: object
properties:
message:
type: string
/import:
post:
summary: Import data
description: Import device, purchasing information, owner transaction, and location transaction data
parameters:
- in: body
name: body
description: Data to import
required: true
schema:
type: object
properties:
devices:
type: array
items:
$ref: '#/definitions/Device'
purchasing_information:
type: array
items:
$ref: '#/definitions/PurchasingInformation'
owner_transactions:
type: array
items:
$ref: '#/definitions/OwnerTransaction'
location_transactions:
type: array
items:
$ref: '#/definitions/LocationTransaction'
responses:
'200':
description: Data imported successfully
schema:
type: object
properties:
devices:
type: array
items:
type: string
purchasing_information:
type: array
items:
type: string
owner_transactions:
type: array
items:
type: string
location_transactions:
type: array
items:
type: string
'401':
description: Authentication failed
schema:
type: object
properties:
message:
type: string
definitions:
Device:
type: object
properties:
device_id:
type: integer
title:
type: string
device_type:
type: string
description:
type: string
accessories:
type: string
rz_username_buyer:
type: string
serial_number:
type: string
image_url:
type: string
PurchasingInformation:
type: object
properties:
purchasing_information_id:
type: integer
price