import { Router } from 'express';
import { AppDataSource } from '../data-source';
import { authentificationToken } from '../middleware/auth';
import { User } from '../entity/User';

const router = Router();

 router.get('/users', authentificationToken, async function (req, res) {
        let UserTypeRepository = AppDataSource.getRepository(User);
        let users = await UserTypeRepository.find();
        res.send(users);
});

router.put('/updateuser/:id', authentificationToken, async function (req, res) {
        const UserTypeRepository = AppDataSource.getRepository(User);
        const user = await UserTypeRepository.findOneBy({ id: parseInt(req.params.id) });
        UserTypeRepository.merge(user, req.body);
        const results = await UserTypeRepository.save(user);
        res.send(results);

});

router.put('/resetpassword/:id', authentificationToken, async function (req, res) {
        const UserTypeRepository = AppDataSource.getRepository(User);
        const user = await UserTypeRepository.findOneBy({ id: parseInt(req.params.id) });
        user.password = req.body;
        const results = await UserTypeRepository.save(user);
        res.send(results);
});

export default router;
