Skip to content

Commit

Permalink
Merge pull request #37 from Chia-Network/dev/michael.taylor/seed-db
Browse files Browse the repository at this point in the history
feat: set up cors, set up db seed
  • Loading branch information
MichaelTaylor3D authored Dec 9, 2021
2 parents 7a17ca1 + 7e0766c commit 20e4154
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 735 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ If you want to alter, drop or add a column or add a foriegn key or anything with

npx sequelize-cli migration:generate --name <enter-type-of-change-here>

#### Seed database

npx sequelize-cli db:seed:all

##### Recommendations

Models are not currently being added to our folder structure because sequelize can handle it. So for now. simply copy and paste the model into the appropriate folder
3 changes: 3 additions & 0 deletions migrations/20211201194652-create-unit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
uuid: {
type: Sequelize.STRING,
},
ProjectId: {
type: Sequelize.STRING,
},
owner: {
type: Sequelize.STRING,
},
Expand Down
16 changes: 8 additions & 8 deletions migrations/20211201195550-create-project.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module.exports = {
primaryKey: true,
type: Sequelize.INTEGER,
},
uuid: {
warehouseProjectId: {
type: Sequelize.STRING,
},
projectID: {
type: Sequelize.STRING,
},
currentRegistry: {
Expand All @@ -18,14 +21,11 @@ module.exports = {
type: Sequelize.STRING,
},
originProjectId: {
type: Sequelize.NUMBER,
type: Sequelize.STRING,
},
program: {
type: Sequelize.STRING,
},
warehouseProjectId: {
type: Sequelize.NUMBER,
},
projectName: {
type: Sequelize.STRING,
},
Expand All @@ -42,7 +42,7 @@ module.exports = {
type: Sequelize.STRING,
},
coveredByNDC: {
type: Sequelize.STRING,
type: Sequelize.INTEGER,
},
NDCLinkage: {
type: Sequelize.STRING,
Expand All @@ -60,7 +60,7 @@ module.exports = {
type: Sequelize.STRING,
},
methodologyVersion: {
type: Sequelize.STRING,
type: Sequelize.NUMBER,
},
validationApproach: {
type: Sequelize.STRING,
Expand All @@ -71,7 +71,7 @@ module.exports = {
projectTag: {
type: Sequelize.STRING,
},
estimatedAnnualAverageEmmisionReduction: {
estimatedAnnualAverageEmissionReduction: {
type: Sequelize.STRING,
},
owner: {
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"debug": "~2.6.9",
"express": "~4.16.1",
"express-joi-validation": "^5.0.0",
Expand Down
10 changes: 10 additions & 0 deletions seeders/20211209204301-add-projects.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const ProjectStub = require('../src/models/projects/projects.stub.json');

module.exports = {
up: async (queryInterface) =>
queryInterface.bulkInsert('Projects', ProjectStub, {}),
down: async (queryInterface) => {
await queryInterface.bulkDelete('Projects');
},
};
10 changes: 10 additions & 0 deletions seeders/20211209205139-add-units.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const UnitStub = require('../src/models/units/units.stub.json');

module.exports = {
up: async (queryInterface) =>
queryInterface.bulkInsert('Units', UnitStub, {}),
down: async (queryInterface) => {
await queryInterface.bulkDelete('Units');
},
};
25 changes: 13 additions & 12 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, ProjectMock } from '../models';
import { Staging, ProjectMock, Project } from '../models';

export const create = async (req, res) => {
// When creating new projects assign a uuid to is so
Expand All @@ -13,7 +13,7 @@ export const create = async (req, res) => {
table: 'Projects',
data: JSON.stringify(req.body),
});
res.json('Success');
res.json('Added project to stage');
} catch (err) {
res.json(err);
}
Expand All @@ -25,9 +25,7 @@ export const findAll = async (req, res) => {
return;
}

res.json({
message: 'Not Yet Implemented',
});
res.json(await Project.findAll());
};

export const findOne = (req, res) => {
Expand All @@ -49,41 +47,44 @@ export const findOne = (req, res) => {

export const update = (req, res) => {
const stagedData = {
uuid: req.body.uuid,
uuid: req.body.warehouseProjectId,
action: 'UPDATE',
table: 'Projects',
data: JSON.stringify(req.body),
};

console.log(stagedData);

Staging.create(stagedData)
.then(() =>
res.json({
message: 'Project created successfully',
message: 'Project update added to staging',
}),
)
.catch(() =>
.catch((err) =>
res.json({
message: 'Error creating new project',
message: 'Error adding update to stage',
error: err,
}),
);
};

export const destroy = (req, res) => {
const stagedData = {
uuid: req.body.uuid,
uuid: req.body.warehouseProjectId,
action: 'DELETE',
table: 'Projects',
};

Staging.create(stagedData)
.then(() =>
res.json({
message: 'Project created successfully',
message: 'Project removal added to stage',
}),
)
.catch(() =>
res.json({
message: 'Error creating new project',
message: 'Error adding project removal to stage',
}),
);
};
20 changes: 14 additions & 6 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Staging, StagingMock } from '../models';
import _ from 'lodash';
import { Staging, StagingMock, Project, Unit } from '../models';

Expand All @@ -20,10 +19,19 @@ export const findAll = async (req, res) => {
}

if (workingData.action === 'UPDATE') {
const Model = workingData.table === 'Projects' ? Project : Unit;
const original = await Model.findOne({
where: { uuid: workingData.uuid },
});
let original;
if (workingData.table === 'Projects') {
original = await Project.findOne({
where: { warehouseProjectId: workingData.uuid },
});
}

if (workingData.table === 'Units') {
original = await Unit.findOne({
where: { uuid: workingData.uuid },
});
}

workingData.diff.original = original;
workingData.diff.change = JSON.parse(workingData.data);
}
Expand All @@ -50,7 +58,7 @@ export const destroy = (req, res) => {
})
.then(() => {
res.json({
message: 'Deleted',
message: 'Deleted from stage',
});
})
.catch((err) => {
Expand Down
6 changes: 2 additions & 4 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 } from '../models';
import { Staging, UnitMock, Unit } from '../models';

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

res.json({
message: 'Not Yet Implemented',
});
res.json(await Unit.findAll());
};

export const findOne = (req, res) => {
Expand Down
12 changes: 6 additions & 6 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ Project.init(
type: Sequelize.NUMBER,
primaryKey: true,
},
uuid: Sequelize.STRING,
warehouseProjectId: Sequelize.STRING,
projectID: Sequelize.STRING,
currentRegistry: Sequelize.STRING,
registryOfOrigin: Sequelize.STRING,
originProjectId: Sequelize.NUMBER,
originProjectId: Sequelize.STRING,
program: Sequelize.STRING,
warehouseProjectId: Sequelize.NUMBER,
projectName: Sequelize.STRING,
projectLink: Sequelize.STRING,
projectDeveloper: Sequelize.STRING,
sector: Sequelize.STRING,
projectType: Sequelize.STRING,
coveredByNDC: Sequelize.STRING,
coveredByNDC: Sequelize.INTEGER,
NDCLinkage: Sequelize.STRING,
projectStatus: Sequelize.STRING,
projectStatusDate: Sequelize.DATE,
unitMetric: Sequelize.STRING,
methodology: Sequelize.STRING,
methodologyVersion: Sequelize.STRING,
methodologyVersion: Sequelize.NUMBER,
validationApproach: Sequelize.STRING,
validationDate: Sequelize.DATE,
projectTag: Sequelize.STRING,
estimatedAnnualAverageEmmisionReduction: Sequelize.STRING,
estimatedAnnualAverageEmissionReduction: Sequelize.STRING,
owner: Sequelize.STRING,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
Expand Down
Loading

0 comments on commit 20e4154

Please sign in to comment.