From c249f5fd60b7bf0d5a81aa04901b1aa6fdbb6a12 Mon Sep 17 00:00:00 2001 From: cereci5049 Date: Sun, 11 Aug 2024 02:23:25 +0530 Subject: [PATCH] 039 --- src/controller/signup.nim | 2 +- src/hh1.nim | 29 +++++------ src/lib/mics.nim | 24 ++++----- src/view/index.nimja | 88 ++++++++++++++++----------------- src/view/partials/_master.nimja | 10 ++-- 5 files changed, 74 insertions(+), 79 deletions(-) diff --git a/src/controller/signup.nim b/src/controller/signup.nim index 9d36a96..022d7ba 100644 --- a/src/controller/signup.nim +++ b/src/controller/signup.nim @@ -10,7 +10,7 @@ proc signup*(ctx: Context): string= if ctx.httpMethod == HttpGet: var user1 = ctx.cookies.getOrDefault("user1", "none") - user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile(".." / "view" / "signup.nimja", baseDir = getScriptDir()) elif ctx.httpMethod == HttpPost: var diff --git a/src/hh1.nim b/src/hh1.nim index f0881a5..16866c5 100644 --- a/src/hh1.nim +++ b/src/hh1.nim @@ -4,7 +4,6 @@ import strutils, json, strformat, - cookies, ./lib/[mics, htmx], ./db/users, ./model/[model, rand], @@ -16,57 +15,54 @@ import "/" -> [get, post]: var - user1 = ctx.cookies.getOrDefault("user1", """{"none": "none"}""") - echo user1 - # var user = to(parseJson(user1), User) + user1 = ctx.cookies.getOrDefault("user1", "") + user = getUserFromCookie(user1) + echo user.firstName compileTemplateFile("view" / "index.nimja", baseDir = getScriptDir()) "/shop" -> get: - # var user = ctx.getUserFromCookie(ctx.cookies.getOrDefault("user", "")) - # echo ctx.cookies.getOrDefault("user", "") var user1 = ctx.cookies.getOrDefault("user1", "none") - # user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "shop.nimja", baseDir = getScriptDir()) "/contact" -> get: var user1 = ctx.cookies.getOrDefault("user1", "none") - # user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "contact.nimja", baseDir = getScriptDir()) "/cart" -> get: - # var user = ctx.getUserFromCookie(ctx.cookies.getOrDefault("user", "")) var user1 = ctx.cookies.getOrDefault("user1", "none") - # user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "cart.nimja", baseDir = getScriptDir()) "/checkout" -> get: var user1 = ctx.cookies.getOrDefault("user1", "none") - user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "checkout.nimja", baseDir = getScriptDir()) "/shop/:id" -> get: var prodId = capitalizeAscii(ctx.pathParams["id"]) user1 = ctx.cookies.getOrDefault("user1", "none") - user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "shop-detail.nimja", baseDir = getScriptDir()) "/testimonial" -> get: var user1 = ctx.cookies.getOrDefault("user1", "none") - user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "testimonial.nimja", baseDir = getScriptDir()) -# "/signup" -> [get, post]: ctx.signup() +"/signup" -> [get, post]: ctx.signup() "/login" -> get: var user1 = ctx.cookies.getOrDefault("user1", "none") - # user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "login.nimja", baseDir = getScriptDir()) "/htmx/login" -> post: var @@ -100,7 +96,6 @@ import echo $(%* temp[1]) echo ctx.cookies ctx.response.headers.add("HX-Redirect", "/") - # ctx.send("hello") else: for a, b in form: val.name = b @@ -136,7 +131,7 @@ import "/:path" -> get: var user1 = ctx.cookies.getOrDefault("user1", "none") - user = to(parseJson(user1), User) + user = getUserFromCookie(user1) compileTemplateFile("view" / "404.nimja", baseDir = getScriptDir()) servePublic("src/public", "/static") diff --git a/src/lib/mics.nim b/src/lib/mics.nim index d366f43..8a56e56 100644 --- a/src/lib/mics.nim +++ b/src/lib/mics.nim @@ -12,15 +12,15 @@ proc newTurso*(): Turso = echo getEnv("DATABASE_URL") result = connect(getEnv("DATABASE_URL"), getEnv("AUTH_TOKEN")) -# proc getUserFromCookie*(ctx: Context, user: string): User = -# if user == "": -# return User( -# firstName: "", -# lastName: "", -# email: "", -# phone: "", -# password: "", -# accessLevel: 0 -# ) -# else: -# return to(parseJson(user), User) \ No newline at end of file +proc getUserFromCookie*(user: string): User = + if user == "": + return User( + firstName: "", + lastName: "", + email: "", + phone: "", + password: "", + accessLevel: 0 + ) + else: + return to(parseJson(user), User) \ No newline at end of file diff --git a/src/view/index.nimja b/src/view/index.nimja index ab5c951..0163151 100644 --- a/src/view/index.nimja +++ b/src/view/index.nimja @@ -20,11 +20,11 @@