Skip to content

Commit

Permalink
Merge branch 'dev' into 47-add-schema-for-day-table-and-add-day-table…
Browse files Browse the repository at this point in the history
…-to-db
  • Loading branch information
ThatMegamind authored Feb 18, 2024
2 parents 50b3ba1 + 8a05522 commit ae45e06
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 40 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const email = require('./routes/nodeMailer');
const app = express();

const catalogRouter = require('./routes/catalog');
const dayRouter = require('./routes/day');

const PORT = process.env.PORT || 3001;

Expand All @@ -33,6 +34,7 @@ app.use('/users', users);
app.use('/catalog', catalogRouter);
app.use('/nodeMailer', email);
app.use('/auth', authRouter);
app.use('/day', dayRouter);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
30 changes: 29 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ const isInteger = (value) => {
return value && /^\d+$/.test(value);
};

// dependency for publishedSchedule.js
const calculateYear = (eventDate, gradeLevel) => {
if (gradeLevel) {
const currentDay = new Date(eventDate);
// console.log('current day', currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1));
if (gradeLevel.toLowerCase() === 'junior') {
// if the current month is august or later
// then junior will be current year + 2
// otherwise junior will be current year + 1
// months are zero indexed
return [(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10)];
}
if (gradeLevel.toLowerCase() === 'senior') {
// if the current month is august or later
// then senior will be current year + 1
// otherwise senior will be current year
return [(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10)];
}
if (gradeLevel.toLowerCase() === 'both') {
return [
(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10),
(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10),
];
}
}
return [];
};

const isObject = (o) => {
return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o);
};
Expand Down Expand Up @@ -57,4 +85,4 @@ const keysToCamel = (data) => {
return data;
};

module.exports = { keysToCamel, isInteger };
module.exports = { keysToCamel, isInteger, calculateYear };
103 changes: 103 additions & 0 deletions routes/day.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const express = require('express');
const { db } = require('../server/db');
const { keysToCamel } = require('../common/utils');

const dayRouter = express.Router();

// GET - returns all days in the table
dayRouter.get('/', async (req, res) => {
try {
const allDays = await db.query(`SELECT * FROM day;`);
res.status(200).json(keysToCamel(allDays));
} catch (err) {
res.status(500).send(err.message);
}
});

// GET - returns a day by eventDate parameter
dayRouter.get('/date', async (req, res) => {
try {
const { eventDate } = req.query;
const dayByDate = await db.query(`SELECT * FROM day WHERE event_date = $1;`, [eventDate]);
res.status(200).json(keysToCamel(dayByDate));
} catch (err) {
res.status(500).send(err.message);
}
});

// GET - returns a day by id
dayRouter.get('/:id', async (req, res) => {
try {
const { id } = req.params;
const dayById = await db.query(`SELECT * FROM day WHERE id = $1;`, [id]);
res.status(200).json(keysToCamel(dayById));
} catch (err) {
res.status(500).send(err.message);
}
});

// POST - creates a new day
dayRouter.post('/', async (req, res) => {
try {
const { eventDate, location, notes } = req.body;
const newDay = await db.query(
`
INSERT INTO day (
id,
event_date,
start_time,
end_time,
location,
notes,
day_count
) VALUES (
nextval('day_id_seq'), $1, '23:59:59', '00:00:00', $2, $3, 0
) RETURNING id;
`,
[eventDate, location, notes],
);
res.status(201).json({
status: 'Success',
id: newDay[0].id,
});
} catch (err) {
res.status(500).send(err.message);
}
});

// PUT - modifies an existing day
dayRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { eventDate, startTime, endTime, location, notes } = req.body;
const updatedDay = await db.query(
`
UPDATE day
SET
event_date = COALESCE($1, event_date),
start_time = COALESCE($2, start_time),
end_time = COALESCE($3, end_time),
location = COALESCE($4, location),
notes = COALESCE($5, notes)
WHERE id = $6
RETURNING *;
`,
[eventDate, startTime, endTime, location, notes, id],
);
res.status(200).send(keysToCamel(updatedDay));
} catch (err) {
res.status(500).send(err.message);
}
});

// DELETE - deletes an existing day
dayRouter.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
const deletedDay = await db.query(`DELETE FROM day WHERE id = $1 RETURNING *;`, [id]);
res.status(200).send(keysToCamel(deletedDay));
} catch (err) {
res.status(500).send(err.message);
}
});
module.exports = dayRouter;
Loading

0 comments on commit ae45e06

Please sign in to comment.