added a migration for File table

This commit is contained in:
bill bittner 2018-07-25 17:21:02 -07:00
parent 0f6193b450
commit 2d7dbdc8a0
5 changed files with 116 additions and 63 deletions

View file

@ -1,5 +1,7 @@
module.exports = (fileInfo, getResult) => {
const addGetResultsToFileData = (fileInfo, getResult) => {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
};
module.exports = addGetResultsToFileData;

View file

@ -1,4 +1,4 @@
module.exports = ({ name, claimId, outpoint, height, address, nsfw, contentType }) => {
const createFileData = ({ name, claimId, outpoint, height, address, nsfw, contentType }) => {
return {
name,
claimId,
@ -11,3 +11,5 @@ module.exports = ({ name, claimId, outpoint, height, address, nsfw, contentType
nsfw,
};
};
module.exports = createFileData;

View file

@ -13,23 +13,35 @@ const db = require('../../../../models');
const claimGet = ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
let fileData;
let resolveResult;
let getResult;
let message;
let completed;
// resolve the claim
db.Claim.resolveClaim(name, claimId)
.then(resolveResult => {
.then(result => {
// make sure a claim actually exists at that uri
if (!resolveResult) {
if (!result) {
throw new Error('No matching uri found in Claim table');
}
let fileData = createFileData(resolveResult);
// get the claim
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
resolveResult = result;
return getClaim(`${name}#${claimId}`);
})
.then(([ fileData, getResult ]) => {
.then(result => {
getResult = result;
fileData = createFileData(resolveResult);
fileData = addGetResultsToFileData(fileData, getResult);
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]);
const upsertCriteria = { name, claimId};
return db.upsert(db.File, fileData, upsertCriteria, 'File');
})
.then(([ fileRecord, {message, completed} ]) => {
res.status(200).json({ success: true, message, completed });
.then(() => {
({ message, completed } = getResult);
res.status(200).json({
success: true,
message,
completed,
});
})
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);

View file

@ -1,34 +1,88 @@
module.exports = {
up: (queryInterface, Sequelize) => {
up: (queryInterface, { INTEGER }) => {
// logic for transforming into the new state
const changeOne = queryInterface.addColumn(
'File',
'height',
{
type : Sequelize.INTEGER,
allowNull: false,
}
);
const changeTwo = queryInterface.addColumn(
'File',
'width',
{
type : Sequelize.INTEGER,
allowNull: false,
}
);
return Promise.all([changeOne, changeTwo]);
return Promise.all([
queryInterface.addColumn(
'File',
'fileHeight',
{
type : INTEGER,
allowNull: false,
default : 0,
}
),
queryInterface.addColumn(
'File',
'fileWidth',
{
type : INTEGER,
allowNull: false,
default : 0,
}
),
queryInterface.removeColumn(
'File',
'address',
),
queryInterface.removeColumn(
'File',
'height',
),
queryInterface.removeColumn(
'File',
'nsfw',
),
queryInterface.removeColumn(
'File',
'trendingEligible',
),
]);
},
down: (queryInterface, Sequelize) => {
// logic for reverting the changes
const reversionOne = queryInterface.removeColumn(
'File',
'height'
);
const reversionTwo = queryInterface.removeColumn(
'File',
'width',
);
return Promise.all([reversionOne, reversionTwo]);
down: (queryInterface, { BOOLEAN, INTEGER, STRING }) => {
return Promise.all([
queryInterface.removeColumn(
'File',
'fileHeight',
),
queryInterface.removeColumn(
'File',
'fileWidth',
),
queryInterface.addColumn(
'File',
'address',
{
type : STRING,
allowNull: false,
}
),
queryInterface.addColumn(
'File',
'height',
{
type : INTEGER,
allowNull: false,
default : 0,
}
),
queryInterface.addColumn(
'File',
'nsfw',
{
type : BOOLEAN,
allowNull : false,
defaultValue: false,
}
),
queryInterface.addColumn(
'File',
'trendingEligible',
{
type : BOOLEAN,
allowNull : false,
defaultValue: true,
}
),
]);
},
};

View file

@ -10,15 +10,16 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
type : STRING,
allowNull: false,
},
address: {
type : STRING,
allowNull: false,
},
outpoint: {
type : STRING,
allowNull: false,
},
height: {
fileHeight: {
type : INTEGER,
allowNull: false,
default : 0,
},
fileWidth: {
type : INTEGER,
allowNull: false,
default : 0,
@ -34,16 +35,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
fileType: {
type: STRING,
},
nsfw: {
type : BOOLEAN,
allowNull : false,
defaultValue: false,
},
trendingEligible: {
type : BOOLEAN,
allowNull : false,
defaultValue: true,
},
},
{
freezeTableName: true,
@ -55,13 +46,5 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
File.hasOne(db.Claim);
};
File.getRecentClaims = function () {
return this.findAll({
where: { nsfw: false, trendingEligible: true },
order: [['createdAt', 'DESC']],
limit: 25,
});
};
return File;
};