From 15ffa9e9c6e6a7ac9b7537d72ab8afe4218d9ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Tue, 5 Apr 2022 18:45:47 +0200 Subject: [PATCH] =?UTF-8?q?Vylep=C5=A1en=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pubignore | 3 +- CHANGELOG.md | 3 ++ lib/src/canteen.dart | 89 +++++++++----------------------------------- pubspec.yaml | 2 +- 4 files changed, 24 insertions(+), 73 deletions(-) diff --git a/.pubignore b/.pubignore index 02818d3..b52a8ca 100644 --- a/.pubignore +++ b/.pubignore @@ -1,2 +1,3 @@ .env -test \ No newline at end of file +test +*.test.dart \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index fd77df0..788d94c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.1.0-alpha.13 +- Další opravy +- Úprava metod `doBurzy` a `objednat`, aby opravdu mohly vracet aktualizované instance `Jidlo` ## 0.1.0-alpha.12 - Skutečná oprava ## 0.1.0-alpha.11 diff --git a/lib/src/canteen.dart b/lib/src/canteen.dart index caa3cd4..141b268 100644 --- a/lib/src/canteen.dart +++ b/lib/src/canteen.dart @@ -334,7 +334,7 @@ class Canteen { /// - `j` - Jídlo, které chceme objednat | [Jidlo] /// /// Výstup: - /// - Upravená instance [Jidlo] tohoto jídla + /// - Aktualizovaná instance [Jidlo] tohoto jídla Future objednat(Jidlo j) async { if (!prihlasen) { return Future.error("Uživatel není přihlášen"); @@ -353,41 +353,14 @@ class Canteen { return Future.error("Uživatel není přihlášen"); } - var novy = await _getRequest( - "/faces/secured/db/dbJidelnicekOnDayView.jsp?day=${j.den.year}-${(j.den.month < 10) ? "0" + j.den.month.toString() : j.den.month}-${(j.den.day < 10) ? "0" + j.den.day.toString() : j.den.day}&terminal=false&rating=null&printer=false&keyboard=false"); // získat novou URL pro objednávání - var lzeObjednat = - !(novy.contains("nelze zrušit") || novy.contains("nelze objednat")); - String? orderUrl; - String? burzaUrl; + var novy = (await jidelnicekDen(den: j.den)) + .jidla + .where( + (element) => element.nazev == j.nazev, + ) + .toList()[0]; - if (lzeObjednat) { - // pokud lze objednat, nastavíme adresu pro objednání - var match = RegExp(r"(?<=ajaxOrder\(this, ').+?(?=')").firstMatch(novy); - if (match != null) { - orderUrl = match.group(0)!.replaceAll("amp;", ""); - } - } else { - // jinak nastavíme URL pro burzu - var match = - RegExp(r"(?<=ajaxOrder\(this, ')(.+?)(?=').+?(do burzy)|(z burzy)") - .firstMatch(novy); - if (match != null) { - burzaUrl = match.group(1)!.replaceAll("amp;", ""); - } - } - - return Jidlo( - varianta: j.varianta, - nazev: j.nazev, - objednano: !j.objednano, - cena: j.cena, - lzeObjednat: j.lzeObjednat, - orderUrl: orderUrl, - den: j.den, - burzaUrl: burzaUrl, - naBurze: (burzaUrl == null) - ? false - : !burzaUrl.contains("plusburza")); // vrátit upravenou instanci + return novy; // vrátit novou instanci } /// Uloží vaše jídlo z/do burzy @@ -396,7 +369,7 @@ class Canteen { /// - `j` - Jídlo, které chceme dát/vzít do/z burzy | [Jidlo] /// /// Výstup: - /// - Upravená instance [Jidlo] tohoto jídla + /// - Aktualizovaná instance [Jidlo] tohoto jídla NEBO [Future] jako chyba Future doBurzy(Jidlo j) async { if (!prihlasen) { return Future.error("Uživatel není přihlášen"); @@ -407,47 +380,20 @@ class Canteen { } var res = await _getRequest("/faces/secured/" + j.burzaUrl!); // provést operaci - if (res.contains("Chyba")) return j; + if (res.contains("Chyba")) return Future.error("Chyba při vykonávání"); if (res.contains("přihlášení uživatele")) { prihlasen = false; return Future.error("Uživatel není přihlášen"); } - var novy = await _getRequest( - "/faces/secured/db/dbJidelnicekOnDayView.jsp?day=${j.den.year}-${(j.den.month < 10) ? "0" + j.den.month.toString() : j.den.month}-${(j.den.day < 10) ? "0" + j.den.day.toString() : j.den.day}&terminal=false&rating=null&printer=false&keyboard=false"); // získat novou URL pro objednávání - var lzeObjednat = - !(novy.contains("nelze zrušit") || novy.contains("nelze objednat")); - String? orderUrl; - String? burzaUrl; + var novy = (await jidelnicekDen(den: j.den)) + .jidla + .where( + (element) => element.nazev == j.nazev, + ) + .toList()[0]; - if (lzeObjednat) { - // pokud lze objednat, nastavíme adresu pro objednání - var match = RegExp(r"(?<=ajaxOrder\(this, ').+?(?=')").firstMatch(novy); - if (match != null) { - orderUrl = match.group(0)!.replaceAll("amp;", ""); - } - } else { - // jinak nastavíme URL pro burzu - var match = - RegExp(r"(?<=ajaxOrder\(this, ')(.+?)(?=').+?(do burzy)|(z burzy)") - .firstMatch(novy); - if (match != null) { - burzaUrl = match.group(1)!.replaceAll("amp;", ""); - } - } - - return Jidlo( - varianta: j.varianta, - nazev: j.nazev, - objednano: !j.objednano, - cena: j.cena, - lzeObjednat: j.lzeObjednat, - orderUrl: orderUrl, - den: j.den, - burzaUrl: burzaUrl, - naBurze: (burzaUrl == null) - ? false - : !burzaUrl.contains("plusburza")); // vrátit upravenou instanci + return novy; // vrátit upravenou instanci } /// Získá aktuální jídla v burze @@ -508,6 +454,7 @@ class Canteen { /// Výstup: /// - [bool], `true`, pokud bylo jídlo úspěšně objednáno z burzy, jinak `false` Future objednatZBurzy(Burza b) async { + if (!prihlasen) return Future.error("Uživatel není přihlášen"); var res = await _getRequest("/faces/secured/" + b.url!); if (res.contains("Chyba")) return false; return true; diff --git a/pubspec.yaml b/pubspec.yaml index 0deef85..74dbac6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: canteenlib description: Library for communication with the czech canteen food ordering system iCanteen -version: 0.1.0-alpha.12 +version: 0.1.0-alpha.13 repository: 'https://github.com/hernikplays/canteenlib' issue_tracker: 'https://github.com/hernikplays/canteenlib/issues' documentation: 'https://docs.hernikplays.cz'