diff --git a/client/html/pages/my/create/create.html b/client/html/pages/my/create/create.html index be9fa76..55a4430 100644 --- a/client/html/pages/my/create/create.html +++ b/client/html/pages/my/create/create.html @@ -62,23 +62,34 @@

Create

-
- My Games - My Shirts - My T-Shirts - My Pants - My Models - Building Games +
+ My Games + My Shirts
- -
-
-

MY GAMES

+
+
+ +
+
+

MY GAMES

+
+
+
+
+
-
-
+
+ +
+
+

MY SHIRTS

+
+
+
+
+
@@ -247,11 +258,46 @@
+ + + + \ No newline at end of file diff --git a/client/js/shirts.js b/client/js/shirts.js new file mode 100644 index 0000000..ccca3df --- /dev/null +++ b/client/js/shirts.js @@ -0,0 +1,122 @@ +function initializeShirts() { + loadShirts(); + setupShirtToggler(); + setupShirtEditModal(); +} + +function loadShirts() { + const token = localStorage.getItem('token'); + $.ajax({ + url: '/api/shirts/user', + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}` + }, + success: function(shirts) { + displayShirts(shirts); + }, + error: function(xhr, status, error) { + console.error('Error fetching shirts:', error); + } + }); +} + +function displayShirts(shirts) { + const shirtsContainer = $('#shirts-container'); + shirtsContainer.empty(); + + if (shirts.length === 0) { + shirtsContainer.append('

You have not created any shirts yet.

'); + return; + } + + shirts.forEach(shirt => { + const shirtElement = $(` +
+

${shirt.title}

+

${shirt.description}

+ ${shirt.title} +

Asset ID: ${shirt.assetId}

+ +
+ `); + shirtsContainer.append(shirtElement); + }); +} + +function setupShirtToggler() { + $('#list-tab a').on('click', function (e) { + e.preventDefault(); + $(this).tab('show'); + }); + + $('#list-shirts-list').on('shown.bs.tab', function (e) { + loadShirts(); + }); +} + +function setupShirtEditModal() { + $(document).on('click', '.edit-shirt', function() { + const shirtId = $(this).data('shirt-id'); + openShirtEditModal(shirtId); + }); + + $('#save-shirt-changes').on('click', function() { + saveShirtChanges(); + }); +} + +function openShirtEditModal(shirtId) { + const token = localStorage.getItem('token'); + $.ajax({ + url: `/api/shirts/${shirtId}`, + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}` + }, + success: function(shirt) { + $('#edit-shirt-id').val(shirt._id); + $('#edit-shirt-title').val(shirt.title); + $('#edit-shirt-description').val(shirt.description); + $('#current-shirt-thumbnail').attr('src', shirt.thumbnailUrl); + $('#editShirtModal').modal('show'); + }, + error: function(xhr, status, error) { + console.error('Error fetching shirt details:', error); + } + }); +} + +function saveShirtChanges() { + const token = localStorage.getItem('token'); + const shirtId = $('#edit-shirt-id').val(); + const title = $('#edit-shirt-title').val(); + const description = $('#edit-shirt-description').val(); + + const formData = new FormData(); + formData.append('title', title); + formData.append('description', description); + + $.ajax({ + url: `/api/shirts/${shirtId}`, + method: 'PUT', + data: formData, + processData: false, + contentType: false, + headers: { + 'Authorization': `Bearer ${token}` + }, + success: function(updatedShirt) { + $('#editShirtModal').modal('hide'); + loadShirts(); + }, + error: function(xhr, status, error) { + console.error('Error updating shirt:', error); + $('#shirt-error-message').text('Error updating shirt: ' + error).removeClass('hidden'); + } + }); +} + +$(document).ready(function() { + initializeShirts(); +}); \ No newline at end of file diff --git a/server/functions/api/routes/shirt.js b/server/functions/api/routes/shirt.js index 7ed40c8..8b895f3 100644 --- a/server/functions/api/routes/shirt.js +++ b/server/functions/api/routes/shirt.js @@ -164,12 +164,12 @@ router.post('/upload', authenticateToken, (req, res, next) => { }); // New route to update a game - router.put('/:id', authenticateToken, upload.single('thumbnail'), async (req, res) => { + router.put('/:id', authenticateToken, async (req, res) => { try { const { id } = req.params; const { title, description } = req.body; - // Check if the game exists and belongs to the current user + // Check if the shirt exists and belongs to the current user const shirt = await Shirt.findOne({ _id: id, creator: req.user.userId }); if (!shirt) { return res.status(404).json({ error: 'Shirt not found or you do not have permission to edit it' }); @@ -178,44 +178,31 @@ router.post('/upload', authenticateToken, (req, res, next) => { // Update shirt details shirt.title = filter.clean(title); shirt.description = filter.clean(description); - shirt.updatedAt = new Date(); // Explicitly set the updatedAt field + shirt.updatedAt = new Date(); - // If a new thumbnail is uploaded, update it - if (req.file) { - // Delete the old thumbnail file - if (shirt.thumbnailUrl) { - const oldThumbnailPath = path.join(__dirname, '../../../../uploads', path.basename(shirt.thumbnailUrl)); - fs.unlink(oldThumbnailPath, (err) => { - if (err) console.error('Error deleting old thumbnail:', err); - }); - } - - // Set the new thumbnail URL - shirt.thumbnailUrl = `/uploads/${req.file.filename}`; - } - - // Save the updated game - await game.save(); + // Save the updated shirt + await shirt.save(); res.json(shirt); } catch (error) { console.error('Error updating shirt:', error); - res.status(500).json({ error: 'Error updating shirt', details: error.message, stack: error.stack }); + res.status(500).json({ error: 'Error updating shirt', details: error.message }); } }); -router.get('/:id', authenticateToken, async (req, res) => { + router.get('/:id', authenticateToken, async (req, res) => { try { - const shirt = await Shirt.findById(req.params.id).populate('creator', 'username'); - if (!shirt) { - return res.status(404).json({ error: 'Shirt not found' }); - } - res.json(shirt); + const { id } = req.params; + const shirt = await Shirt.findOne({ _id: id, creator: req.user.userId }); + if (!shirt) { + return res.status(404).json({ error: 'Shirt not found or you do not have permission to view it' }); + } + res.json(shirt); } catch (error) { - console.error('Error fetching shirt:', error); - res.status(500).json({ error: 'Error fetching shirt', details: error.message }); + console.error('Error fetching shirt:', error); + res.status(500).json({ error: 'Error fetching shirt', details: error.message }); } -}); + }); router.get('/user/:username', authenticateToken, async (req, res) => { try { diff --git a/uploads/1728183299927-89437201_10216152954596170_8517994551216963584_n.png b/uploads/1728183299927-89437201_10216152954596170_8517994551216963584_n.png new file mode 100644 index 0000000..79f4d31 Binary files /dev/null and b/uploads/1728183299927-89437201_10216152954596170_8517994551216963584_n.png differ