Skip to content

Commit

Permalink
Cleaning up and testing PR
Browse files Browse the repository at this point in the history
  • Loading branch information
abedmohammed committed Jan 20, 2025
1 parent c2fcf8e commit 25395f2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 103 deletions.
208 changes: 110 additions & 98 deletions backend/src/controllers/journalController.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,129 @@
const journalService = require('../services/journalService');

const createDailyJournal = async (req, res) => {
try {
const { title, content } = req.body;
const userId = req.user.id;
const dailyRecordId = req.dailyRecord.id;

if (!title || !content) {
return res.status(400).json({ error: 'Missing required fields' });
}

const journal = await journalService.createDailyJournal(userId, dailyRecordId, title, content);


const response = {
id: journal.id,
title: journal.title,
content: journal.content,
userId: journal.user_id,
dailyRecordId: journal.daily_record_id,
createdAt: journal.created_at,
updatedAt: journal.updated_at
};

res.status(201).json(response);
} catch (error) {
if (error.message === 'A daily journal already exists for this record') {
res.status(409).json({ error: error.message });
} else {
res.status(500).json({ error: 'Failed to create daily journal' });
}
try {
const { title, content } = req.body;
const userId = req.token.user.id;
const dailyRecordId = req.dailyRecord.id;

if (!title || !content) {
return res.status(400).json({ error: 'Missing required fields' });
}

const journal = await journalService.createDailyJournal(
userId,
dailyRecordId,
title,
content
);

const response = {
id: journal.id,
title: journal.title,
content: journal.content,
userId: journal.user_id,
dailyRecordId: journal.daily_record_id,
createdAt: journal.created_at,
updatedAt: journal.updated_at,
};

res.status(201).json(response);
} catch (error) {
if (error.message === 'A daily journal already exists for this record') {
res.status(409).json({ error: error.message });
} else {
res.status(500).json({ error: 'Failed to create daily journal' });
}
}
};

const createTabJournal = async (req, res) => {
try {
const { title, content, tabId } = req.body;
const userId = req.user.id;

if (!title || !content || !tabId) {
return res.status(400).json({ error: 'Missing required fields' });
}

const journal = await journalService.createTabJournal(userId, tabId, title, content);


const response = {
id: journal.id,
title: journal.title,
content: journal.content,
userId: journal.user_id,
tabId: journal.tab_id,
createdAt: journal.created_at,
updatedAt: journal.updated_at
};

res.status(201).json(response);
} catch (error) {
res.status(500).json({ error: 'Failed to create journal' });
try {
const { title, content, tabId } = req.body;
const userId = req.token.user.id;

if (!title || !content || !tabId) {
return res.status(400).json({ error: 'Missing required fields' });
}

const journal = await journalService.createTabJournal(
userId,
tabId,
title,
content
);

const response = {
id: journal.id,
title: journal.title,
content: journal.content,
userId: journal.user_id,
tabId: journal.tab_id,
createdAt: journal.created_at,
updatedAt: journal.updated_at,
};

res.status(201).json(response);
} catch (error) {
res.status(500).json({ error: 'Failed to create journal' });
}
};

const updateJournalInfo = async (req, res) => {
try {
const { title, tabId } = req.body;
const { id } = req.params;
const userId = req.user.id;

if (!title) {
return res.status(400).json({ error: 'Title is required' });
}

const journal = await journalService.updateJournalInfo(id, userId, title, tabId);


const response = {
id: journal.id,
title: journal.title,
tabId: journal.tab_id,
updatedAt: journal.updated_at
};

res.status(200).json(response);
} catch (error) {
if (error.message === 'Cannot modify tab of a daily record journal') {
res.status(400).json({ error: error.message });
} else if (error.message === 'Journal not found') {
res.status(404).json({ error: error.message });
} else {
res.status(500).json({ error: 'Failed to update journal' });
}
try {
const { title, tabId } = req.body;
const { id } = req.params;
const userId = req.token.user.id;

if (!title) {
return res.status(400).json({ error: 'Title is required' });
}

const journal = await journalService.updateJournalInfo(
id,
userId,
title,
tabId
);

const response = {
id: journal.id,
title: journal.title,
tabId: journal.tab_id,
updatedAt: journal.updated_at,
};

res.status(200).json(response);
} catch (error) {
if (error.message === 'Cannot modify tab of a daily record journal') {
res.status(400).json({ error: error.message });
} else if (error.message === 'Journal not found') {
res.status(404).json({ error: error.message });
} else {
res.status(500).json({ error: 'Failed to update journal' });
}
}
};

const deleteJournal = async (req, res) => {
try {
const { id } = req.params;
const userId = req.user.id;

await journalService.deleteJournal(id, userId);
res.status(200).json({ message: 'Journal deleted successfully' });
} catch (error) {
if (error.message === 'Journal not found or unauthorized') {
res.status(404).json({ error: error.message });
} else {
res.status(500).json({ error: 'Failed to delete journal' });
}
try {
const { id } = req.params;
const userId = req.token.user.id;

await journalService.deleteJournal(id, userId);
res.status(200).json({ message: 'Journal deleted successfully' });
} catch (error) {
if (error.message === 'Journal not found or unauthorized') {
res.status(404).json({ error: error.message });
} else {
res.status(500).json({ error: 'Failed to delete journal' });
}
}
};

module.exports = {
createDailyJournal,
createTabJournal,
updateJournalInfo,
deleteJournal
};
createDailyJournal,
createTabJournal,
updateJournalInfo,
deleteJournal,
};
17 changes: 12 additions & 5 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ const tabRoutes = require('./routes/tabsRoute');
const journalRoutes = require('./routes/journalRoute');

// Route middleware
app.use('/api/journals', authProtect, dailyRecordMiddleware, journalRoutes);
app.use('/api/warehouses', authProtect, dailyRecordMiddleware, warehouseRoutes);
app.use('/api/daily-records', dailyRecordMiddleware, dailyRecordRoutes);

app.use('/api/auth', authRoute);
app.use('/api/moods', dailyRecordMiddleware, moodRoutes);
app.use('/api/tabs', dailyRecordMiddleware, tabRoutes);
app.use(
'/api/daily-records',
authProtect,
dailyRecordMiddleware,
dailyRecordRoutes
);

app.use('/api/journals', authProtect, dailyRecordMiddleware, journalRoutes);
app.use('/api/tabs', authProtect, dailyRecordMiddleware, tabRoutes);

// app.use('/api/moods', dailyRecordMiddleware, moodRoutes);

// Default route
app.get('/api', (req, res) =>
Expand All @@ -51,7 +59,6 @@ app.get('/api/status', (req, res) => res.send('Success.'));

// Test sum endpoint
app.get('/api/sum', (req, res) => {
log('GET /api/sum');
log('GET /api/sum');
const { a, b } = req.query;

Expand Down

0 comments on commit 25395f2

Please sign in to comment.