import express from 'express'
import UserM from '../models/user'

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

        UserM.findOne({username: u, password: p}).then((user)=>{
            res.json(user)
        }).catch((err)=>{
            console.log(err)
            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
        }

        new UserM(user).save().then(ok=>{
            res.json({message: "ok"})
        }).catch(err=>{
            console.log(err)
            res.json({message: "fail register"})
        })
    }
}