add bio in user page, there is a problem with matching on the SELECT btw, idk wtf is the type
This commit is contained in:
parent
152cefa9e4
commit
302525f2ab
4 changed files with 86 additions and 9 deletions
8
src/dune
8
src/dune
|
|
@ -1,6 +1,6 @@
|
|||
(executable
|
||||
(public_name permap)
|
||||
(modules app content db login permap register template user)
|
||||
(modules app content db login permap register template user user_profile)
|
||||
(libraries
|
||||
bos
|
||||
directories
|
||||
|
|
@ -31,6 +31,12 @@
|
|||
(action
|
||||
(run dream_eml %{deps} --workspace %{workspace_root})))
|
||||
|
||||
(rule
|
||||
(targets user_profile.ml)
|
||||
(deps user_profile.eml.html)
|
||||
(action
|
||||
(run dream_eml %{deps} --workspace %{workspace_root})))
|
||||
|
||||
(rule
|
||||
(target content.ml)
|
||||
(deps
|
||||
|
|
|
|||
|
|
@ -64,7 +64,12 @@ let logout request =
|
|||
let content = "Logged out !" in
|
||||
render_unsafe content request
|
||||
|
||||
let profile request = render_unsafe (User.profile request) request
|
||||
let profile_get request = render_unsafe (User_profile.f request) request
|
||||
|
||||
let profile_post request =
|
||||
match%lwt Dream.form request with
|
||||
| `Ok [ ("bio", bio) ] -> render_unsafe (User_profile.f ~bio request) request
|
||||
| _ -> assert false
|
||||
|
||||
let () =
|
||||
Dream.run @@ Dream.logger @@ Dream.memory_sessions
|
||||
|
|
@ -78,6 +83,7 @@ let () =
|
|||
; Dream.get "/user" user
|
||||
; Dream.get "/user/:user" user_profile
|
||||
; Dream.get "/logout" logout
|
||||
; Dream.get "/profile" profile
|
||||
; Dream.get "/profile" profile_get
|
||||
; Dream.post "/profile" profile_post
|
||||
]
|
||||
@@ Dream.not_found
|
||||
|
|
|
|||
48
src/user.ml
48
src/user.ml
|
|
@ -2,6 +2,7 @@ type t =
|
|||
{ nick : string
|
||||
; password : string
|
||||
; email : string (* TODO: make email optional ? *)
|
||||
; bio : string
|
||||
}
|
||||
|
||||
let () =
|
||||
|
|
@ -10,7 +11,7 @@ let () =
|
|||
Db.with_db (fun db ->
|
||||
exec0 db
|
||||
"CREATE TABLE IF NOT EXISTS user (nick TEXT, password TEXT, email \
|
||||
TEXT);" )
|
||||
TEXT, bio TEXT);" )
|
||||
in
|
||||
match res with
|
||||
| Ok () -> ()
|
||||
|
|
@ -76,8 +77,12 @@ let register ~email ~nick ~password =
|
|||
| Ok [ [| Data.INT 0L |] ] -> (
|
||||
let res =
|
||||
Db.with_db (fun db ->
|
||||
exec_raw_args db "INSERT INTO user VALUES (?, ?, ?);"
|
||||
[| Data.TEXT nick; Data.TEXT password; Data.TEXT email |]
|
||||
exec_raw_args db "INSERT INTO user VALUES (?, ?, ?, ?);"
|
||||
[| Data.TEXT nick
|
||||
; Data.TEXT password
|
||||
; Data.TEXT email
|
||||
; Data.TEXT ""
|
||||
|]
|
||||
~f:Cursor.to_list )
|
||||
in
|
||||
match res with
|
||||
|
|
@ -111,9 +116,11 @@ let public_profile request =
|
|||
~f:Cursor.to_list )
|
||||
in
|
||||
match user with
|
||||
| Ok [ [| Data.TEXT nick; Data.TEXT password; Data.TEXT email |] ] ->
|
||||
Format.sprintf "nick = `%s`; password = `%s`; email = `%s`" nick password
|
||||
email
|
||||
| Ok
|
||||
[ [| Data.TEXT nick; Data.TEXT password; Data.TEXT email; Data.TEXT bio |]
|
||||
] ->
|
||||
Format.sprintf "nick = `%s`; password = `%s`; email = `%s`; bio = '%s'" nick
|
||||
password email bio
|
||||
| Ok _ -> "incoherent db answer"
|
||||
| Error e -> Format.sprintf "db error: %s" (Rc.to_string e)
|
||||
|
||||
|
|
@ -121,3 +128,32 @@ let profile request =
|
|||
match Dream.session "nick" request with
|
||||
| None -> "not logged in"
|
||||
| Some nick -> Format.sprintf "Hello %s !" nick
|
||||
|
||||
let update_bio bio nick =
|
||||
let valid = true in
|
||||
(* TODO check bio len and FORBIDEN WORDS *)
|
||||
if not valid then
|
||||
Error "Not biologic"
|
||||
else
|
||||
let open Sqlite3_utils in
|
||||
let res =
|
||||
Db.with_db (fun db ->
|
||||
exec_raw_args db "UPDATE user SET bio=? WHERE nick=?;"
|
||||
[| Data.TEXT bio; Data.TEXT nick |]
|
||||
~f:Cursor.to_list )
|
||||
in
|
||||
match res with
|
||||
| Ok _ -> Ok ()
|
||||
| Error e -> Error (Format.sprintf "db error: %s" (Rc.to_string e))
|
||||
|
||||
let get_bio nick =
|
||||
let open Sqlite3_utils in
|
||||
let res =
|
||||
Db.with_db (fun db ->
|
||||
exec_raw_args db "SELECT bio FROM user WHERE nick=?;"
|
||||
[| Data.TEXT nick |] ~f:Cursor.to_list )
|
||||
in
|
||||
match res with
|
||||
| Ok [ [| Data.TEXT bio |] ] -> Ok bio
|
||||
| Error e -> Error (Format.sprintf "db error: %s" (Rc.to_string e))
|
||||
| Ok _ -> Error "incoherent db result"
|
||||
|
|
|
|||
29
src/user_profile.eml.html
Normal file
29
src/user_profile.eml.html
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
let f ?bio request =
|
||||
% begin match Dream.session "nick" request with
|
||||
% | None ->
|
||||
not logged in
|
||||
% | Some nick ->
|
||||
%begin match bio with
|
||||
% | None ->
|
||||
% let bio = match User.get_bio nick with
|
||||
% | Ok bio -> bio
|
||||
% | Error e -> e
|
||||
%in
|
||||
<%s Format.sprintf "Hello %s !" nick %>
|
||||
<%s! Dream.form_tag ~action:"/profile" request %>
|
||||
<div class="mb-3">
|
||||
<label for="bio" class="form-label">Bio</label>
|
||||
<input name="bio" type="text" class="form-control" id="bio" aria-describedby="bioHelp" value="<%s bio %>" </input>
|
||||
<div id="bioHelp" class="form-text">Who are you?</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
</form>
|
||||
% | Some bio ->
|
||||
% begin match User.update_bio bio nick with
|
||||
% | Ok () ->
|
||||
Bio updated !
|
||||
% | Error e ->
|
||||
<%s e %>
|
||||
% end;
|
||||
% end;
|
||||
%end;
|
||||
Loading…
Add table
Add a link
Reference in a new issue