From 706dfaf0f7861258a167bb3f0ca3d0ac97f0be11 Mon Sep 17 00:00:00 2001 From: Swrup Date: Tue, 14 Dec 2021 23:47:26 +0100 Subject: [PATCH] add latlng to /add_plant form by clicking map --- src/add_plant.eml.html | 3 ++- src/map.ml | 17 ++++++++--------- src/permap.ml | 22 +++++++++++++++------- src/user.ml | 20 +++++++++++--------- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/add_plant.eml.html b/src/add_plant.eml.html index 1bf68d9..9c567ad 100644 --- a/src/add_plant.eml.html +++ b/src/add_plant.eml.html @@ -3,7 +3,8 @@ let f nick request =
<%s! Dream.form_tag ~action:"/add_plant" ~enctype:`Multipart_form_data request %> - + + diff --git a/src/map.ml b/src/map.ml index 4c2e000..1f90c59 100644 --- a/src/map.ml +++ b/src/map.ml @@ -50,19 +50,18 @@ let () = let on_click e = log "on_click@."; + (*TODO use Jv.find *) let lat_lng = Jv.get e "latlng" in ignore @@ Jv.call popup "setLatLng" [| lat_lng |]; - ignore @@ Jv.call popup "setContent" [| Jv.of_string "YOU CLICKED HERE" |]; + ignore @@ Jv.call popup "setContent" [| Jv.of_string "euujjj" |]; ignore @@ Jv.call popup "openOn" [| map |]; - (*TODO use Brr to insert lat_lng in the form *) - let open Brr in - let lat_lng_input = El.input ~at:At.[ id (Jstr.v "lat_lng") ] () in - ignore - @@ El.set_at (Jstr.of_string "value") - (Some (Jstr.of_string "FUCK")) - lat_lng_input; - () + let lat = Jv.get lat_lng "lat" in + let lng = Jv.get lat_lng "lng" in + let lat_input = Jv.get Jv.global "lat_input" in + let lng_input = Jv.get Jv.global "lng_input" in + ignore @@ Jv.call lat_input "setAttribute" [| Jv.of_string "value"; lat |]; + ignore @@ Jv.call lng_input "setAttribute" [| Jv.of_string "value"; lng |] let () = (*add on_click callback to map*) diff --git a/src/permap.ml b/src/permap.ml index 643454a..3994542 100644 --- a/src/permap.ml +++ b/src/permap.ml @@ -154,17 +154,25 @@ let add_plant_post request = | None -> render_unsafe "Not logged in" request | Some nick -> ( match%lwt Dream.multipart request with - | `Ok [ ("files", files); ("lat_lng", lat_lng); ("tags", tags) ] - | `Ok (("files", files) :: ("lat_lng", lat_lng) :: ("tags", tags) :: _ :: _) - -> ( + | `Ok + [ ("files", files) + ; ("lat_input", lat) + ; ("lng_input", lng) + ; ("tags", tags) + ] + | `Ok + (("files", files) + :: ("lat_input", lat) :: ("lng_input", lng) :: ("tags", tags) :: _ :: _ + ) -> ( match tags with | [] -> render_unsafe "Field tag is empty" request | [ (_, tags) ] -> ( - match lat_lng with - | [] -> render_unsafe "Field tag is empty" request - | [ (_, lat_lng) ] -> + match (lat, lng) with + | [], _ -> render_unsafe "Field tag is empty" request + | _, [] -> render_unsafe "Field tag is empty" request + | [ (_, lat) ], [ (_, lng) ] -> let res = - match User.add_plant lat_lng tags files nick with + match User.add_plant (lat, lng) tags files nick with | Ok () -> "Your plant was uploaded!" | Error e -> e in diff --git a/src/user.ml b/src/user.ml index 7a97183..9079c86 100644 --- a/src/user.ml +++ b/src/user.ml @@ -29,8 +29,8 @@ module Q = struct let create_plant_gps_table = Caqti_request.exec Caqti_type.unit - "CREATE TABLE IF NOT EXISTS plant_gps (plant_id TEXT, gps TEXT, FOREIGN \ - KEY(plant_id) REFERENCES plant_user(plant_id));" + "CREATE TABLE IF NOT EXISTS plant_gps (plant_id TEXT, lat TEXT,lng TEXT, \ + FOREIGN KEY(plant_id) REFERENCES plant_user(plant_id));" let get_password = Caqti_request.find_opt Caqti_type.string Caqti_type.string @@ -86,8 +86,8 @@ module Q = struct let upload_plant_gps = Caqti_request.exec - Caqti_type.(tup2 string string) - "INSERT INTO plant_gps VALUES (?,?);" + Caqti_type.(tup3 string string string) + "INSERT INTO plant_gps VALUES (?,?,?);" let upload_plant_image = Caqti_request.exec @@ -113,8 +113,9 @@ module Q = struct "SELECT tag FROM plant_tag WHERE plant_id=?;" let get_plant_gps = - Caqti_request.find_opt Caqti_type.string Caqti_type.string - "SELECT gps FROM plant_gps WHERE plant_id=?;" + Caqti_request.find_opt Caqti_type.string + Caqti_type.(tup2 string string) + "SELECT lat, lng FROM plant_gps WHERE plant_id=?;" end module Db = @@ -207,7 +208,7 @@ let view_plant plant_id = | Some count -> ( let gps = match Db.find_opt Q.get_plant_gps plant_id with - | Ok (Some gps) -> gps + | Ok (Some (lat, lng)) -> lat ^ " " ^ lng | Ok None -> "" | Error e -> Format.sprintf "db error: %s" (Caqti_error.show e) in @@ -333,7 +334,7 @@ let upload_avatar files nick = (* TODO do the same for text input: check length, forbidden chars and have a forbidden words filter*) let is_valid_image _content = true -let add_plant gps tags files nick = +let add_plant (lat, lng) tags files nick = let tags_len = String.length tags in if tags_len > 1000 then Error "tags too long" @@ -353,7 +354,8 @@ let add_plant gps tags files nick = | Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e)) | Ok _ -> ( (* add to plant_id <-> gps table*) - let res_gps = Db.exec Q.upload_plant_gps (plant_id, gps) in + (*TODO check if valid latlng *) + let res_gps = Db.exec Q.upload_plant_gps (plant_id, lat, lng) in match res_gps with | Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e)) | Ok _ -> (