188 lines
5.6 KiB
V
188 lines
5.6 KiB
V
module main
|
|
|
|
import veb
|
|
|
|
@['/controller/user/create'; post]
|
|
pub fn (mut app App) controller_create_user(mut ctx Context, first_name string, last_name string, email string, password string, gender string) veb.Result {
|
|
// Create a map of field names and their values
|
|
fields := {
|
|
'first_name': first_name
|
|
'last_name': last_name
|
|
'email': email
|
|
'password': password
|
|
'gender': gender
|
|
}
|
|
|
|
// Check for empty fields
|
|
mut empty_fields := []string{}
|
|
for field_name, value in fields {
|
|
if value == '' {
|
|
empty_fields << field_name
|
|
}
|
|
}
|
|
|
|
// If any fields are empty, return field-specific error messages
|
|
if empty_fields.len > 0 {
|
|
mut response := '<script>'
|
|
|
|
// Reset all field errors first
|
|
response += 'document.querySelectorAll(".field-error").forEach(el => el.textContent = "");'
|
|
response += 'document.querySelectorAll("input").forEach(el => el.classList.remove("input-error"));'
|
|
|
|
// Set error for each empty field
|
|
for field in empty_fields {
|
|
response += 'document.querySelector(".' + field + '-error").textContent = "(Required)";'
|
|
response += 'document.querySelector("[name=' + field +
|
|
']").classList.add("input-error");'
|
|
}
|
|
|
|
response += '</script>'
|
|
response += '<div class="alert alert-danger">Please fill in all required fields</div>'
|
|
|
|
return ctx.html(response)
|
|
}
|
|
|
|
// Try to add the user
|
|
app.service_add_user(first_name, last_name, email, password, gender) or {
|
|
error_html := '<div class="alert alert-danger">Error: ${err}</div>'
|
|
return ctx.html(error_html)
|
|
}
|
|
|
|
if x := app.service_find_user_by_email(email, password) {
|
|
// Generate and insert the token using user ID
|
|
token := app.auth.add_token(x.id) or { '' }
|
|
// Authenticate the user by adding the token to the cookies
|
|
ctx.set_cookie(name: 'token', value: token, path: '/')
|
|
}
|
|
|
|
// Return success message with HTML
|
|
success_html := '<div class="alert alert-success">
|
|
<h4>User created successfully!</h4>
|
|
<p>Welcome, ${first_name}! Your account has been created.</p>
|
|
<a href="/login" class="btn btn-primary mt-3">Login Now</a>
|
|
</div>'
|
|
|
|
return ctx.html(success_html)
|
|
}
|
|
|
|
@['/controller/user'; post]
|
|
pub fn (mut app App) controller_get_user(mut ctx Context, email string, password string) veb.Result {
|
|
// Create a map of field names and their values
|
|
fields := {
|
|
'email': email
|
|
'password': password
|
|
}
|
|
|
|
// Check for empty fields
|
|
mut empty_fields := []string{}
|
|
for field_name, value in fields {
|
|
if value == '' {
|
|
empty_fields << field_name
|
|
}
|
|
}
|
|
|
|
// If any fields are empty, return field-specific error messages
|
|
if empty_fields.len > 0 {
|
|
mut response := '<script>'
|
|
|
|
// Reset all field errors first
|
|
response += 'document.querySelectorAll(".field-error").forEach(el => el.textContent = "");'
|
|
response += 'document.querySelectorAll("input").forEach(el => el.classList.remove("input-error"));'
|
|
|
|
// Set error for each empty field
|
|
for field in empty_fields {
|
|
response += 'document.querySelector(".' + field + '-error").textContent = "(Required)";'
|
|
response += 'document.querySelector("[name=' + field +
|
|
']").classList.add("input-error");'
|
|
}
|
|
|
|
response += '</script>'
|
|
response += '<div class="alert alert-danger">Please fill in all required fields</div>'
|
|
|
|
return ctx.html(response)
|
|
}
|
|
|
|
// Try to find the user
|
|
user := app.service_find_user_by_email(email, password) or {
|
|
error_html := '<div class="alert alert-danger">Invalid email or password</div>'
|
|
return ctx.html(error_html)
|
|
}
|
|
|
|
token := app.auth.add_token(user.id) or { '' }
|
|
// Authenticate the user by adding the token to the cookies
|
|
ctx.set_cookie(name: 'token', value: token, path: '/')
|
|
|
|
// Return success message with HTML and redirect
|
|
success_html := '<div class="alert alert-success">
|
|
<h4>Login successful!</h4>
|
|
<p>Welcome back, ${user.first_name}!</p>
|
|
</div>
|
|
<script>
|
|
setTimeout(function() {
|
|
window.location.href = "/";
|
|
}, 2000);
|
|
</script>'
|
|
|
|
return ctx.html(success_html)
|
|
}
|
|
|
|
@['/controller/user/update'; post]
|
|
pub fn (app &App) controller_update_user(mut ctx Context, first_name string, last_name string, password string) veb.Result {
|
|
// Create a map of field names and their values
|
|
fields := {
|
|
'first_name': first_name
|
|
'last_name': last_name
|
|
'password': password
|
|
}
|
|
|
|
// Check for empty fields
|
|
mut empty_fields := []string{}
|
|
for field_name, value in fields {
|
|
if value == '' {
|
|
empty_fields << field_name
|
|
}
|
|
}
|
|
|
|
// If any fields are empty, return field-specific error messages
|
|
if empty_fields.len > 0 {
|
|
mut response := '<script>'
|
|
|
|
// Reset all field errors first
|
|
response += 'document.querySelectorAll(".field-error").forEach(el => el.textContent = "");'
|
|
response += 'document.querySelectorAll("input").forEach(el => el.classList.remove("input-error"));'
|
|
|
|
// Set error for each empty field
|
|
for field in empty_fields {
|
|
response += 'document.querySelector(".' + field + '-error").textContent = "(Required)";'
|
|
response += 'document.querySelector("[name=' + field +
|
|
']").classList.add("input-error");'
|
|
}
|
|
|
|
response += '</script>'
|
|
response += '<div class="alert alert-danger">Please fill in all required fields</div>'
|
|
|
|
return ctx.html(response)
|
|
}
|
|
|
|
id := ctx.get_cookie('token')
|
|
|
|
// Try to update the user
|
|
app.service_update_user(id, first_name, last_name, password) or {
|
|
error_html := '<div class="alert alert-danger">Error: ${err}</div>'
|
|
return ctx.html(error_html)
|
|
}
|
|
|
|
// Return success message with HTML
|
|
success_html := '<div class="alert alert-success">
|
|
<h4>Profile Updated Successfully!</h4>
|
|
<p>Your profile information has been updated.</p>
|
|
</div>
|
|
<script>
|
|
setTimeout(function() {
|
|
window.location.href = "/profile";
|
|
}, 2000);
|
|
</script>'
|
|
|
|
return ctx.html(success_html)
|
|
}
|