127 lines
3.1 KiB
Dart
127 lines
3.1 KiB
Dart
import 'package:appwrite/appwrite.dart';
|
|
import 'package:appwrite/models.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:ltx_flutter/appwrite/auth_api.dart';
|
|
import 'package:ltx_flutter/constants/constants.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class DatabaseAPI extends ChangeNotifier {
|
|
Client client = Client();
|
|
late final Account account;
|
|
late final Databases databases;
|
|
final AuthAPI auth = AuthAPI();
|
|
|
|
late List<Document> _entries = [];
|
|
bool _ready = false;
|
|
|
|
// Getter methods
|
|
List<Document> get entries => _entries;
|
|
int get length => _entries.length;
|
|
bool get ready => _ready;
|
|
double get progress => length / total();
|
|
|
|
final DateFormat formatter = DateFormat('yyyy-MM-dd');
|
|
|
|
// Constructor
|
|
DatabaseAPI() {
|
|
init();
|
|
getAll();
|
|
}
|
|
|
|
init() {
|
|
client
|
|
.setEndpoint(APPWRITE_URL)
|
|
.setProject(APPWRITE_PROJECT_ID)
|
|
.setSelfSigned();
|
|
account = Account(client);
|
|
databases = Databases(client);
|
|
}
|
|
|
|
int total() {
|
|
String dateISO = DateTime.now().toIso8601String();
|
|
|
|
var referenceDate = DateTime.parse("2023-01-01");
|
|
|
|
final date = DateTime.parse(dateISO);
|
|
return date.difference(referenceDate).inDays;
|
|
}
|
|
|
|
getAll() async {
|
|
String paginationQuery = Query.offset(0);
|
|
int max = total();
|
|
|
|
while (_entries.length < max) {
|
|
int remainder = max - _entries.length;
|
|
int limit = remainder > 100 ? 100 : remainder;
|
|
|
|
if (_entries.isNotEmpty) {
|
|
String lastDate = _entries.last.$id;
|
|
paginationQuery = Query.cursorAfter(lastDate);
|
|
}
|
|
|
|
List<Document> newEntries = await getEntries(
|
|
limit: limit,
|
|
paginationQuery: paginationQuery,
|
|
);
|
|
|
|
_entries.addAll(newEntries);
|
|
notifyListeners();
|
|
}
|
|
_ready = true;
|
|
}
|
|
|
|
getOne({required String date}) async {
|
|
int offset =
|
|
DateTime.parse(date).difference(DateTime.parse("2023-01-01")).inDays +
|
|
1;
|
|
|
|
List<Document> response = await getEntries(
|
|
limit: 1,
|
|
paginationQuery: Query.offset(offset),
|
|
);
|
|
|
|
_entries.add(response.first);
|
|
_entries.sort(
|
|
(a, b) => a.$id.compareTo(b.$id),
|
|
);
|
|
notifyListeners();
|
|
return response.first;
|
|
}
|
|
|
|
getEntries({int limit = 100, paginationQuery}) async {
|
|
var response = await databases.listDocuments(
|
|
databaseId: APPWRITE_DATABASE_ID,
|
|
collectionId: COLLECTION,
|
|
queries: [
|
|
Query.orderAsc("date"),
|
|
paginationQuery,
|
|
Query.limit(limit),
|
|
]);
|
|
return response.documents;
|
|
}
|
|
|
|
Future<Document> addEntry(
|
|
{required String date,
|
|
List hours = const [],
|
|
int mood = -1,
|
|
String comments = ""}) {
|
|
return databases.createDocument(
|
|
databaseId: APPWRITE_DATABASE_ID,
|
|
collectionId: COLLECTION,
|
|
documentId: date,
|
|
data: {
|
|
'date': DateTime.parse(date),
|
|
'hours': hours,
|
|
'mood': mood,
|
|
'comments': comments
|
|
});
|
|
}
|
|
|
|
Future<dynamic> deleteEntry({required String date}) {
|
|
return databases.deleteDocument(
|
|
databaseId: APPWRITE_DATABASE_ID,
|
|
collectionId: COLLECTION,
|
|
documentId: date);
|
|
}
|
|
}
|