- Created WatchBuddy model - M:N junctions to EntrySeen/EpisodeSeen and Media relation for profile pictures - Full CRUD + search + profile picture upload - WatchBuddySelect multi-select with search and inline create, replaced all seen_with text inputs - WatchBuddiesPage (grid) and WatchBuddyProfilePage (edit form, icon-based contacts, poster-grid watch history, monthly timeline)
110 lines
2.7 KiB
JavaScript
110 lines
2.7 KiB
JavaScript
const Entry = require("../models/Entry");
|
|
const EntrySeen = require("../models/EntrySeen");
|
|
const WatchBuddy = require("../models/WatchBuddy");
|
|
|
|
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, buddy_ids } = req.body;
|
|
|
|
const entry = await Entry.findByPk(entry_id);
|
|
if (!entry) {
|
|
return res.status(404).json({ error: "Entry not found" });
|
|
}
|
|
|
|
const newEntrySeen = await EntrySeen.create({
|
|
entry_id,
|
|
date_seen,
|
|
seen_with,
|
|
});
|
|
|
|
// Link watch buddies
|
|
if (Array.isArray(buddy_ids) && buddy_ids.length > 0) {
|
|
await newEntrySeen.setBuddies(buddy_ids);
|
|
}
|
|
|
|
// Auto-upgrade TV series status when first watched
|
|
if (
|
|
entry.is_tvseries &&
|
|
(!entry.watch_status || entry.watch_status === "plan_to_watch")
|
|
) {
|
|
entry.watch_status = "watching";
|
|
await entry.save();
|
|
}
|
|
|
|
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"]],
|
|
include: [{ model: WatchBuddy, as: "buddies" }],
|
|
});
|
|
|
|
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, buddy_ids } = req.body;
|
|
await EntrySeen.update(
|
|
{ date_seen, seen_with },
|
|
{ where: { id: req.params.id } },
|
|
);
|
|
|
|
const record = await EntrySeen.findByPk(req.params.id);
|
|
if (!record) {
|
|
return res.status(404).json({ message: "Entry seen record not found." });
|
|
}
|
|
|
|
if (Array.isArray(buddy_ids)) {
|
|
await record.setBuddies(buddy_ids);
|
|
}
|
|
|
|
res.status(200).json(record);
|
|
}
|
|
|
|
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();
|