import express from 'express'
import {users} from './users'

export class UserController{
    login = (req: express.Request, res: express.Response)=>{
        let username = req.body.username;
        let password = req.body.password;

        let found  = false
        for(let i=0; i<users.length; i++){
            if(users[i].username==username && users[i].password==password){
                found = true
                res.json(users[i])
            }
        }
        if(!found) res.json(null)
    }

    register = (req: express.Request, res: express.Response)=>{
        let username = req.body.username;
        let password = req.body.password;
        let firstname = req.body.firstname;
        let lastname = req.body.lastname;

        let user = {
            username: username,
            password: password,
            firstname: firstname,
            lastname: lastname
        }

        users.push(user)
        console.log(users)
        res.json({message: "ok"})
    }
}