ekoetki/database/catalog.js

84 lines
1.6 KiB
JavaScript

const crypto = require("node:crypto");
const DB = require("./connection");
exports.createCatalog = createCatalog;
exports.getCatalogs = getCatalogs;
exports.countCatalogs = countCatalogs;
exports.updateCatalog = updateCatalog;
exports.deleteCatalogs = deleteCatalogs;
async function createCatalog(
owner_id,
name,
score,
description
)
{
const [id] = await DB.table("events")
.insert({
owner_id,
name,
score,
description
})
.returning("id");
return id;
}
async function updateCatalog(
id,
owner_id,
name,
score,
description
)
{
await DB.table("events")
.where("id",id)
.where("owner_id", owner_id)
.update({
name,
score,
description
});
}
async function getCatalogs(
owner_id,
offset,
limit,
searchTerm
)
{
return await DB.table("events")
.where("owner_id",owner_id)
.whereNull("deleted_at")
.where("name","like",`%${searchTerm ?? ""}%`)
.orderBy("score","desc")
.limit(limit)
.offset(offset);
}
async function countCatalogs(owner_id, searchTerm)
{
return (
await DB.table("events")
.where("owner_id",owner_id)
.whereNull("deleted_at")
.where("name","like",`%${searchTerm ?? ""}%`)
.count("id as total")
.first()
).total;
}
async function deleteCatalogs(owner_id, id)
{
await DB.table("events")
.where("owner_id",owner_id)
.where("id",id)
.update({
deleted_at: DB.fn.now()
});
}