const Entry = require("../models/Entry"); const EntrySeen = require("../models/EntrySeen"); class EntrySeenController { constructor() { if (!EntrySeenController.instance) { EntrySeenController.instance = this; } return EntrySeenController.instance; } async getEntrySeens(req, res) { const entrySeens = await EntrySeen.findAll(); res.status(200).json(entrySeens); } async getEntrySeenById(req, res) { const entrySeen = await EntrySeen.findByPk(req.params.id); if (!entrySeen) { res.status(404).json({ message: "Entry seen record not found." }); } res.status(200).json(entrySeen); } async createEntrySeen(req, res) { const { entry_id, date_seen, seen_with } = req.body; // ? checking the an entry with the given id does exist const entry = await Entry.findByPk(entry_id); if (!entry) { return res.status(404).json({ error: "Entry not found" }); } // Create a new EntrySeen record const newEntrySeen = await EntrySeen.create({ entry_id, date_seen, seen_with, }); res.status(201).json(newEntrySeen); } async getEntrySeensByEntryId(req, res) { const { entryId } = req.params; const entrySeens = await EntrySeen.findAll({ where: { entry_id: entryId }, order: [["date_seen", "DESC"]], }); if (entrySeens.length > 0) { res.status(200).json(entrySeens); } else { res .status(404) .json({ message: "No records found for the given entry ID." }); } } async updateEntrySeen(req, res) { const { date_seen, seen_with } = req.body; const updated = await EntrySeen.update( { date_seen, seen_with }, { where: { id: req.params.id } }, ); if (updated[0] > 0) { const updatedEntrySeen = await EntrySeen.findByPk(req.params.id); res.status(200).json(updatedEntrySeen); } else { res.status(404).json({ message: "Entry seen record not found." }); } } async deleteEntrySeen(req, res) { const entrySeen = await EntrySeen.findByPk(req.params.id); if (!entrySeen) res.status(404).json({ message: "Entry seen record not found." }); const deleted = await entrySeen.destroy(); if (deleted) { res.status(204).send(); } } } module.exports = new EntrySeenController();