{"size": 2262, "ext": "js", "max_stars_repo_path": "NodeJS_Studies/ORM_Sequelize/api/controllers/LevelsController.js", "max_stars_repo_name": "wiltonjr4/JavaScript", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "NodeJS_Studies/ORM_Sequelize/api/controllers/LevelsController.js", "max_issues_repo_name": "wiltonjr4/JavaScript", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "NodeJS_Studies/ORM_Sequelize/api/controllers/LevelsController.js", "max_forks_repo_name": "wiltonjr4/JavaScript", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.0638297872, "max_line_length": 106, "alphanum_fraction": 0.507515473}
const database = require('../models')

class LevelsController
{
    static async getAllLevels(requisition, answer)
    {
        try
        {
            const allLevels = await database.Levels.findAll()
            return answer.status(200).json(allLevels)
        }
        catch (error)
        {
            return answer.status(500).json(error.message)
        }
    }

    static async getOneLevel(requisition, answer)
    {
        const { id } = requisition.params
        
        try
        {
            const oneLevel = await database.Levels.findOne({
                where:
                {
                    id: Number(id)
                }
            })

            return answer.status(200).json(oneLevel)
        }
        catch(error)
        {
            return answer.status(500).json(error.message)
        }
    }

    static async createLevel(requisition, answer)
    {
        const newLevel = requisition.body

        try
        {
            const newLevelCreated = await database.Levels.create(newLevel)
            return answer.status(201).json(newLevelCreated)
        }
        catch(error)
        {
            return answer.status(500).json(error.message)
        }
    }

    static async updateLevel(requisition, answer)
    {
        const newInfo = requisition.body
        const { id } = requisition.params

        try
        {
            await database.Levels.update(newInfo, { where: { id: Number(id) }})
            
            const oneLevel = await database.Levels.findOne({
                where:
                {
                    id: Number(id)
                }
            })

            return answer.status(200).json(oneLevel)
        }
        catch(error)
        {
            return answer.status(500).json(error.message)
        }
    }

    static async deleteLevel(requisition, answer)
    {
        const { id } = requisition.params

        try
        {
            await database.Levels.destroy({where: { id: Number(id) }})
            return answer.status(200).json({message: `The Level with ID: ${id} was successfully deleted`})
        }
        catch(error)
        {
            return answer.status(500).json(error.message)
        }
    }
}

module.exports = LevelsController