Files
WaifuBoard/electron/api/list/list.controller.js

159 lines
5.5 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getList = getList;
exports.getSingleEntry = getSingleEntry;
exports.upsertEntry = upsertEntry;
exports.deleteEntry = deleteEntry;
exports.getListByFilter = getListByFilter;
const listService = __importStar(require("./list.service"));
async function getList(req, reply) {
const userId = req.user?.id;
if (!userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
try {
const results = await listService.getUserList(userId);
return { results };
}
catch (err) {
console.error(err);
return reply.code(500).send({ error: "Failed to retrieve list" });
}
}
async function getSingleEntry(req, reply) {
const userId = req.user?.id;
const { entryId } = req.params;
const { source, entry_type } = req.query;
if (!userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
if (!entryId || !source || !entry_type) {
return reply.code(400).send({ error: "Missing required identifier: entryId, source, or entry_type." });
}
try {
const entry = await listService.getSingleListEntry(userId, entryId, source, entry_type);
if (!entry) {
return reply.code(404).send({ found: false, message: "Entry not found in user list." });
}
return { found: true, entry: entry };
}
catch (err) {
console.error(err);
return reply.code(500).send({ error: "Failed to retrieve list entry" });
}
}
async function upsertEntry(req, reply) {
const userId = req.user?.id;
const body = req.body;
if (!userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
if (!body.entry_id || !body.source || !body.status || !body.entry_type) {
return reply.code(400).send({
error: "Missing required fields (entry_id, source, status, entry_type)."
});
}
try {
const entryData = {
user_id: userId,
entry_id: body.entry_id,
external_id: body.external_id,
source: body.source,
entry_type: body.entry_type,
status: body.status,
progress: body.progress || 0,
score: body.score || null,
start_date: body.start_date || null,
end_date: body.end_date || null,
repeat_count: body.repeat_count ?? 0,
notes: body.notes || null,
is_private: body.is_private ?? 0
};
const result = await listService.upsertListEntry(entryData);
return { success: true, changes: result.changes };
}
catch (err) {
console.error(err);
return reply.code(500).send({ error: "Failed to save list entry" });
}
}
async function deleteEntry(req, reply) {
const userId = req.user?.id;
const { entryId } = req.params;
const { source } = req.query; // ✅ VIENE DEL FRONT
if (!userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
if (!entryId || !source) {
return reply.code(400).send({ error: "Missing entryId or source." });
}
try {
const result = await listService.deleteListEntry(userId, entryId, source);
if (result.success) {
return { success: true, external: result.external };
}
else {
return reply.code(404).send({
error: "Entry not found or unauthorized to delete."
});
}
}
catch (err) {
console.error(err);
return reply.code(500).send({ error: "Failed to delete list entry" });
}
}
async function getListByFilter(req, reply) {
const userId = req.user?.id;
const { status, entry_type } = req.query;
if (!userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
if (!status && !entry_type) {
return reply.code(400).send({
error: "At least one filter is required (status or entry_type)."
});
}
try {
const results = await listService.getUserListByFilter(userId, status, entry_type);
return { results };
}
catch (err) {
console.error(err);
return reply.code(500).send({ error: "Failed to retrieve filtered list" });
}
}