Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
I have these controllers that deletes a user, i want to implement DRY(Don't repeat yourself) principle and make the res json message dynamic

I want to implement a DRY principle for my code below which deletes two different user and also i want to implement a dynamic response based on the details of the model provided to the find method

//This code here deletes the student
exports.deleteStudent = async (req, res) => {
    try {
        let ID = req.query.id
        let exactStudent = await Student.findById(ID)
        if (!exactStudent) {
            return res.status(400).json({ error: "Student with the specified ID not present" })
        }
        await Student.findByIdAndDelete(ID);
        res.status(200).json({ success: 'Student Deleted successfully ' })
    } catch (error) {
        throw error
    }
}

//the code here deletes the teachers

exports.deleteTeacher = async (req, res) => {
    try {
        let ID = req.query.id
        let exactTeacher = await Teacher.findById(ID)
        if (!exactTeacher) {
            return res.status(400).json({ error: "Teacher with the specified ID not present" })
        }
        await Teacher.findByIdAndDelete(ID);
        res.status(200).json({ error: 'Teacher Deleted successfully ' })
    } catch (error) {
        throw error
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As I understood your question, you need something like this:

async function deletePerson(Model, id) => {
    try {
        return await Model.findByIdAndDelete(id);
    } catch (error) {
        throw error;
    }
}

// Inside the controllers

exports.deleteStudent = async (req, res) => {
    try {
        const deletedEntity = await deletePerson(Student, req.query.id);
        if (!deletedEntity) {
            return res.status(400).json({ error: "Student with the specified ID not present" })
        }
        res.status(200).json({ error: 'Student Deleted successfully ' })
    } catch (error) {
        throw error
    }
}

exports.deleteTeacher = async (req, res) => {
    try {
        const deletedEntity = await deletePerson(Teacher, req.query.id);
        if (!deletedEntity) {
            return res.status(400).json({ error: "Teacher with the specified ID not present" })
        }
        res.status(200).json({ error: 'Teacher Deleted successfully ' })
    } catch (error) {
        throw error
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda