60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import type { User } from "@markone/core"
|
|
import type { Collection } from "@markone/core"
|
|
import { db } from "~/database.js"
|
|
|
|
function findCollections(user: User): Collection[] {
|
|
const collectionsQuery = db.query<Collection, { userId: string }>(`
|
|
SELECT id, name, description
|
|
FROM collections
|
|
WHERE user_id = $userId
|
|
`)
|
|
return collectionsQuery.all({ userId: user.id })
|
|
}
|
|
|
|
function insertCollection(collection: Collection, user: User): void {
|
|
const query = db.query(`
|
|
INSERT INTO collections (id, user_id, name, description)
|
|
VALUES ($id, $userId, $name, $description)
|
|
`)
|
|
|
|
query.run({
|
|
id: collection.id,
|
|
userId: user.id,
|
|
name: collection.name,
|
|
description: collection.description,
|
|
})
|
|
}
|
|
|
|
function updateCollection(collection: Collection, user: User): void {
|
|
db.query(`
|
|
UPDATE collections
|
|
SET name = $name, description = $description
|
|
WHERE id = $id AND user_id = $userId
|
|
`).run({
|
|
id: collection.id,
|
|
userId: user.id,
|
|
name: collection.name,
|
|
description: collection.description,
|
|
})
|
|
}
|
|
|
|
function deleteCollection(collectionId: string, user: User): void {
|
|
db.query(`
|
|
DELETE FROM collections
|
|
WHERE id = $id AND user_id = $userId
|
|
`).run({
|
|
id: collectionId,
|
|
userId: user.id,
|
|
})
|
|
}
|
|
|
|
function findCollectionById(collectionId: string, user: User): Collection | null {
|
|
return db.query<Collection, { id: string; userId: string }>(`
|
|
SELECT *
|
|
FROM collections
|
|
WHERE id = $id AND user_id = $userId
|
|
`).get({ id: collectionId, userId: user.id })
|
|
}
|
|
|
|
export { findCollections, insertCollection, updateCollection, deleteCollection, findCollectionById }
|