diff --git a/client/html/pages/catalog/catalog.html b/client/html/pages/catalog/catalog.html index d64d261..90f35ec 100644 --- a/client/html/pages/catalog/catalog.html +++ b/client/html/pages/catalog/catalog.html @@ -21,25 +21,6 @@ -
@@ -50,9 +31,12 @@No shirts available in the catalog.
'); + shirtsContainer.append('Creator: ${shirt.creator ? shirt.creator.username : 'Unknown'}
-Price: ${shirt.Price} currency
-For Sale: ${shirt.IsForSale ? 'Yes' : 'No'}
- View Details + shirts.forEach((shirt, index) => { + const shirtPanel = ` +Creator: ${shirt.creator ? shirt.creator.username : 'Unknown'}
+Price: ${shirt.Price} currency
+For Sale: ${shirt.IsForSale ? 'Yes' : 'No'}
+${shirt.Description}
-Creator: ${shirt.creator ? shirt.creator.username : 'Unknown'}
-Price: ${shirt.Price} currency
-For Sale: ${shirt.IsForSale ? 'Yes' : 'No'}
- ${isOwner ? 'You own this shirt
' : (shirt.IsForSale ? '' : '')} +By ${shirt.creator ? shirt.creator.username : 'Unknown'}
+${shirt.Description}
+Price: ${shirt.Price} currency
+For Sale: ${shirt.IsForSale ? 'Yes' : 'No'}
+Error loading your shirts. Please try again later.
'); + } }); -} + } -function displayUserShirts(shirts) { + function displayUserShirts(shirts) { const shirtsContainer = $('#user-shirts'); shirtsContainer.empty(); diff --git a/server/functions/api/models/User.js b/server/functions/api/models/User.js index 7e91786..5c4bb40 100644 --- a/server/functions/api/models/User.js +++ b/server/functions/api/models/User.js @@ -116,6 +116,11 @@ forumPostCount: { default: 0 }, +inventory: [{ + type: mongoose.Schema.Types.ObjectId, + ref: 'Asset' +}], + }); diff --git a/server/functions/api/routes/shirt.js b/server/functions/api/routes/shirt.js index b25bc73..ea12ec9 100644 --- a/server/functions/api/routes/shirt.js +++ b/server/functions/api/routes/shirt.js @@ -213,34 +213,90 @@ router.get('/catalog', async (req, res) => { router.post('/purchase/:id', authenticateToken, async (req, res) => { try { - const shirt = await Asset.findOne({ _id: req.params.id, AssetType: 'Shirt', IsForSale: 1 }); - if (!shirt) { - return res.status(404).json({ error: 'Shirt not found or not for sale' }); - } + const shirt = await Asset.findOne({ _id: req.params.id, AssetType: 'Shirt', IsForSale: 1 }); + if (!shirt) { + return res.status(404).json({ error: 'Shirt not found or not for sale' }); + } - const user = await User.findById(req.user.userId); - if (user.currency < shirt.Price) { - return res.status(400).json({ error: 'Insufficient funds' }); - } + const user = await User.findById(req.user.userId); + if (!user) { + return res.status(404).json({ error: 'User not found' }); + } - if (shirt.creator.toString() === user._id.toString()) { - return res.status(400).json({ error: 'You already own this shirt' }); - } + if (user.inventory && user.inventory.includes(shirt._id)) { + return res.status(400).json({ error: 'You already own this shirt' }); + } - user.currency -= shirt.Price; - user.inventory.push(shirt._id); - await user.save(); + if (user.currency < shirt.Price) { + return res.status(400).json({ error: 'Insufficient funds' }); + } - shirt.Sales += 1; - await shirt.save(); + if (shirt.creator.toString() === user._id.toString()) { + return res.status(400).json({ error: 'You already own this shirt' }); + } + + user.currency -= shirt.Price; + + if (!user.inventory) { + user.inventory = []; + } + + user.inventory.push(shirt._id); + await user.save(); - res.json({ success: true, newBalance: user.currency }); + shirt.Sales += 1; + await shirt.save(); + + res.json({ success: true, newBalance: user.currency }); } catch (error) { - console.error('Error purchasing shirt:', error); - res.status(500).json({ error: 'Internal server error' }); + console.error('Error purchasing shirt:', error); + res.status(500).json({ error: 'Internal server error', details: error.message }); } }); - +router.get('/check-ownership/:id', authenticateToken, async (req, res) => { + try { + const shirt = await Asset.findOne({ _id: req.params.id, AssetType: 'Shirt' }); + if (!shirt) { + return res.status(404).json({ error: 'Shirt not found' }); + } + + const user = await User.findById(req.user.userId); + if (!user) { + return res.status(404).json({ error: 'User not found' }); + } + + const owned = user.inventory && user.inventory.includes(shirt._id); + const isCreator = shirt.creator.toString() === user._id.toString(); + + // Include the shirt's price in the response + res.json({ owned, isCreator, price: shirt.Price }); + } catch (error) { + console.error('Error checking shirt ownership:', error); + res.status(500).json({ error: 'Internal server error', details: error.message }); + } +}); + +router.get('/user/id/:id', authenticateToken, async (req, res) => { + try { + const userId = req.params.id; + const user = await User.findById(userId); + if (!user) { + return res.status(404).json({ error: 'User not found' }); + } + + const createdShirts = await Asset.find({ creator: userId, AssetType: 'Shirt' }).sort({ createdAt: -1 }); + const ownedShirts = await Asset.find({ _id: { $in: user.inventory }, AssetType: 'Shirt' }).sort({ createdAt: -1 }); + const allShirts = [...createdShirts, ...ownedShirts]; + const uniqueShirts = Array.from(new Set(allShirts.map(s => s._id.toString()))) + .map(_id => allShirts.find(s => s._id.toString() === _id)); + + res.json(uniqueShirts); + } catch (error) { + console.error('Error fetching user shirts by ID:', error); + res.status(500).json({ error: 'Internal server error' }); + } +}); + router.get('/user', authenticateToken, async (req, res) => { try { const shirts = await Asset.find({ creator: req.user.userId, AssetType: 'Shirt' }).sort({ updatedAt: -1 });