Make project table associated with the user
This commit is contained in:
@@ -17,6 +17,20 @@ module.exports = {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
isPublic: {
|
||||
type: Sequelize.BOOLEAN,
|
||||
allowNull: false,
|
||||
},
|
||||
userId: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: 'users',
|
||||
key: 'id',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
},
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
|
||||
@@ -8,6 +8,19 @@ function defineProject(sequelize, DataTypes) {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
isPublic: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
},
|
||||
userId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: 'user',
|
||||
key: 'id',
|
||||
},
|
||||
onDelete: 'CASCADE',
|
||||
},
|
||||
});
|
||||
|
||||
Project.associate = (models) => {
|
||||
|
||||
@@ -22,9 +22,9 @@ function defineUser(sequelize, DataTypes) {
|
||||
},
|
||||
});
|
||||
|
||||
// User.associate = (models) => {
|
||||
// User.hasMany(models.Project, { foreignKey: "userId" });
|
||||
// };
|
||||
User.associate = (models) => {
|
||||
User.hasMany(models.Project, { foreignKey: 'userId' });
|
||||
};
|
||||
|
||||
return User;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ module.exports = function (sequelize) {
|
||||
|
||||
router.put('/:projectId', async (req, res) => {
|
||||
const projectId = req.params.projectId;
|
||||
const { name, detail } = req.body;
|
||||
const { name, detail, isPublic } = req.body;
|
||||
try {
|
||||
const project = await Project.findByPk(projectId);
|
||||
if (!project) {
|
||||
@@ -17,6 +17,7 @@ module.exports = function (sequelize) {
|
||||
await project.update({
|
||||
name,
|
||||
detail,
|
||||
isPublic,
|
||||
});
|
||||
res.json(project);
|
||||
} catch (error) {
|
||||
|
||||
@@ -8,10 +8,12 @@ module.exports = function (sequelize) {
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, detail } = req.body;
|
||||
const { name, detail, isPublic, userId } = req.body;
|
||||
const newProject = await Project.create({
|
||||
name,
|
||||
detail,
|
||||
isPublic,
|
||||
userId,
|
||||
});
|
||||
res.json(newProject);
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,20 +1,39 @@
|
||||
'use strict';
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
const hashedPassword = await bcrypt.hash('password', 10);
|
||||
|
||||
// Add projects table records
|
||||
await queryInterface.bulkInsert('Users', [
|
||||
{
|
||||
email: 'admin@testplat.com',
|
||||
password: hashedPassword,
|
||||
username: 'Admin',
|
||||
role: 1,
|
||||
avatarPath: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
]);
|
||||
|
||||
// Add projects table records
|
||||
await queryInterface.bulkInsert('Projects', [
|
||||
{
|
||||
name: 'TestPlat Test',
|
||||
detail: "Test Plat's Manual test",
|
||||
userId: 1,
|
||||
isPublic: true,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
{
|
||||
name: 'Iron Muscle App(筋トレアプリ)',
|
||||
detail: 'リリース前総合評価',
|
||||
userId: 1,
|
||||
isPublic: true,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user