-
Notifications
You must be signed in to change notification settings - Fork 1
Nurse CRUD Operations
Do Le Long An edited this page Aug 2, 2024
·
1 revision
Base URL: https://medvoice-fastapi.ngrok.dev/nurses
Endpoint: GET /nurses
Description: Retrieve a list of all nurses.
Request:
- Method:
GET
- URL:
https://medvoice-fastapi.ngrok.dev/nurses
- Headers: None
Response:
- Status:
200 OK
- Body: JSON array of nurses
[ { "id": 1, "name": "John Doe", "email": "[email protected]" }, { "id": 2, "name": "Jane Smith", "email": "[email protected]" } ]
Flutter/Dart Code:
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<List<dynamic>> fetchNurses() async {
final response = await http.get('https://medvoice-fastapi.ngrok.dev/nurses');
if (response.statusCode == 200) {
return json.decode(response.body);
} else {
throw Exception('Failed to load nurses');
}
}
Endpoint: GET /nurses/{nurse_id}
Description: Retrieve details of a specific nurse by their ID.
Request:
- Method:
GET
- URL:
https://medvoice-fastapi.ngrok.dev/nurses/{nurse_id}
- Headers: None
- Path Parameters:
-
nurse_id
(integer): The ID of the nurse to retrieve.
-
Response:
- Status:
200 OK
- Body: JSON object of the nurse
{ "id": 1, "name": "John Doe", "email": "[email protected]" }
- Status:
404 Not Found
if the nurse does not exist.
Flutter/Dart Code:
Future<Map<String, dynamic>> fetchNurseById(int id) async {
final response = await http.get('https://medvoice-fastapi.ngrok.dev/nurses/$id');
if (response.statusCode == 200) {
return json.decode(response.body);
} else {
throw Exception('Failed to load nurse');
}
}
Endpoint: POST /nurses
Description: Create a new nurse.
Request:
- Method:
POST
- URL:
https://medvoice-fastapi.ngrok.dev/nurses
- Headers:
Content-Type: application/json
- Body: JSON object of the nurse to create
{ "name": "John Doe", "email": "[email protected]", "password": "password123" }
Response:
- Status:
201 Created
- Body: JSON object of the created nurse
{ "id": 1, "name": "John Doe", "email": "[email protected]" }
- Status:
400 Bad Request
if the email is already registered.
Flutter/Dart Code:
Future<void> createNurse(String name, String email, String password) async {
final response = await http.post(
'https://medvoice-fastapi.ngrok.dev/nurses',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'name': name,
'email': email,
'password': password,
}),
);
if (response.statusCode == 201) {
print('Nurse created');
} else {
throw Exception('Failed to create nurse');
}
}
Endpoint: PUT /nurses/{nurse_id}
Description: Update details of an existing nurse.
Request:
- Method:
PUT
- URL:
https://medvoice-fastapi.ngrok.dev/nurses/{nurse_id}
- Headers:
Content-Type: application/json
- Path Parameters:
-
nurse_id
(integer): The ID of the nurse to update.
-
- Body: JSON object of the nurse with updated details
{ "name": "John Doe", "email": "[email protected]", "password": "newpassword123" }
Response:
- Status:
200 OK
- Body: JSON object of the updated nurse
{ "id": 1, "name": "John Doe", "email": "[email protected]" }
- Status:
404 Not Found
if the nurse does not exist.
Flutter/Dart Code:
Future<void> updateNurse(int id, String name, String email, String password) async {
final response = await http.put(
'https://medvoice-fastapi.ngrok.dev/nurses/$id',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'name': name,
'email': email,
'password': password,
}),
);
if (response.statusCode == 200) {
print('Nurse updated');
} else {
throw Exception('Failed to update nurse');
}
}
Endpoint: DELETE /nurses/{nurse_id}
Description: Delete an existing nurse.
Request:
- Method:
DELETE
- URL:
https://medvoice-fastapi.ngrok.dev/nurses/{nurse_id}
- Headers: None
- Path Parameters:
-
nurse_id
(integer): The ID of the nurse to delete.
-
Response:
- Status:
200 OK
- Body: JSON object of the deleted nurse
{ "id": 1, "name": "John Doe", "email": "[email protected]" }
- Status:
404 Not Found
if the nurse does not exist.
Flutter/Dart Code:
Future<void> deleteNurse(int id) async {
final response = await http.delete(
'https://medvoice-fastapi.ngrok.dev/nurses/$id',
);
if (response.statusCode == 200) {
print('Nurse deleted');
} else {
throw Exception('Failed to delete nurse');
}
}