Skip to content

Commit

Permalink
Merge pull request #45 from NSWC-Crane/CHRIS_DEV
Browse files Browse the repository at this point in the history
Reference pull request for full details.
  • Loading branch information
crodriguez6497 authored Mar 13, 2024
2 parents 4684e87 + c3a8060 commit eb90aa7
Show file tree
Hide file tree
Showing 32 changed files with 635 additions and 410 deletions.
7 changes: 7 additions & 0 deletions Api/Controllers/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ module.exports.getCollection = async function getCollection(req, res, next){
res.status(201).json(getCollection)
}

module.exports.getCollectionAssetLabel = async function getCollectionAssetLabel(req, res, next) {

var getCollection = await collectionService.getCollectionAssetLabel(req, res, next)

res.status(201).json(getCollection)
}

module.exports.getCollectionPoamStatus = async function getCollectionPoamStatus(req, res, next){

var getCollection = await collectionService.getCollectionPoamStatus(req, res, next)
Expand Down
42 changes: 39 additions & 3 deletions Api/Controllers/Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ module.exports.importCollectionAndAssets = async function importCollectionAndAss
await collectionRecord.update(collectionData);
}

if (collection.labels && Array.isArray(collection.labels)) {
for (const label of collection.labels) {
const labelData = {
collectionId: collectionRecord.collectionId,
labelName: label.name,
description: label.description,
stigmanLabelId: label.labelId,
};

await db.Label.findOrCreate({
where: { stigmanLabelId: label.labelId, collectionId: collectionRecord.collectionId },
defaults: labelData
});
}
}

// Handle Assets
for (const asset of assets) {
const assetData = {
Expand All @@ -307,12 +323,32 @@ module.exports.importCollectionAndAssets = async function importCollectionAndAss
if (!assetCreated) {
await assetRecord.update(assetData);
}
if (asset.labelIds && Array.isArray(asset.labelIds)) {
for (const labelId of asset.labelIds) {
const labelRecord = await db.Label.findOne({
where: { stigmanLabelId: labelId, collectionId: collectionRecord.collectionId },
});

if (labelRecord) {
await db.AssetLabels.findOrCreate({
where: {
assetId: assetRecord.assetId,
labelId: labelRecord.labelId
},
defaults: {
assetId: assetRecord.assetId,
labelId: labelRecord.labelId,
collectionId: collectionRecord.collectionId
}
});
}
}
}
}

res.status(200).json({ message: 'Collection and Assets Imported Successfully' });
res.status(200).json({ message: 'Collection, Assets, and Labels Imported Successfully' });
} catch (error) {
// Log the error and send a server error response
console.error(error);
res.status(500).json({ message: 'Internal Server Error' });
}
}
}
5 changes: 0 additions & 5 deletions Api/Controllers/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,26 @@ const labelService = require('../Services/mysql/labelService')


module.exports.getLabels = async function getLabels(req, res, next){
// res.status(201).json({message: "getLabels Method called successfully"})
var labels = await labelService.getLabels(req,res,next);
res.status(201).json(labels)
}

module.exports.getLabel = async function getLabel(req, res, next){
// res.status(201).json({message: "getLabel Method called successfully"});
var label = await labelService.getLabel(req,res,next);
res.status(201).json(label)
}

module.exports.postLabel = async function postLabel(req, res, next){
//res.status(201).json({message: "post:Label Method called successfully"});
var label = await labelService.postLabel(req,res,next);
res.status(201).json(label)
}

module.exports.putLabel = async function putLabel(req, res, next){
// res.status(201).json({message: "putLabel Method called successfully"});
var label = await labelService.putLabel(req,res,next);
res.status(201).json(label)
}

module.exports.deleteLabel= async function deleteLabel(req, res, next){
// res.status(201).json({message: "deleteLabel Method called successfully"});
var label = await labelService.deleteLabel(req,res,next);
res.status(201).json(label)
}
20 changes: 20 additions & 0 deletions Api/Models/assetLabels.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = (sequelize, DataTypes) => {
const AssetLabels = sequelize.define('assetlabels', {
assetId: {
type: DataTypes.INTEGER,
primaryKey: true,
},
collectionId: {
type: DataTypes.INTEGER,
},
labelId: {
type: DataTypes.INTEGER,
primaryKey: true,
},
}, {
tableName: 'assetlabels',
timestamps: false,
});

return AssetLabels;
};
26 changes: 26 additions & 0 deletions Api/Models/label.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = (sequelize, DataTypes) => {
const Label = sequelize.define('label', {
labelId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
collectionId: {
type: DataTypes.INTEGER,
},
description: {
type: DataTypes.STRING(255),
},
labelName: {
type: DataTypes.STRING(50),
},
stigmanLabelId: {
type: DataTypes.STRING(50),
},
}, {
tableName: 'label',
timestamps: false,
});

return Label;
};
100 changes: 52 additions & 48 deletions Api/Services/mysql/assetLabelService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ const dbUtils = require('./utils')
const mysql = require('mysql2')

exports.getAssetLabels = async function getAssetLabels(req, res, next) {

if (!req.params.collectionId) {
console.info('getAssetLabels collectionId not provided.');
return next({
status: 422,
errors: {
collectionId: 'is required',
}
});
}
try {
let connection
connection = await dbUtils.pool.getConnection()
let sql = "SELECT t1.assetId, assetName, t1.labelId, labelName FROM poamtracking.assetlabels t1 " +
"INNER JOIN poamtracking.asset t2 ON t1.assetId = t2.assetId " +
"INNER JOIN poamtracking.label t3 ON t1.labelId = t3.labelId " +
"ORDER BY t3.labelName"

let [rowAssetLabels] = await connection.query(sql)
console.log("rowAssets: ", rowAssetLabels[0])
let sql = `
SELECT t1.assetId, assetName, t1.labelId, labelName
FROM poamtracking.assetlabels t1
INNER JOIN poamtracking.asset t2 ON t1.assetId = t2.assetId
INNER JOIN poamtracking.label t3 ON t1.labelId = t3.labelId
WHERE t3.collectionId = ?
ORDER BY t3.labelName
`;

let [rowAssetLabels] = await connection.query(sql, [req.params.collectionId]);
await connection.release()

var size = Object.keys(rowAssetLabels).length
Expand All @@ -46,13 +57,11 @@ exports.getAssetLabels = async function getAssetLabels(req, res, next) {
}
catch (error) {
let errorResponse = { null: "null" }
//await connection.release()
return errorResponse;
}
}

exports.getAssetLabelsByAsset = async function getAssetLabelsByAsset(req, res, next) {
//console.log("getAssetLabels (Service) ...");
if (!req.params.assetId) {
console.info('getAssetLabelByAsset assetId not provided.');
return next({
Expand Down Expand Up @@ -200,46 +209,49 @@ exports.getAssetLabel = async function getAssetLabel(req, res, next) {
}

exports.postAssetLabel = async function posAssetLabel(req, res, next) {
// res.status(201).json({ message: "postAsset (Service) Method called successfully" });

if (!req.body.assetId) {
console.info('postAssetLabel assetId not provided.');
console.info('postAssetLabel assetId not provided.');
return next({
status: 422,
errors: {
assetId: 'is required',
}
});
}

if (!req.body.labelId) {
} else if (!req.body.labelId) {
console.info('postAssetLabel labelId not provided.');
return next({
status: 422,
errors: {
labelId: 'is required',
}
});
} else if (!req.body.collectionId) {
console.info('postAssetLabel collectionId not provided.');
return next({
status: 422,
errors: {
collectionId: 'is required',
}
});
}


try {
let connection
connection = await dbUtils.pool.getConnection()

let sql_query = `INSERT INTO poamtracking.assetlabels (assetId, labelId)
values (?, ?)`
let sql_query = `INSERT INTO poamtracking.assetlabels (assetId, collectionId, labelId)
values (?, ?, ?)`

await connection.query(sql_query, [req.body.assetId, req.body.labelId])
await connection.query(sql_query, [req.body.assetId, req.body.collectionId, req.body.labelId])
await connection.release()

let sql = "SELECT t1.assetId, assetName, t1.labelId, labelName FROM poamtracking.assetlabels t1 " +
"INNER JOIN poamtracking.asset t2 ON t1.assetId = t2.assetId " +
"INNER JOIN poamtracking.label t3 ON t1.labelId = t3.labelId " +
"WHERE t1.assetId = " + req.body.assetId + " AND t1.labelId = " + req.body.labelId +
" ORDER BY t3.labelName"
let [rowAssetLabel] = await connection.query(sql)
console.log("rowAssetLabel: ", rowAssetLabel[0])
"WHERE t1.assetId = ? AND t1.labelId = ? " +
"ORDER BY t3.labelName"
let [rowAssetLabel] = await connection.query(sql, [req.body.assetId, req.body.labelId])
await connection.release()

var assetLabel = [rowAssetLabel[0]]
Expand All @@ -255,31 +267,24 @@ exports.postAssetLabel = async function posAssetLabel(req, res, next) {
}

exports.putAssetLabel = async function putAssetLabel(req, res, next) {
// res.status(201).json({ message: "putAssetLabel(Service) Method called successfully" });

if (!req.body.assetId) {
console.info('putAssetLabel assetId not provided.');
return next({
status: 422,
errors: {
assetId: 'is required',
}
});
}

if (!req.body.labelId) {
console.info('putAssetLabel labelId not provided.');
return next({
status: 422,
errors: {
labelId: 'is required',
}
});
}

if (!req.body.assetId) {
console.info('postAssetLabel assetId not provided.');
return next({
status: 422,
errors: {
assetId: 'is required',
}
});
} else if (!req.body.labelId) {
console.info('postAssetLabel labelId not provided.');
return next({
status: 422,
errors: {
labelId: 'is required',
}
});
}
try {
// Noting to update, only unique index, if we get here, just return what was sent in.

const message = new Object()
message.assetId = req.body.assetId
message.labelId = req.body.labelId
Expand All @@ -294,7 +299,6 @@ exports.putAssetLabel = async function putAssetLabel(req, res, next) {
}

exports.deleteAssetLabel = async function deleteAssetLabel(req, res, next) {
// res.status(201).json({ message: "deletePermission (Service) Method called successfully" });
if (!req.params.assetId) {
console.info('deleteAssetLabel assetId not provided.');
return next({
Expand Down
2 changes: 0 additions & 2 deletions Api/Services/mysql/collectionApproverService.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ exports.postCollectionApprover = async function postCollectionApprover(req, res,

let sql_query = `INSERT INTO poamtracking.collectionapprovers (collectionId, userId, status) values (?, ?, ?)`

//await connection.query(sql_query, [req.body.labelName, req.body.description, req.body.poamCount])
//await connection.release()
await connection.query(sql_query, [req.body.collectionId, req.body.userId, req.body.status]);
await connection.release();

Expand Down
28 changes: 28 additions & 0 deletions Api/Services/mysql/collectionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,34 @@ exports.getCollection = async function getCollection(userName, collectionId, req



}

exports.getCollectionAssetLabel = async function getCollectionAssetLabel(req, res, next) {
let connection;
try {
connection = await dbUtils.pool.getConnection();
let sql = `
SELECT l.labelName, COUNT(pl.labelId) AS labelCount
FROM poamtracking.assetlabels pl
INNER JOIN poamtracking.asset p ON pl.assetId = p.assetId
INNER JOIN poamtracking.label l ON pl.labelId = l.labelId
WHERE p.collectionId = ?
GROUP BY l.labelName;
`;
let [rows] = await connection.query(sql, [req.params.collectionId]);

let assetLabel = rows.map(row => ({
label: row.labelName,
labelCount: row.labelCount
}));

return { assetLabel };
} catch (error) {
console.error("Error fetching asset label counts: ", error);
throw new Error("Unable to fetch asset label counts");
} finally {
if (connection) await connection.release();
}
}

exports.getCollectionPoamStatus = async function getCollectionPoamStatus( req, res, next){
Expand Down
Loading

0 comments on commit eb90aa7

Please sign in to comment.