var db = require('../models/dbCon');
const { Sequelize,Op } = require('sequelize');
const users= db.users;
const User = require('../models/User.js');

class UserService {


    getUser = async (username) => {          
        try {
            let result = await users.findAll({ where:{userName:username}});
            let user = new User();
             if(result!=undefined && result!=null){
                user.password = result[0].password;
                user.id = result[0].id;
                user.name = result[0].name;
                user.username = result[0].userName;     
             }
           return user;
        }
        catch(e) {
            console.log(e);
        }
    }

    adduser = async (req, res)=> {       
        try {
            let info = {
                userName:req.body.username,
                name:req.body.username,
                password:req.body.password,
                isActive:1
            }
            if(req.body!=null && req.body!=undefined)
            {
              const result = await users.create(info)
               res.status(200).json({
                  message: 'Success',
                  data:result,
                  code:200
              })
              }
             else
              return res.sendStatus(400).json({
                message:'Failed',
                code:400
               });
        }
        catch(e) {
            console.log(e);
            res.sendStatus(500);
        }
    }

};
module.exports = UserService;