split tags
This commit is contained in:
parent
8c341f5cdc
commit
6587e1867f
1 changed files with 40 additions and 29 deletions
69
src/user.ml
69
src/user.ml
|
|
@ -329,37 +329,48 @@ let add_plant tags files nick =
|
||||||
match files with
|
match files with
|
||||||
| files -> (
|
| files -> (
|
||||||
(* TODO parse tags*)
|
(* TODO parse tags*)
|
||||||
(* id for plant*)
|
let tags_len = String.length tags in
|
||||||
let ok_list = List.map (fun (_, content) -> is_valid_image content) files in
|
if tags_len > 1000 then
|
||||||
let valid_files = List.for_all (fun valid -> valid) ok_list in
|
Error "tags too long"
|
||||||
if not valid_files then
|
|
||||||
Error "Invalid image"
|
|
||||||
else
|
else
|
||||||
(* add plant to db *)
|
let tag_list = Str.split (Str.regexp " +") tags in
|
||||||
|
(* id for plant*)
|
||||||
|
let ok_list =
|
||||||
|
List.map (fun (_, content) -> is_valid_image content) files
|
||||||
|
in
|
||||||
|
let valid_files = List.for_all (fun valid -> valid) ok_list in
|
||||||
|
if not valid_files then
|
||||||
|
Error "Invalid image"
|
||||||
|
else
|
||||||
|
(* add plant to db *)
|
||||||
|
|
||||||
(* TODO make a plant_id *)
|
(* TODO make a plant_id *)
|
||||||
let plant_id = Uuidm.to_string (Uuidm.v4_gen random_state ()) in
|
let plant_id = Uuidm.to_string (Uuidm.v4_gen random_state ()) in
|
||||||
(* add to plant_id <-> user*)
|
(* add to plant_id <-> user*)
|
||||||
let res_plant = Db.exec Q.upload_plant_id (plant_id, nick) in
|
let res_plant = Db.exec Q.upload_plant_id (plant_id, nick) in
|
||||||
match res_plant with
|
match res_plant with
|
||||||
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
||||||
| Ok _ -> (
|
|
||||||
(* add to plant_id <-> tag table*)
|
|
||||||
(* TODO iter on tags*)
|
|
||||||
let res_tags = Db.exec Q.upload_plant_tag (plant_id, tags) in
|
|
||||||
match res_tags with
|
|
||||||
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
||||||
| Ok _ -> (
|
| Ok _ -> (
|
||||||
(* add to plant_id <-> image*)
|
(* add to plant_id <-> tag table*)
|
||||||
let res_images =
|
(* TODO iter on tag_list*)
|
||||||
List.find_opt Result.is_error
|
let res_tags =
|
||||||
(List.mapi
|
List.map
|
||||||
(fun nb (_, content) ->
|
(fun tag -> Db.exec Q.upload_plant_tag (plant_id, tag))
|
||||||
Db.exec Q.upload_plant_image (plant_id, content, nb) )
|
tag_list
|
||||||
files )
|
|
||||||
in
|
in
|
||||||
match res_images with
|
if List.exists Result.is_error res_tags then
|
||||||
| Some (Error e) ->
|
Error (Format.sprintf "db error")
|
||||||
Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
else
|
||||||
| Some (Ok _) -> assert false
|
(* add to plant_id <-> image*)
|
||||||
| None -> Ok () ) ) )
|
let res_images =
|
||||||
|
List.find_opt Result.is_error
|
||||||
|
(List.mapi
|
||||||
|
(fun nb (_, content) ->
|
||||||
|
Db.exec Q.upload_plant_image (plant_id, content, nb) )
|
||||||
|
files )
|
||||||
|
in
|
||||||
|
match res_images with
|
||||||
|
| Some (Error e) ->
|
||||||
|
Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
||||||
|
| Some (Ok _) -> assert false
|
||||||
|
| None -> Ok () ) )
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue