Skip to content

Commit

Permalink
Renomeação de arquivos
Browse files Browse the repository at this point in the history
  • Loading branch information
NaluFigueira committed Oct 28, 2019
1 parent ac0baa6 commit 9c9a5e1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import * as Yup from 'yup';
import Enrollment from '../models/Enrollment';
import Plan from '../models/Plan';

class EnrollmentController {
class PlanController {
async index(req, res) {
const enrollments = await Enrollment.findAll({
const plans = await Plan.findAll({
order: [['title', 'DESC']],
});

return res.json(enrollments);
return res.json(plans);
}

async show(req, res) {
const enrollment = await Enrollment.findOne({
const plan = await Plan.findOne({
where: { id: req.params.id },
});

if (!enrollment) return res.status(400).json({ error: 'Invalid id!' });
if (!Plan) return res.status(400).json({ error: 'Invalid id!' });

return res.json(enrollment);
return res.json(plan);
}

async store(req, res) {
Expand All @@ -32,14 +32,14 @@ class EnrollmentController {

const { title } = req.body;

const invalidTitle = await Enrollment.findOne({ where: { title } });
const invalidTitle = await Plan.findOne({ where: { title } });

if (invalidTitle)
return res.status(400).json({ error: 'This plan already exists!' });

const enrollment = await Enrollment.create(req.body);
const plan = await Plan.create(req.body);

return res.json(enrollment);
return res.json(plan);
}

async update(req, res) {
Expand All @@ -53,25 +53,24 @@ class EnrollmentController {
return res.status(400).json({ error: 'Invalid data!' });

const { title } = req.body;
const validEnrollmentPlan = await Enrollment.findOne({ where: { title } });
const validPlan = await Plan.findOne({ where: { title } });

if (!validEnrollmentPlan)
res.status(400).json({ error: "This type of enrollment doesn't exist!" });
if (!validPlan) res.status(400).json({ error: "This plan doesn't exist!" });

const updatedPlan = await validEnrollmentPlan.update(req.body);
const updatedPlan = await validPlan.update(req.body);

return res.json(updatedPlan);
}

async delete(req, res) {
const deleted = await Enrollment.destroy({
const deleted = await Plan.destroy({
where: { id: req.params.id },
});

if (!deleted) return res.status(400).json({ error: 'Invalid id!' });

return res.json({ message: 'Enrollment type deleted!' });
return res.json({ message: 'Plan deleted!' });
}
}

export default new EnrollmentController();
export default new PlanController();
4 changes: 2 additions & 2 deletions src/app/models/Enrollment.js → src/app/models/Plan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Sequelize, { Model } from 'sequelize';

class Enrollment extends Model {
class Plan extends Model {
static init(sequelize) {
super.init(
{
Expand All @@ -15,4 +15,4 @@ class Enrollment extends Model {
}
}

export default Enrollment;
export default Plan;
4 changes: 2 additions & 2 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Sequelize from 'sequelize';

import User from '../app/models/User';
import Student from '../app/models/Student';
import Enrollment from '../app/models/Enrollment';
import Plan from '../app/models/Plan';

import DataBaseConfig from '../config/database';

const models = [User, Student, Enrollment];
const models = [User, Student, Plan];

class Database {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('enrollments', {
return queryInterface.createTable('plans', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
Expand Down Expand Up @@ -32,6 +32,6 @@ module.exports = {
},

down: queryInterface => {
return queryInterface.dropTable('enrollments');
return queryInterface.dropTable('plans');
},
};
12 changes: 6 additions & 6 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from 'express';

import StudentController from './app/controllers/StudentController';
import SessionController from './app/controllers/SessionController';
import EnrollmentController from './app/controllers/EnrollmentController';
import PlanController from './app/controllers/PlanController';
import authMiddleware from './app/middlewares/auth';

const routes = new Router();
Expand All @@ -13,9 +13,9 @@ routes.use(authMiddleware);
routes.post('/students', StudentController.store);
routes.put('/students', StudentController.update);

routes.get('/enrollments', EnrollmentController.index);
routes.get('/enrollments/:id', EnrollmentController.show);
routes.post('/enrollments', EnrollmentController.store);
routes.put('/enrollments', EnrollmentController.update);
routes.delete('/enrollments/:id', EnrollmentController.delete);
routes.get('/plans', PlanController.index);
routes.get('/plans/:id', PlanController.show);
routes.post('/plans', PlanController.store);
routes.put('/plans', PlanController.update);
routes.delete('/plans/:id', PlanController.delete);
export default routes;

0 comments on commit 9c9a5e1

Please sign in to comment.