Skip to content

Commit

Permalink
feature: finish drug
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed Dec 24, 2021
1 parent 3907ecb commit a90caf8
Show file tree
Hide file tree
Showing 51 changed files with 2,336 additions and 572 deletions.
Binary file added assets/images/blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/drug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/medi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions lib/core/helpers/show_custom_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:happy_care/core/themes/colors.dart';
import 'package:sizer/sizer.dart';

showConfirmDialog(BuildContext context,
{required String title,
required String contentDialog,
required void Function() confirmFunction}) async {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(
title,
style: GoogleFonts.openSans(fontWeight: FontWeight.w600),
),
content: Text(
contentDialog,
),
actions: [
TextButton(
onPressed: confirmFunction,
child: Text(
"Xác nhận",
style: GoogleFonts.openSans(color: kMainColor),
),
),
TextButton(
onPressed: () => Get.back(result: false),
child: Text(
"Không",
style: GoogleFonts.openSans(color: kMainColor),
),
),
],
),
);
}

showLoadingDialog(BuildContext context, {required String contentDialog}) {
return showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(
color: kMainColor,
),
SizedBox(
height: 1.2.h,
),
Text(contentDialog,
style: GoogleFonts.openSans(
color: Colors.white, fontWeight: FontWeight.w700)),
],
),
);
});
}
28 changes: 28 additions & 0 deletions lib/data/api/drug_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:async';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart';

class DrugApi {
final Client http;

DrugApi(this.http);

Future<String> getAllDrug() async {
var response = await http
.get(
// Uri.parse("${dotenv.env['BASE_URL']}$path"),
Uri.parse("${dotenv.env['BASE_URL']}/api/drugs"),
)
.timeout(Duration(minutes: 1), onTimeout: () {
throw TimeoutException("Time out exception");
});
if (response.statusCode == 200) {
print("=======GET_DRUG======\n" + response.body);
return response.body;
} else {
throw Exception();
}
}

}
80 changes: 80 additions & 0 deletions lib/data/api/prescription_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart';

class PrescriptionApi {
final Client http;

PrescriptionApi(this.http);

Future<String> getMyPrescription(String token) async {
final header = {
'Authorization': 'Bearer $token',
};

var response = await http
.get(Uri.parse("${dotenv.env['BASE_URL']}/api/prescriptions/me"),
headers: header)
.timeout(Duration(minutes: 1), onTimeout: () {
throw TimeoutException("Time out exception");
});

if (response.statusCode == 200) {
print("=======GET_PRESCRIPTION======\n" + response.body);
return response.body;
} else {
print("=======GET_PRESCRIPTION_ERROR======");
throw Exception();
}
}

Future<String> createNewPrescription(
String token, Map<String, dynamic> body) async {
final headers = {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
};
print(body);
var response = await http
.post(Uri.parse("${dotenv.env['BASE_URL']}/api/prescriptions"),
headers: headers, body: jsonEncode(body))
.timeout(Duration(minutes: 1), onTimeout: () {
throw TimeoutException("Het gio roi thang l");
});

if (response.statusCode == 200) {
print("Up don thuoc thanh cong");
return response.body;
} else {
print("Up don thuoc deo thanh cong");
print(response.body);
throw Exception();
}
}

Future<String> updatePrescription(
String token, String id ,Map<String, dynamic> body) async {
final headers = {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
};
print(body);
var response = await http
.patch(Uri.parse("${dotenv.env['BASE_URL']}/api/prescriptions/$id"),
headers: headers, body: jsonEncode(body))
.timeout(Duration(minutes: 1), onTimeout: () {
throw TimeoutException("Het gio roi thang l");
});

if (response.statusCode == 200) {
print("Update don thuoc thanh cong");
return response.body;
} else {
print("Update don thuoc deo thanh cong");
print(response.body);
throw Exception();
}
}
}
27 changes: 27 additions & 0 deletions lib/data/api/user_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,31 @@ class UserApi {
throw Exception("Cannot get user information: $userId");
}
}

Future<String> changePassword(
String token, String oldPassword, String newPassword) async {
final body = {
"oldPassword": oldPassword,
"newPassword": newPassword,
};
final headers = {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
};

var response = await http
.post(Uri.parse("${dotenv.env['BASE_URL']}/api/users/change-password"),
headers: headers, body: convert.jsonEncode(body))
.timeout(Duration(minutes: 1), onTimeout: () {
throw TimeoutException("Time out exception");
});

if (response.statusCode == 200) {
print("===========CHANGE_PASSS_OK================\n" + response.body);
return response.body;
} else {
print("CHANGEE FAIL");
throw Exception();
}
}
}
25 changes: 25 additions & 0 deletions lib/data/models/drug.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Drug {
String? id;
String? name;
String? description;


Drug({this.id, this.name, this.description});

@override
String toString() {
return 'Drug(id: $id, name: $name, description: $description)';
}

factory Drug.fromJson(Map<String, dynamic> json) => Drug(
id: json['_id'] as String?,
name: json['name'] as String?,
description: json['description'] as String?,
);

Map<String, dynamic> toJson() => {
'_id': id,
'name': name,
'description': description,
};
}
4 changes: 4 additions & 0 deletions lib/data/models/prescription/medicine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ class Medicine {
'dosage': dosage,
'_id': id,
};
Map<String, dynamic> toJsonNoId() => {
'drug': drug,
'dosage': dosage,
};
}
17 changes: 17 additions & 0 deletions lib/data/repositories/drug_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:convert';

import 'package:happy_care/data/api/drug_api.dart';
import 'package:happy_care/data/models/drug.dart';

class DrugRepository {
final DrugApi? drugApi;

DrugRepository({this.drugApi});

Future<List<Drug>> getAllDrug() async {
String response = await drugApi!.getAllDrug();
var converted = jsonDecode(response);
Iterable list = converted['data']['drugs'];
return list.map((data) => Drug.fromJson(data)).toList();
}
}
67 changes: 67 additions & 0 deletions lib/data/repositories/prescription_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:convert';

import 'package:happy_care/core/utils/shared_pref.dart';
import 'package:happy_care/data/api/prescription_api.dart';
import 'package:happy_care/data/models/prescription/medicine.dart';
import 'package:happy_care/data/models/prescription/prescription.dart';

class PrescriptionRepository {
final PrescriptionApi? prescriptionApi;

PrescriptionRepository({this.prescriptionApi});

Future<List<Prescription>> getMyPrescription() async {
try {
String token = await SharedPrefUtils.getStringKey('token');
String response = await prescriptionApi!.getMyPrescription(token);
var converted = jsonDecode(response);
Iterable list = converted['data']['prescriptions'];
return list.map((e) => Prescription.fromJson(e)).toList();
} catch (_) {
throw Exception();
}
}

Future<String> createNewPrescription(
{required String diagnose,
required String memberId,
required String doctorId,
required List<Medicine> medicines,
String? note}) async {
try {
String token = await SharedPrefUtils.getStringKey('token');
final body = {
'diagnose': diagnose,
'member': memberId,
'doctor': doctorId,
'medicines': medicines.map((e) => e.toJsonNoId()).toList(),
'note': note,
};
String response =
await prescriptionApi!.createNewPrescription(token, body);
var converted = jsonDecode(response);
return converted['data']['prescriptionId'];
} catch (_) {
throw Exception();
}
}

Future<void> updatePrescription({
required String prescriptionId,
required String diagnose,
required List<Medicine> medicines,
String? note,
}) async {
try {
String token = await SharedPrefUtils.getStringKey('token');
final body = {
'diagnose': diagnose,
'medicines': medicines.map((e) => e.toJsonNoId()).toList(),
'note': note,
};
await prescriptionApi!.updatePrescription(token, prescriptionId, body);
} catch (_) {
throw Exception();
}
}
}
1 change: 0 additions & 1 deletion lib/data/repositories/spec_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:happy_care/core/utils/shared_pref.dart';
import 'package:happy_care/data/models/specialization.dart';
import 'package:happy_care/data/api/spec_api.dart';
import 'dart:convert' as convert;
Expand Down
9 changes: 9 additions & 0 deletions lib/data/repositories/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ class UserRepository {
throw Exception();
}
}

Future<void> changePassword(String oldPassword, String newPassword) async {
try {
String token = await SharedPrefUtils.getStringKey("token");
await userApi!.changePassword(token, oldPassword, newPassword);
} catch (_) {
throw Exception();
}
}
}
Loading

0 comments on commit a90caf8

Please sign in to comment.