From c2597788d7be5f7231a3702b9ddfdfb50212ed55 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 29 Dec 2022 21:30:52 +0200 Subject: [PATCH] - Implemented `viewRecord()` - Added unittests for `viewRecord()` --- source/libpb.d | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/source/libpb.d b/source/libpb.d index 12b1537..7c21d6c 100644 --- a/source/libpb.d +++ b/source/libpb.d @@ -84,6 +84,29 @@ public class PocketBase } } + public RecordType viewRecord(RecordType)(string table, string id) + { + RecordType recordOut; + + try + { + string responseData = cast(string)get(pocketBaseURL~"collections/"~table~"/records/"~id); + JSONValue responseJSON = parseJSON(responseData); + + recordOut = fromJSON!(RecordType)(responseJSON); + + return recordOut; + } + catch(CurlException e) + { + throw new PBException(PBException.ErrorType.CURL_NETWORK_ERROR, e.msg); + } + catch(JSONException e) + { + throw new PBException(PBException.ErrorType.JSON_PARSE_ERROR, e.msg); + } + } + public RecordType updateRecord(string, RecordType)(string table, RecordType item) { idAbleCheck(item); @@ -401,6 +424,7 @@ unittest unittest { import core.thread : Thread, dur; + import std.string : cmp; PocketBase pb = new PocketBase(); @@ -426,5 +450,10 @@ unittest assert(recordStored.age == 46); Thread.sleep(dur!("seconds")(3)); + Person recordFetched = pb.viewRecord!(Person)("dummy", recordStored.id); + assert(recordFetched.age == 46); + assert(cmp(recordFetched.name, "Tristan Gonzales") == 0); + assert(cmp(recordFetched.id, recordStored.id) == 0); + pb.deleteRecord("dummy", recordStored); }