diff --git a/server/functions/api/routes/shirt.js b/server/functions/api/routes/shirt.js index 2d28d68..52ed8fe 100644 --- a/server/functions/api/routes/shirt.js +++ b/server/functions/api/routes/shirt.js @@ -87,9 +87,9 @@ router.get('/user', authenticateToken, async (req, res) => { // Fetch shirts created by user const createdShirts = await Asset.find({ - creator: user._id, + creator: user.userId, AssetType: 'Shirt', - }).populate('creator', 'username'); + }); console.log('Created shirts found:', createdShirts.length); @@ -97,7 +97,7 @@ router.get('/user', authenticateToken, async (req, res) => { const ownedShirts = await Asset.find({ _id: { $in: user.inventory }, AssetType: 'Shirt', - }).populate('creator', 'username'); + }); console.log('Owned shirts found:', ownedShirts.length); @@ -132,19 +132,19 @@ router.get('/user', authenticateToken, async (req, res) => { router.get('/:id', async (req, res) => { try { const id = req.params.id; - const shirt = await Asset.findOne({ _id: id, AssetType: 'Shirt' }).populate( - 'creator', - 'username' - ); + const shirt = await Asset.findOne({ assetId: id, AssetType: 'Shirt' }); if (!shirt) { return res.status(404).json({ error: 'Shirt not found' }); } - res.json(shirt); + const user = await User.findOne({ userId: shirt.creator }); + const shirtWithUsername = { + ...shirt.toObject(), + creatorUsername: user ? user.username : 'Unknown', + }; + res.json(shirtWithUsername); } catch (error) { console.error('Error fetching shirt:', error); - res - .status(500) - .json({ error: 'Error fetching shirt', details: error.message }); + res.status(500).json({ error: 'Error fetching shirt', details: error.message }); } }); @@ -267,11 +267,11 @@ router.post( await thumbnailQueue.addToQueue(shirtassetId, 'Shirt'); - await User.findByIdAndUpdate(req.user._id, { - $push: { shirts: shirt._id }, + await User.findOneAndUpdate({ userId: req.user.userId }, { + $push: { shirts: shirt.assetId }, }); - res.status(201).json({ shirtId: shirt._id, assetId: shirt.assetId }); + res.status(201).json({ shirtId: shirt.assetId, assetId: shirt.assetId }); } catch (error) { console.error('Error saving shirt:', error); res @@ -509,7 +509,7 @@ router.get('/user/:username', authenticateToken, async (req, res) => { return res.status(404).json({ error: 'User not found' }); } const createdShirts = await Asset.find({ - creator: user._id, + creator: user.userId, // Use userId (Number) AssetType: 'Shirt', }).sort({ createdAt: -1 }); const ownedShirts = await Asset.find({ @@ -520,12 +520,28 @@ router.get('/user/:username', authenticateToken, async (req, res) => { const uniqueShirts = Array.from( new Set(allShirts.map((s) => s._id.toString())) ).map((_id) => allShirts.find((s) => s._id.toString() === _id)); - res.json(uniqueShirts); + + // Fetch all unique creators + const creatorIds = uniqueShirts.map(shirt => shirt.creator); + const creators = await User.find({ userId: { $in: creatorIds } }); + const creatorMap = {}; + creators.forEach(user => { + creatorMap[user.userId] = user.username; + }); + + // Append username to each shirt + const shirtsWithUsernames = uniqueShirts.map(shirt => ({ + ...shirt.toObject(), + creatorUsername: creatorMap[shirt.creator] || 'Unknown', + })); + + res.json(shirtsWithUsernames); } catch (error) { console.error('Error fetching user shirts:', error); res.status(500).json({ error: 'Internal server error' }); } }); + // Function to generate a unique asset ID function generateAssetId() { const timestamp = Date.now().toString(36); diff --git a/uploads/1729119022605-573616139c1e4a9ef51962a243f26007.png b/uploads/1729119022605-573616139c1e4a9ef51962a243f26007.png new file mode 100644 index 0000000..eb04efb Binary files /dev/null and b/uploads/1729119022605-573616139c1e4a9ef51962a243f26007.png differ diff --git a/uploads/1729119448995-573616139c1e4a9ef51962a243f26007.png b/uploads/1729119448995-573616139c1e4a9ef51962a243f26007.png new file mode 100644 index 0000000..eb04efb Binary files /dev/null and b/uploads/1729119448995-573616139c1e4a9ef51962a243f26007.png differ diff --git a/uploads/1729119715578-573616139c1e4a9ef51962a243f26007.png b/uploads/1729119715578-573616139c1e4a9ef51962a243f26007.png new file mode 100644 index 0000000..eb04efb Binary files /dev/null and b/uploads/1729119715578-573616139c1e4a9ef51962a243f26007.png differ diff --git a/uploads/1729120357394-573616139c1e4a9ef51962a243f26007.png b/uploads/1729120357394-573616139c1e4a9ef51962a243f26007.png new file mode 100644 index 0000000..eb04efb Binary files /dev/null and b/uploads/1729120357394-573616139c1e4a9ef51962a243f26007.png differ diff --git a/uploads/1729120562492-573616139c1e4a9ef51962a243f26007.png b/uploads/1729120562492-573616139c1e4a9ef51962a243f26007.png new file mode 100644 index 0000000..eb04efb Binary files /dev/null and b/uploads/1729120562492-573616139c1e4a9ef51962a243f26007.png differ