-
-
Save fredarend/a6d24c7945094555ef2af44e96a64dd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Yup from 'yup'; | |
// import { Op } from 'sequelize'; | |
import Course from '../models/Course'; | |
import Organization from '../models/Organization'; | |
import Company from '../models/Company'; | |
import Teacher from '../models/Teacher'; | |
import CoursesCompanies from '../models/CoursesCompanies'; | |
import CoursesOrganizations from '../models/CoursesOrganizations'; | |
import CoursesTeachers from '../models/CoursesTeachers'; | |
class CourseController { | |
async store(req, res) { | |
const schema = Yup.object().shape({ | |
name: Yup.string().required(), | |
description: Yup.string().required(), | |
objective: Yup.string().required(), | |
target_audience: Yup.string().required(), | |
workload: Yup.string().required(), | |
organizations: Yup.array(), | |
companies: Yup.array(), | |
teachers: Yup.array(), | |
tutor_user_id: Yup.number(), | |
}); | |
if (!(await schema.isValid(req.body))) { | |
return res.status(400).json({ error: 'Validation fails!' }); | |
} | |
const { | |
id, | |
name, | |
description, | |
objective, | |
target_audience, | |
tutor_user_id, | |
} = await Course.create(req.body); | |
const { organizations } = req.body; | |
const organizationsArray = organizations.map(async (organization) => { | |
const organizationExists = await Organization.findByPk(organization); | |
if (!organizationExists) { | |
return res | |
.status(400) | |
.json({ error: `Organization ${organization} does not exists!` }); | |
} | |
return { | |
course_id: id, | |
organization_id: organization, | |
}; | |
}); | |
await CoursesOrganizations.bulkCreate(organizationsArray); | |
const { companies } = req.body; | |
const companiesArray = companies.map(async (company) => { | |
const companyExists = await Company.findByPk(company); | |
if (!companyExists) { | |
return res | |
.status(400) | |
.json({ error: `Company ${company} does not exists!` }); | |
} | |
return { | |
course_id: id, | |
company_id: company, | |
}; | |
}); | |
await CoursesCompanies.bulkCreate(companiesArray); | |
const { teachers } = req.body; | |
const teachersArray = teachers.map(async (teacher) => { | |
const teacherExists = await Teacher.findByPk(teacher); | |
if (!teacherExists) { | |
return res | |
.status(400) | |
.json({ error: `Teacher ${teacher} does not exists!` }); | |
} | |
return { | |
course_id: id, | |
teacher_id: teacher, | |
}; | |
}); | |
await CoursesTeachers.bulkCreate(teachersArray); | |
return res.json({ | |
id, | |
name, | |
description, | |
objective, | |
target_audience, | |
organizationsArray, | |
companiesArray, | |
teachersArray, | |
tutor_user_id, | |
}); | |
} | |
} | |
export default new CourseController(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment