From f6ce580eeca9d359aee8ae87e5b58f17d26701e5 Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 7 Jun 2024 15:01:27 +0200 Subject: [PATCH] fix: save poi to pb --- front/src/pages/maps/save_poi.astro | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 front/src/pages/maps/save_poi.astro diff --git a/front/src/pages/maps/save_poi.astro b/front/src/pages/maps/save_poi.astro new file mode 100644 index 0000000..df7120f --- /dev/null +++ b/front/src/pages/maps/save_poi.astro @@ -0,0 +1,47 @@ +--- +import AstroUtils from "libs/AstroUtils"; +import type { RecordModel } from "pocketbase"; + + +const pb = Astro.locals.pb + +if(!pb.authStore.isValid){ + return Astro.redirect("/account") +} + +await AstroUtils.wrap(async () => { + if (Astro.request.method !== 'POST') { + return + } + const poiJson = await Astro.request.json() + + const data = {Poi: poiJson.Poi, Poi_id:poiJson.Poi.id} + + let record: RecordModel + + try { + record = await pb.collection('POI').create(data); + } catch (error) { + try{ + record = await pb.collection('POI').getFirstListItem(`Poi_id="${poiJson.Poi.id}"`) + } catch (error2) { + console.log("error 1 :") + console.log(error) + console.log("error 2 :") + console.log(error2) + } + } + + try { + if(poiJson.save){ + pb.collection('user_poi').create({owner: pb.authStore.model!.id, poi_list:record!.id}) + }else{ + record = await pb.collection('user_poi').getFirstListItem(`poi_list="${record!.id}"&&owner="${pb.authStore.model!.id}"`) + pb.collection('user_poi').delete(record.id) + } + } catch (error) { + console.log(error) + } + +}) +---