Merge pull request 'ayushman' (#35) from ayushman into main

Reviewed-on: #35
This commit is contained in:
cereci5049 2024-08-18 13:40:53 +02:00
commit 9b7acdcb0b
7 changed files with 57 additions and 46 deletions

View file

@ -8,7 +8,7 @@ proc htmxEmail*(ctx:Context)=
em = ctx.urlForm["em"]
val: Validity
if em == "":
val.message = "Last Name is Required"
val.message = "Email is Required"
val.mark = "is-invalid"
else:
val.message = ""
@ -20,7 +20,7 @@ proc htmxPassword*(ctx:Context)=
pw = ctx.urlForm["pw"]
val: Validity
if pw == "":
val.message = "Last Name is Required"
val.message = "Password is Required"
val.mark = "is-invalid"
else:
val.message = ""
@ -44,7 +44,7 @@ proc htmxLastName*(ctx:Context)=
ln = ctx.urlForm["ln"]
val: Validity
if ln == "":
val.message = "First Name is Required"
val.message = "Last Name is Required"
val.mark = "is-invalid"
else:
val.message = ""
@ -56,9 +56,9 @@ proc htmxPhone*(ctx:Context)=
ph = ctx.urlForm["ph"]
val: Validity
if ph == "":
val.message = "First Name is Required"
val.message = "Phone Number is Required"
val.mark = "is-invalid"
else:
val.message = ""
val.mark = ""
ctx.send htmxPhone(ph,val)
ctx.send htmxPhone(ph,val)

View file

@ -10,13 +10,13 @@ proc htmxLogin*(ctx: Context) =
var
form = ctx.urlForm
em =form["em"]
ph =form["ph"]
ps=form["pw"]
val: Validity
validity = initTable[string, Validity]()
if em.len==0 or ps.len==0:
if ph.len==0 or ps.len==0:
for a, b in form:
if form[a] == "":
val.name = ""
@ -31,7 +31,7 @@ proc htmxLogin*(ctx: Context) =
ctx.send(htmxLogin(validity, ""))
let conn=newTurso()
var temp=getUser(conn, em, ps)
var temp=getUser(conn, ph, ps)
if temp[0] == true:
ctx &= initCookie("user1", $(%* temp[1]))
echo $(%* temp[1])
@ -43,4 +43,4 @@ proc htmxLogin*(ctx: Context) =
val.message = ""
val.mark = ""
validity[a] = val
ctx.send(htmxLogin(validity, "Invalid Email or Password"))
ctx.send(htmxLogin(validity, "Invalid Email or Password"))

View file

@ -17,18 +17,19 @@ proc signup*(ctx: Context): string=
form = ctx.urlForm
firstName = form["fn"]
lastName = form["ln"]
email = form["em"]
phone = form["ph"]
password = form["pw"]
userType = form["ut"]
conn = newTurso()
val: Validity
validity = initTable[string, Validity]()
echo firstName, lastName, email, password
echo firstName, lastName, password, userType, phone
if firstName.len == 0 or lastName.len == 0 or email.len == 0 or phone.len == 0 or password.len == 0:
if firstName.len == 0 or lastName.len == 0 or phone.len == 0 or password.len == 0:
echo "lllo"
for a, b in form:
if form[a] == "":
val.name = ""
@ -45,7 +46,6 @@ proc signup*(ctx: Context): string=
var user = User(
firstName: firstName,
lastName: lastName,
email: email,
phone: phone,
password: password,
accessLevel: 1

View file

@ -12,7 +12,7 @@ proc setupUsers*(conn: Turso) =
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(255) NOT NULL,
lastName VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
email VARCHAR(255),
phone VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
createdAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
@ -23,13 +23,14 @@ proc setupUsers*(conn: Turso) =
proc createPost*(conn: Turso, user: User)=
## createPost inserts a new user into the Users table
conn.execute(fmt"""INSERT INTO users (firstName, lastName, email, phone, password, accessLevel) VALUES ("{user.firstName}", "{user.lastName}", "{user.email}", "{user.phone}", "{user.password}", {user.accessLevel});""")
echo "lllo1"
conn.execute(fmt"""INSERT INTO Users (firstName, lastName, phone, password, accessLevel) VALUES ("{user.firstName}", "{user.lastName}", "{user.phone}", "{user.password}", {user.accessLevel});""")
proc getUser*(conn: Turso, email: string, password: string): (bool, User)=
proc getUser*(conn: Turso, phone: string, password: string): (bool, User)=
## getUser retrieves a user from the Users table
echo "h1"
echo phone, password
var
data = conn.getData(fmt"""SELECT * FROM users WHERE email = "{email}" and password = "{password}";""")
data = conn.getData(fmt"""SELECT * FROM Users WHERE phone = "{phone}" and password = "{password}";""")
user = User(
firstName: "",
lastName: "",
@ -40,16 +41,13 @@ proc getUser*(conn: Turso, email: string, password: string): (bool, User)=
)
if data["rows"].len > 0:
echo "h2"
user = User(
firstName: data["rows"][0][1]["value"].getStr(),
lastName: data["rows"][0][2]["value"].getStr(),
email: data["rows"][0][3]["value"].getStr(),
phone: data["rows"][0][4]["value"].getStr(),
password: data["rows"][0][5]["value"].getStr(),
accessLevel: parseInt(data["rows"][0][8]["value"].getStr())
)
result = (true, user)
else:
echo "h3"
result = (false, user)
result = (false, user)

View file

@ -89,15 +89,6 @@ proc htmxSignup*(val: Table[string, Validity], message: string): string =
<input type="text" name="ln" class="form-control {val["ln"].mark}" required autocomplete="off" value="{val["ln"].name}">
</div>
<div class="form-group">
<label for="email">Email:</label>
<label class="text-danger">{val["em"].message}</label>
<input type="email" name="em" class="form-control {val["em"].mark}" required autocomplete="off" value="{val["em"].name}">
</div>
<div class="form-group">
<label for="email">Phone:</label>
<label class="text-danger">{val["ph"].message}</label>
@ -143,7 +134,7 @@ proc htmxFirstName*(firstName: string, input: Validity): string =
proc htmxLastName*(lastName: string, input: Validity): string =
result = fmt"""
<div class="form-group mt-5"
<div class="form-group"
hx-target="this"
hx-swap="outerHTML"
>
@ -158,7 +149,7 @@ proc htmxLastName*(lastName: string, input: Validity): string =
proc htmxPhone*(phone: string, input: Validity): string =
result = fmt"""
<div class="form-group mt-5"
<div class="form-group"
hx-target="this"
hx-swap="outerHTML"
>

View file

@ -19,11 +19,13 @@
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">Email:</label>
<label for="email">Phone:</label>
<label class="text-danger"></label>
<input type="email" name="em" id="email" class="form-control" required autocomplete="off"
<input type="email" name="ph" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/email"
hx-post="/htmx/phone"
>
</div>
@ -50,4 +52,4 @@
</div>
</div>
{% endblock %}
{% endblock %}

View file

@ -31,27 +31,39 @@
</div>
<div class="form-group">
<div class="form-group"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">Last Name:</label>
<label class="text-danger"></label>
<input type="text" name="ln" class="form-control" required autocomplete="off">
<input type="text" name="ln" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/lname"
>
</div>
<div class="form-group">
<label for="email">Email:</label>
<label class="text-danger"></label>
<!-- <div class="form-group"> -->
<!-- <label for="email">Email:</label> -->
<!-- <label class="text-danger"></label> -->
<input type="email" name="em" class="form-control" required autocomplete="off">
<!-- <input type="email" name="em" class="form-control" required autocomplete="off"> -->
</div>
<!-- </div> -->
<div class="form-group">
<div class="form-group"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">Phone:</label>
<label class="text-danger"></label>
<input type="email" name="ph" class="form-control" required autocomplete="off">
<input type="email" name="ph" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/phone"
>
</div>
@ -68,6 +80,14 @@
</div>
<div class="form-group">
<label for="user_type" class="text-black">User Type:</label>
<select id="user_type" class="form-control" name="ut">
<option value="1">User</option>
<option value="2">Seller</option>
</select>
</div>
<hr>
<button id="b4" type="submit" class="btn btn-primary">Submit</button>