UNPKG

@replyke/express

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

39 lines (38 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const models_1 = require("../../../models"); exports.default = async (req, res) => { const { entityId } = req.params; const projectId = req.project.id; if (!entityId) { res.status(400).json({ error: "Invalid entity ID.", code: "entity/invalid-id", }); return; } try { // Increment the views count directly and handle the deeply nested result structure const [[affectedRows, affectedCount]] = (await models_1.Entity.increment({ views: 1 }, { where: { projectId, id: entityId } })); // Type assertion for nested structure // Check if the entity was found and incremented if (affectedCount === 0 || affectedRows.length === 0) { res.status(404).json({ error: "Entity not found.", code: "entity/not-found", }); return; } // Retrieve the first updated instance const updatedEntity = affectedRows[0]; // Return the updated entity in JSON format res.status(200).json(updatedEntity.toJSON()); } catch (err) { console.error("Failed to increment entity views:", err); res.status(500).json({ error: "An error occurred while updating entity views.", code: "entity/server-error", details: err.message, }); } };