ThomasTJdev / nim_websitecreator

Nim fullstack website framework - deploy a website within minutes
https://nimwc.org
MIT License
177 stars 7 forks source link

Import modules (procs) or keep them in routes.nim #2

Closed ThomasTJdev closed 6 years ago

ThomasTJdev commented 6 years ago

To keep the source code simple, most of the work are kept in the routes.nim.

Should this "work" be split in procs, which are imported? By doing so it is possible for plugins to import these procs.

E.g. when creating a user (post "/users/add"). The plugin openregistration includes a lot of the same source code. The amount of code could be smaller when plugins has access to this. But will the procs be flexible enough? Maybe the question is: what to include in routes vs modules..

post "/users/add":
  createTFD()
  if c.loggedIn and c.rank in [Admin, Moderator]:
    when defined(demo):
      if c.email == "test@test.com":
        redirect("/error/" & encodeUrl("Error: The test user can not add new users"))

    cond(@"status" in ["User", "Moderator", "Admin", "Deactivated"])

    if (c.rank != Admin and @"status" == "Admin") or c.rank == User:
      redirect("/error/" & encodeUrl("Error: You are not allowed to add a user with this status"))

    if @"name" == "" or @"email" == "" or @"status" == "":
      redirect("/error/" & encodeUrl("Error: Name, email and status are required"))

    if @"email" == "test@test.com":
      redirect("/error/" & encodeUrl("Error: test@test.com is taken by the system"))

    if not ("@" in email and "." in email):
      redirect("/error/" & encodeUrl("Error: Your email has a wrong format"))

    let emailExist = getValue(db, sql"SELECT id FROM person WHERE email = ?", @"email")
    if emailExist != "":
      redirect("/error/" & encodeUrl("Error: A user with that email does already exists"))

    let salt = makeSalt()
    let passwordOriginal = randomString(12)
    let password = makePassword(passwordOriginal, salt)
    let secretUrl = randomStringDigitAlpha(99)

    let userID = insertID(db, sql"INSERT INTO person (name, email, status, password, salt, secretUrl) VALUES (?, ?, ?, ?, ?, ?)", @"name", @"email", @"status", password, salt, secretUrl)

    asyncCheck sendEmailActivationManual(@"email", @"name", passwordOriginal, "/users/activate?id=" & $userID & "&ident=" & secretUrl, c.username)

    redirect("/users")
ThomasTJdev commented 6 years ago

It's a minor problem now. Code will be moved to modules over time.