diff --git a/client/js/admin/assets.js b/client/js/admin/assets.js index 939549e..53503bd 100644 --- a/client/js/admin/assets.js +++ b/client/js/admin/assets.js @@ -155,34 +155,34 @@ function displayRecentAssets(assets) { const assetsTable = ` <table class="table table-striped"> - <thead> - <tr> - <th>ID</th> - <th>Name</th> - <th>Type</th> - <th>Creator</th> - <th>Price</th> - <th>Sales</th> - <th>Actions</th> - </tr> - </thead> - <tbody> - ${assets.map((asset) => ` - <tr> - <td>${asset.assetId || 'N/A'}</td> - <td>${asset.Name || 'N/A'}</td> - <td>${asset.AssetType || 'N/A'}</td> - <td>${asset.creator && asset.creator.username ? asset.creator.username : 'Unknown'}</td> - <td>${asset.Price !== undefined ? asset.Price : 'N/A'}</td> - <td>${asset.Sales !== undefined ? asset.Sales : 'N/A'}</td> - <td> - <button class="btn btn-warning btn-xs edit-asset" data-asset-id="${asset._id}">Edit</button> - <button class="btn btn-danger btn-xs delete-asset" data-asset-id="${asset._id}">Delete</button> - ${asset.AssetType !== 'Image' ? `<button class="btn btn-secondary btn-xs redraw-asset" data-asset-id="${asset._id}">Redraw</button>` : ''} - </td> - </tr> - `).join('')} - </tbody> + <thead> + <tr> + <th>ID</th> + <th>Name</th> + <th>Type</th> + <th>Creator</th> + <th>Price</th> + <th>Sales</th> + <th>Actions</th> + </tr> + </thead> + <tbody> + ${assets.map((asset) => ` + <tr> + <td>${asset.assetId || 'N/A'}</td> + <td>${asset.Name || 'N/A'}</td> + <td>${asset.AssetType || 'N/A'}</td> + <td>${asset.creator && asset.creator.username ? asset.creator.username : 'Unknown'}</td> + <td>${asset.Price !== undefined ? asset.Price : 'N/A'}</td> + <td>${asset.Sales !== undefined ? asset.Sales : 'N/A'}</td> + <td> + <button class="btn btn-warning btn-xs edit-asset" data-asset-id="${asset._id}">Edit</button> + <button class="btn btn-danger btn-xs delete-asset" data-asset-id="${asset._id}">Delete</button> + ${asset.AssetType !== 'Image' ? `<button class="btn btn-secondary btn-xs redraw-asset" data-asset-id="${asset._id}">Redraw</button>` : ''} + </td> + </tr> + `).join('')} + </tbody> </table> `; diff --git a/client/js/admin/games.js b/client/js/admin/games.js index 4806173..ba47ffe 100644 --- a/client/js/admin/games.js +++ b/client/js/admin/games.js @@ -27,35 +27,22 @@ function displayGames(games) { games.forEach((game) => { gamesList.append(` - <div class="col-md-4 col-sm-6 mb-4"> - <div class="panel panel-primary"> - <div class="panel-heading"> - <h3 class="panel-title">${escapeHtml(game.title)}</h3> - </div> - <div class="panel-body"> - <div style="width: 100%; padding-top: 56.25%; position: relative; overflow: hidden;"> - <img src="${ - game.thumbnailUrl || - '/images/default-game-thumbnail.png' - }" alt="${escapeHtml( - game.title - )} thumbnail" class="img-responsive" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover;"> + <div class="col-md-4 col-sm-6 mb-4"> + <div class="panel panel-primary"> + <div class="panel-heading"> + <h3 class="panel-title">${escapeHtml(game.title)}</h3> + </div> + <div class="panel-body"> + <div style="width: 100%; padding-top: 56.25%; position: relative; overflow: hidden;"> + <img src="${game.thumbnailUrl || '/images/default-game-thumbnail.png'}" alt="${escapeHtml(game.title)} thumbnail" class="img-responsive" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover;"> </div> - <p><strong>Creator:</strong> ${escapeHtml( - game.creator.username - )}</p> - <p><strong>Created:</strong> ${new Date( - game.createdAt - ).toLocaleString()}</p> - <p><strong>Description:</strong> ${escapeHtml( - game.description || 'No description provided.' - )}</p> + <p><strong>Creator:</strong> ${escapeHtml(game.creator.username)}</p> + <p><strong>Created:</strong> ${new Date(game.createdAt).toLocaleString()}</p> + <p><strong>Description:</strong> ${escapeHtml(game.description || 'No description provided.')}</p> </div> <div class="panel-footer"> - <button class="btn btn-danger btn-block delete-game" data-game-id="${ - game._id - }"> - <i class="glyphicon glyphicon-trash"></i> Delete Game + <button class="btn btn-danger btn-block delete-game" data-game-id="${game._id}"> + <i class="glyphicon glyphicon-trash"></i> Delete Game </button> </div> </div> diff --git a/client/js/catalog.js b/client/js/catalog.js index a8ecc92..c526e24 100644 --- a/client/js/catalog.js +++ b/client/js/catalog.js @@ -41,7 +41,7 @@ function displayShirts(shirts) { }" style="max-height: 150px;"> <hr> <p><strong>Creator:</strong> ${ - shirt.creator ? shirt.creator.username : 'Unknown' + shirt.creator && shirt.creator.username ? shirt.creator.username : 'Unknown' }</p> <p><strong>Price:</strong> ${shirt.Price} currency</p> <p><strong>For Sale:</strong> ${ diff --git a/server/functions/api/routes/admin.js b/server/functions/api/routes/admin.js index b57655f..7b3f310 100644 --- a/server/functions/api/routes/admin.js +++ b/server/functions/api/routes/admin.js @@ -115,32 +115,6 @@ router.get('/assets/recent', authenticateToken, async (req, res) => { } }); -// redraw the specific asset -router.post('/assets/:id/redraw', authenticateToken, async (req, res) => { - const assetId = req.params.id; - - try { - const asset = await Asset.findById(assetId).populate('creator', 'username'); - if (!asset) { - return res.status(404).json({ error: 'Asset not found' }); - } - - if (asset.AssetType === 'Image') { - return res.status(400).json({ error: 'Image assets cannot be redrawn' }); - } - - try { - await thumbnailQueue.addToQueue(asset.assetId, asset.AssetType); - res.json({ message: 'Asset redraw queued successfully' }); - } catch (queueError) { - console.error('Error adding to thumbnail queue:', queueError); - res.status(500).json({ error: 'Error queuing asset redraw' }); - } - } catch (error) { - console.error('Error processing asset redraw:', error); - res.status(500).json({ error: 'Internal server error' }); - } -}); // get the asset by id router.get('/assets/:id', authenticateToken, async (req, res) => { @@ -180,6 +154,34 @@ router.get('/assets/:id', authenticateToken, async (req, res) => { } }); +// redraw the specific asset +router.post('/assets/:id/redraw', authenticateToken, async (req, res) => { + const assetId = req.params.id; + + try { + const asset = await Asset.findById(assetId).populate('creator', 'username'); + if (!asset) { + return res.status(404).json({ error: 'Asset not found' }); + } + + if (asset.AssetType === 'Image') { + return res.status(400).json({ error: 'Image assets cannot be redrawn' }); + } + + try { + await thumbnailQueue.addToQueue(asset.assetId, asset.AssetType); + res.json({ message: 'Asset redraw queued successfully' }); + } catch (queueError) { + console.error('Error adding to thumbnail queue:', queueError); + res.status(500).json({ error: 'Error queuing asset redraw' }); + } + } catch (error) { + console.error('Error processing asset redraw:', error); + res.status(500).json({ error: 'Internal server error' }); + } +}); + + // update asset router.put('/assets/:id', authenticateToken, async (req, res) => { const assetId = req.params.id; diff --git a/server/functions/api/routes/shirt.js b/server/functions/api/routes/shirt.js index 9751dc8..767eaea 100644 --- a/server/functions/api/routes/shirt.js +++ b/server/functions/api/routes/shirt.js @@ -56,6 +56,7 @@ router.get('/', async (req, res) => { const shirts = await Shirt.find().populate('creator', 'username'); res.json(shirts); } catch (error) { + console.error('Error fetching shirts:', error); res.status(500).json({ error: error.message }); } }); @@ -186,6 +187,12 @@ router.post( }); } + const user = await User.findOne({ userId: req.user.userId }); + if (!user) { + return res.status(404).json({ error: 'User not found.' }); + } + + const assetHash = generateAssetId(); const assetId = await getNextAssetId(); @@ -212,7 +219,7 @@ router.post( const asset = new Asset({ assetId: assetId, FileLocation: assetLocation, - creator: req.user.userId, + creator: user._id, // Use the user's ObjectId here AssetType: 'Image', Name: filter.clean(title), Description: filter.clean(description), @@ -251,7 +258,7 @@ router.post( const shirt = new Asset({ assetId: shirtassetId, FileLocation: shirtassetLocation, - creator: req.user.userId, + creator: user._id, // Use the user's ObjectId here AssetType: 'Shirt', Name: filter.clean(title), Description: filter.clean(description), @@ -301,9 +308,9 @@ router.delete('/:id', authenticateToken, async (req, res) => { router.get('/catalog', async (req, res) => { try { - const shirts = await Asset.find({ AssetType: 'Shirt', IsForSale: 1 }).sort({ - createdAt: -1, - }); + const shirts = await Asset.find({ AssetType: 'Shirt', IsForSale: 1 }) + .populate('creator', 'username') + .sort({ createdAt: -1 }) res.json(shirts); } catch (error) { console.error('Error fetching catalog shirts:', error); @@ -401,11 +408,11 @@ router.get('/user/id/:id', authenticateToken, async (req, res) => { const createdShirts = await Asset.find({ creator: userId, AssetType: 'Shirt', - }).sort({ createdAt: -1 }); + }).populate('creator', 'username').sort({ createdAt: -1 }); const ownedShirts = await Asset.find({ _id: { $in: user.inventory }, AssetType: 'Shirt', - }).sort({ createdAt: -1 }); + }).populate('creator', 'username').sort({ createdAt: -1 }); const allShirts = [...createdShirts, ...ownedShirts]; const uniqueShirts = Array.from( new Set(allShirts.map((s) => s._id.toString()))