고원빈

[frontend] sqltie 생성

...@@ -94,3 +94,6 @@ appbar 관련 디자인은 추후 구현 예정 ...@@ -94,3 +94,6 @@ appbar 관련 디자인은 추후 구현 예정
94 ### 2021-05-29 94 ### 2021-05-29
95 + 약병 리스트 ui 변경 95 + 약병 리스트 ui 변경
96 + DashBoard 새로고침 96 + DashBoard 새로고침
97 +
98 +### 2021-05-30
99 ++ Sqlite 생성
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -6,6 +6,8 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; ...@@ -6,6 +6,8 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
6 import '../models/Bottle.dart'; 6 import '../models/Bottle.dart';
7 import '../DashBoard.dart'; 7 import '../DashBoard.dart';
8 import '../../utils/user_secure_stoarge.dart'; 8 import '../../utils/user_secure_stoarge.dart';
9 +import '../../utils/DBHelper.dart';
10 +import '../models/UserBottle.dart';
9 11
10 class BottleList extends StatefulWidget { 12 class BottleList extends StatefulWidget {
11 BottleList({Key key}) : super(key: key); 13 BottleList({Key key}) : super(key: key);
...@@ -19,13 +21,14 @@ class _BottleListState extends State<BottleList> { ...@@ -19,13 +21,14 @@ class _BottleListState extends State<BottleList> {
19 Future<String> getBottleList() async { 21 Future<String> getBottleList() async {
20 String hubid = await UserSecureStorage.getHubId(); 22 String hubid = await UserSecureStorage.getHubId();
21 String usertoken = await UserSecureStorage.getUserToken(); 23 String usertoken = await UserSecureStorage.getUserToken();
24 +
25 + var provider = DBHelper();
22 http.Response response = await http.get( 26 http.Response response = await http.get(
23 Uri.encodeFull( 27 Uri.encodeFull(
24 DotEnv().env['SERVER_URL'] + 'bottle/hub/' + hubid.toString()), 28 DotEnv().env['SERVER_URL'] + 'bottle/hub/' + hubid.toString()),
25 headers: {"authorization": usertoken}, 29 headers: {"authorization": usertoken},
26 ); 30 );
27 - print(response.body); 31 +
28 - print(1);
29 if (_bottleList.length != 0) { 32 if (_bottleList.length != 0) {
30 _bottleList.clear(); 33 _bottleList.clear();
31 } 34 }
...@@ -37,6 +40,20 @@ class _BottleListState extends State<BottleList> { ...@@ -37,6 +40,20 @@ class _BottleListState extends State<BottleList> {
37 Map<String, dynamic> map = values[i]; 40 Map<String, dynamic> map = values[i];
38 _bottleList.add(Bottle.fromJson(map)); 41 _bottleList.add(Bottle.fromJson(map));
39 } 42 }
43 + for (int i = 0; i < _bottleList.length; i++) {
44 + UserBottle temp = new UserBottle();
45 + temp.bottleId = _bottleList[i].bottleId;
46 + temp.bottleName = _bottleList[i].bottleId.toString();
47 + provider.createData(temp);
48 + }
49 + List<UserBottle> _userbottleList = new List<UserBottle>();
50 + _userbottleList = await provider.getAllBottle();
51 + for (int i = 0; i < _userbottleList.length; i++) {
52 + print(_userbottleList[i].bottleId);
53 + print(12345678);
54 + }
55 +
56 + print(provider.getAllBottle());
40 return "GET"; 57 return "GET";
41 } else if (response.statusCode == 404) { 58 } else if (response.statusCode == 404) {
42 return "Not Found"; 59 return "Not Found";
......
1 class UserBottle { 1 class UserBottle {
2 - String bottleId; 2 + int bottleId;
3 String bottleName; 3 String bottleName;
4 4
5 UserBottle({this.bottleId, this.bottleName}); 5 UserBottle({this.bottleId, this.bottleName});
6 +
7 + factory UserBottle.fromJson(Map<String, dynamic> parsedJson) {
8 + return UserBottle(
9 + bottleId: parsedJson['bottleId'],
10 + bottleName: parsedJson['bottleName'],
11 + );
12 + }
13 +
14 + Map<String, dynamic> toJson() =>
15 + {"bottleId": bottleId, "bottleName": bottleName};
6 } 16 }
......
1 +import 'dart:io';
2 +import 'package:path_provider/path_provider.dart';
3 +import 'package:sqflite/sqflite.dart';
4 +import 'package:path/path.dart';
5 +import '../screens/models/UserBottle.dart';
6 +
7 +final String tableName = 'medicinename';
8 +
9 +class DBHelper {
10 + DBHelper._();
11 +
12 + static final DBHelper _db = DBHelper._();
13 +
14 + factory DBHelper() => _db;
15 +
16 + static Database _database;
17 +
18 + Future<Database> get database async {
19 + if (_database != null) return _database;
20 +
21 + _database = await initDB();
22 + return _database;
23 + }
24 +
25 + initDB() async {
26 + Directory documentsDirectory = await getApplicationDocumentsDirectory();
27 + String path = join(documentsDirectory.path, 'medicinename.db');
28 +
29 + return await openDatabase(
30 + path,
31 + version: 1,
32 + onCreate: (Database db, int version) async {
33 + await db.execute('''
34 + CREATE TABLE $tableName
35 + (bottleId INTEGER PRIMARY KEY,
36 + bottleName TEXT)
37 + ''');
38 + },
39 + );
40 + }
41 +
42 + createData(UserBottle bottle) async {
43 + final db = await database;
44 + var res = await db.insert(tableName, bottle.toJson(),
45 + conflictAlgorithm: ConflictAlgorithm.replace);
46 + return res;
47 + }
48 +
49 + getBottle(int bottleId) async {
50 + final db = await database;
51 + var res =
52 + await db.query(tableName, where: 'bottleId=?', whereArgs: [bottleId]);
53 + return res.isNotEmpty ? UserBottle.fromJson(res.first) : Null;
54 + }
55 +
56 + Future<List<UserBottle>> getAllBottle() async {
57 + final db = await database;
58 + var res = await db.query(tableName);
59 + List<UserBottle> list =
60 + res.isNotEmpty ? res.map((c) => UserBottle.fromJson(c)).toList() : [];
61 +
62 + return list;
63 + }
64 +
65 + updateBottle(UserBottle bottle) async {
66 + final db = await database;
67 + var res = db.update(tableName, bottle.toJson(),
68 + where: 'bottleId=?', whereArgs: [bottle.bottleId]);
69 + return res;
70 + }
71 +}