<commit_before>import {NextFunction, Request, Response, Router} from 'express'
import DAO from '../../../utils/DAO'
import * as _ from 'lodash'

class AdminCouncilRouter {
    public router: Router;

    constructor() {
        this.router = Router();
        this.init()
    }

    public getCouncils(req: Request, res: Response, next: NextFunction) {
        DAO.getAllFaculties()
            .then((result) => {
                res
                    .status(200)
                    .render('./admin/councils', {faculties: result.payload, layout: 'admin'});
            });
    }

    public getCouncil(req: Request, res: Response, next: NextFunction) {
        DAO.getOneCouncil(req.params.id)
            .then((result) => {
                // Calculate free positions
                let i
                const free = {phd: result.payload.phdPositions, stud: result.payload.studentPositions};
                for (i = 0; i < result.payload.CouncilInstances[0].Users.length; i++) {
                    if (result.payload.CouncilInstances[0].Users[i].phd === true) {
                        free.phd--
                    } else {
                        free.stud--
                    }
                }
                console.log(JSON.stringify(result.payload.CouncilInstances[0].Users, null, 2));
                res
                    .status(200)
                    .render('./admin/council', {council: result.payload, free, layout: 'admin'})
            });
    }

    public createCouncil(req: Request, res: Response, next: NextFunction) {
        console.log(req.query.faculty)
        DAO.getAllFaculties()
        .then((result) => {
            res
                .status(200)
                .render('admin/council/create', {faculties: result.payload, selected: req.query.faculty, layout: 'admin'});
        });
    };

    public postCreateCouncil(req: Request, res: Response, next: NextFunction) {
        if (req.body.councilName) {
            // Create new council AND then new instance of that council.
            const council = {
                name: req.body.councilName,
                description: req.body.comments,
                studentPositions: req.body.students,
                phdPositions: req.body.phd,
                facultyId: req.body.faculty
            }
            const instance = {
                councilId: -1,
                from: req.body.dateFrom,
                till: req.body.dateTill
            }

            console.log(council)

            DAO.createCouncil(council)
            .then((result) => {
                console.log(result)
                const created = JSON.parse(result).payload
                instance.councilId = created.id
                console.log(instance)

                // TODO:
                DAO.createCouncilInstance(instance)
                .then(() => {
                    res
                    .status(200)
                    .redirect('/admin/councils')
                })
            })


        } else {
            // Create new council instance.

            res
            .status(200)
            .redirect('/admin/councils')
        }

    }
<commit_msg>Implementing creating new council instance.<commit_after>import {NextFunction, Request, Response, Router} from 'express'
import DAO from '../../../utils/DAO'
import * as _ from 'lodash'

class AdminCouncilRouter {
    public router: Router;

    constructor() {
        this.router = Router();
        this.init()
    }

    public getCouncils(req: Request, res: Response, next: NextFunction) {
        DAO.getAllFaculties()
            .then((result) => {
                res
                    .status(200)
                    .render('./admin/councils', {faculties: result.payload, layout: 'admin'});
            });
    }

    public getCouncil(req: Request, res: Response, next: NextFunction) {
        DAO.getOneCouncil(req.params.id)
            .then((result) => {
                // Calculate free positions
                let i
                const free = {phd: result.payload.phdPositions, stud: result.payload.studentPositions};
                for (i = 0; i < result.payload.CouncilInstances[0].Users.length; i++) {
                    if (result.payload.CouncilInstances[0].Users[i].phd === true) {
                        free.phd--
                    } else {
                        free.stud--
                    }
                }
                console.log(JSON.stringify(result.payload.CouncilInstances[0].Users, null, 2));
                res
                    .status(200)
                    .render('./admin/council', {council: result.payload, free, layout: 'admin'})
            });
    }

    public createCouncil(req: Request, res: Response, next: NextFunction) {
        console.log(req.query.faculty)
        DAO.getAllFaculties()
        .then((result) => {
            res
                .status(200)
                .render('admin/council/create', {faculties: result.payload, selected: req.query.faculty, layout: 'admin'});
        });
    };

    public postCreateCouncil(req: Request, res: Response, next: NextFunction) {
        if (req.body.councilName) {
            // Create new council AND then new instance of that council.
            const council = {
                name: req.body.councilName,
                description: req.body.comments,
                studentPositions: req.body.students,
                phdPositions: req.body.phd,
                facultyId: req.body.faculty
            }
            const instance = {
                councilId: -1,
                from: req.body.dateFrom,
                till: req.body.dateTill
            }

            console.log(council)

            DAO.createCouncil(council)
            .then((result) => {
                console.log(result)
                const created = JSON.parse(result).payload
                instance.councilId = created.id
                console.log(instance)

                // TODO:
                DAO.createCouncilInstance(instance)
                .then((result) => {
                    console.log('<NAME>')
                    console.log(result)
                    res
                    .status(200)
                    .redirect('/admin/councils')
                })
            })


        } else {
            // Create new council instance.
            console.log(req.body)
            const instance = {
                councilId: req.body.council,
                from: req.body.dateFrom,
                till: req.body.dateTill
            }

            DAO.createCouncilInstance(instance)
                .then((result) => {
                    console.log('<NAME>')
                    console.log(result)
                    res
                    .status(200)
                    .redirect('/admin/councils')
                })

            res
            .status(200)
            .redirect('/admin/councils')
        }

    }

{'fraction_non_alphanumeric': 0.1143307990245302, 'fraction_numerical': 0.006455314875914503, 'mean_word_length': 1.3017497523935293, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5317022', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1796, 'n_words': 352}