Skip to content

Commit

Permalink
changed uploading shirts to ids, uploading shirts work sends to s3 bu…
Browse files Browse the repository at this point in the history
…t its not sending to the website...
  • Loading branch information
singharaj-usai committed Oct 16, 2024
1 parent 35324e6 commit 5b41e69
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions server/functions/api/routes/shirt.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ 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);

// Fetch shirts owned by user
const ownedShirts = await Asset.find({
_id: { $in: user.inventory },
AssetType: 'Shirt',
}).populate('creator', 'username');
});

console.log('Owned shirts found:', ownedShirts.length);

Expand Down Expand Up @@ -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 });
}
});

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5b41e69

Please sign in to comment.