spee.ch/server/controllers/serveController.js

118 lines
4 KiB
JavaScript
Raw Normal View History

2018-03-28 01:08:01 +02:00
const { db } = require('mysqlConfig');
2017-06-20 01:15:28 +02:00
const logger = require('winston');
const { returnPaginatedChannelClaims } = require('../helpers/channelPagination.js');
const NO_CHANNEL = 'NO_CHANNEL';
2017-12-06 02:08:34 +01:00
const NO_CLAIM = 'NO_CLAIM';
const NO_FILE = 'NO_FILE';
module.exports = {
2017-12-05 19:18:49 +01:00
getClaimId (channelName, channelClaimId, name, claimId) {
2017-11-21 21:53:43 +01:00
if (channelName) {
2017-12-05 19:18:49 +01:00
return module.exports.getClaimIdByChannel(channelName, channelClaimId, name);
2017-11-21 21:53:43 +01:00
} else {
return module.exports.getClaimIdByClaim(name, claimId);
}
},
getClaimIdByClaim (claimName, claimId) {
logger.debug(`getClaimIdByClaim(${claimName}, ${claimId})`);
return new Promise((resolve, reject) => {
2017-12-06 02:08:34 +01:00
db.Claim.getLongClaimId(claimName, claimId)
.then(longClaimId => {
if (!longClaimId) {
resolve(NO_CLAIM);
}
resolve(longClaimId);
})
.catch(error => {
reject(error);
});
});
},
2017-12-05 19:18:49 +01:00
getClaimIdByChannel (channelName, channelClaimId, claimName) {
logger.debug(`getClaimIdByChannel(${channelName}, ${channelClaimId}, ${claimName})`);
return new Promise((resolve, reject) => {
2017-12-05 19:18:49 +01:00
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel id
2017-12-06 02:08:34 +01:00
.then(longChannelId => {
if (!longChannelId) {
return [null, null];
}
2017-12-06 02:08:34 +01:00
return Promise.all([longChannelId, db.Claim.getClaimIdByLongChannelId(longChannelId, claimName)]); // 2. get the long claim id
})
2017-12-06 02:08:34 +01:00
.then(([longChannelId, longClaimId]) => {
if (!longChannelId) {
return resolve(NO_CHANNEL);
}
if (!longClaimId) {
return resolve(NO_CLAIM);
}
resolve(longClaimId);
})
.catch(error => {
reject(error);
});
});
},
getChannelData (channelName, channelClaimId, page) {
return new Promise((resolve, reject) => {
// 1. get the long channel Id (make sure channel exists)
db.Certificate.getLongChannelId(channelName, channelClaimId)
.then(longChannelClaimId => {
2017-12-06 02:08:34 +01:00
if (!longChannelClaimId) {
return [null, null, null];
}
// 2. get the short ID and all claims for that channel
return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName)]);
})
.then(([longChannelClaimId, shortChannelClaimId]) => {
if (!longChannelClaimId) {
return resolve(NO_CHANNEL);
}
// 3. return all the channel information
resolve({
channelName,
longChannelClaimId,
shortChannelClaimId,
});
})
.catch(error => {
reject(error);
});
});
},
getChannelClaims (channelName, channelClaimId, page) {
return new Promise((resolve, reject) => {
// 1. get the long channel Id (make sure channel exists)
db.Certificate.getLongChannelId(channelName, channelClaimId)
.then(longChannelClaimId => {
if (!longChannelClaimId) {
return [null, null, null];
}
// 2. get the short ID and all claims for that channel
return Promise.all([longChannelClaimId, db.Claim.getAllChannelClaims(longChannelClaimId)]);
})
.then(([longChannelClaimId, channelClaimsArray]) => {
2017-12-06 02:08:34 +01:00
if (!longChannelClaimId) {
return resolve(NO_CHANNEL);
}
// 3. format the data for the view, including pagination
let paginatedChannelViewData = returnPaginatedChannelClaims(channelName, longChannelClaimId, channelClaimsArray, page);
// 4. return all the channel information and contents
resolve(paginatedChannelViewData);
})
.catch(error => {
reject(error);
});
});
},
2017-11-21 21:53:43 +01:00
getLocalFileRecord (claimId, name) {
return db.File.findOne({where: {claimId, name}})
.then(file => {
if (!file) {
return NO_FILE;
2017-11-21 21:53:43 +01:00
}
return file.dataValues;
});
},
};