"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const data_source_1 = require("../data-source");
const Student_1 = require("../entity/Student");
const auth_1 = require("../middleware/auth");
const TypeStudent_1 = require("../entity/TypeStudent");
const TitleStudent_1 = require("../entity/TitleStudent");
const router = (0, express_1.Router)();
router.post('/addStudent', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Student_1.Student);
        const student = repo.create(req.body);
        const results = await repo.save(student);
        res.send(results);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.put('/updateStudent/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Student_1.Student);
        const student = await repo.findOne({
            where: { id: parseInt(req.params.id) },
            relations: ['TypeStudent', 'TitleStudent']
        });
        repo.merge(student, req.body);
        const results = await repo.save(student);
        res.send(results);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.delete('/deletestudent/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Student_1.Student);
        const s = await repo.findOne({ where: { id: parseInt(req.params.id) } });
        s.enable = !s.enable;
        await repo.save(s);
        res.send(true);
    }
    catch (error) {
        console.error(error);
        res.send(error);
    }
});
router.get('/studenttypes', auth_1.authentificationToken, async function (req, res) {
    let TypeStudentTypeRepository = data_source_1.AppDataSource.getRepository(TypeStudent_1.TypeStudent);
    let TypeStudents = await TypeStudentTypeRepository.find();
    res.send(TypeStudents);
});
router.put('/updatestudenttype/:id', auth_1.authentificationToken, async function (req, res) {
    const TypeStudentTypeRepository = data_source_1.AppDataSource.getRepository(TypeStudent_1.TypeStudent);
    const typeStudent = await TypeStudentTypeRepository.findOneBy({ id: parseInt(req.params.id) });
    TypeStudentTypeRepository.merge(typeStudent, req.body);
    const results = await TypeStudentTypeRepository.save(typeStudent);
    res.send(results);
});
router.post('/addstudenttype', auth_1.authentificationToken, async function (req, res) {
    const TypeStudentTypeRepository = data_source_1.AppDataSource.getRepository(TypeStudent_1.TypeStudent);
    const typeStudent = await TypeStudentTypeRepository.create(req.body);
    const results = await TypeStudentTypeRepository.save(typeStudent);
    res.send(results);
});
router.get('/studenttitles', auth_1.authentificationToken, async function (req, res) {
    let TitleStudentRepository = data_source_1.AppDataSource.getRepository(TitleStudent_1.TitleStudent);
    let TitleStudents = await TitleStudentRepository.find();
    res.send(TitleStudents);
});
router.put('/updatestudenttitle/:id', auth_1.authentificationToken, async function (req, res) {
    const TitleStudentRepository = data_source_1.AppDataSource.getRepository(TitleStudent_1.TitleStudent);
    const titleStudent = await TitleStudentRepository.findOneBy({ id: parseInt(req.params.id) });
    TitleStudentRepository.merge(titleStudent, req.body);
    const results = await TitleStudentRepository.save(titleStudent);
    res.send(results);
});
router.post('/addstudenttitle', auth_1.authentificationToken, async function (req, res) {
    const TitleStudentRepository = data_source_1.AppDataSource.getRepository(TitleStudent_1.TitleStudent);
    const titleStudent = await TitleStudentRepository.create(req.body);
    const results = await TitleStudentRepository.save(titleStudent);
    res.send(results);
});
exports.default = router;
