#[Copyright 2025 ITwrx.
This file is part of Simple Site Manager.
Simple Site Manager is released under the GNU Affero General Public License 3.0.
See COPYING or <https://www.gnu.org/licenses/> for details.]#

import sqliteral, "../helpers/db"

type
  User* = object
    email*, password*: string
    id*: int

proc getUserByEmail*(email: string): User =
  {.gcsafe.}:
    var user: User
    for row in db1.rows(SelectUsersByEmail, email): 
      user.id = row.getInt(0)
      user.email = row.getString(1)
      user.password = row.getString(2)
    return user
  
proc getEmailByUserId*(id: string): string =
  {.gcsafe.}:
    var user: User
    for row in db1.rows(SelectUserById, id): 
      user.id = row.getInt(0)
      user.email = row.getString(1)
    return user.email
  
proc createUser*(email: string, encodedHash: string) =
  {.gcsafe.}:
    db1.transaction:
      discard db1.insert(InsertUser, email, encodedHash)
