Skip to content

Commit

Permalink
feat: controller resolves all relationships in response
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Dec 14, 2021
1 parent d41dbb9 commit f0b819c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 34 deletions.
38 changes: 22 additions & 16 deletions migrations/20211201194416-create-qualification.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,55 @@ module.exports = {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
},
projectId: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
qualificationId: {
type: Sequelize.NUMBER,
},
qualificationLink: {
type: Sequelize.STRING,
},
type: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
label: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
creditingPeriodStartDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
creditingPeriodEndDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
validityStartDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
validityEndDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
unitQuantity: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
owner: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
unitId: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
type: Sequelize.DATE,
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Qualifications');
}
};
},
};
29 changes: 16 additions & 13 deletions migrations/20211201194541-create-vintage.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,43 @@ module.exports = {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
},
startDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
endDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
verificationApproach: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
verificationDate: {
type: Sequelize.DATE
type: Sequelize.DATE,
},
verificationBody: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
owner: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
projectId: {
type: Sequelize.NUMBER
type: Sequelize.NUMBER,
},
UnitId: {
type: Sequelize.NUMBER,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
type: Sequelize.DATE,
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Vintages');
}
};
},
};
23 changes: 21 additions & 2 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, ProjectMock, Project } from '../models';
import {
Staging,
ProjectMock,
Project,
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
} from '../models';

export const create = async (req, res) => {
// When creating new projects assign a uuid to is so
Expand All @@ -25,7 +34,17 @@ export const findAll = async (req, res) => {
return;
}

res.json(await Project.findAll());
res.json(
await Project.findAll({
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
}),
);
};

export const findOne = (req, res) => {
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, UnitMock, Unit } from '../models';
import { Staging, UnitMock, Unit, Qualification, Vintage } from '../models';

export const create = (req, res) => {
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -31,7 +31,11 @@ export const findAll = async (req, res) => {
return;
}

res.json(await Unit.findAll());
res.json(
await Unit.findAll({
include: [Qualification, Vintage],
}),
);
};

export const findOne = (req, res) => {
Expand Down
3 changes: 3 additions & 0 deletions src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Qualification.init(
primaryKey: true,
type: Sequelize.INTEGER,
},
qualificationId: {
type: Sequelize.NUMBER,
},
qualificationLink: {
type: Sequelize.STRING,
},
Expand Down
4 changes: 3 additions & 1 deletion src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects/index';
import { Project, Qualification, Vintage } from '../../models';

class Unit extends Model {
static associate() {
Unit.belongsTo(Project);
Unit.hasMany(Qualification);
Unit.hasMany(Vintage);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/models/vintages/vintages.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Vintage.init(
projectId: Sequelize.NUMBER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
UnitId: Sequelize.NUMBER,
},
{
sequelize,
Expand Down

0 comments on commit f0b819c

Please sign in to comment.