implement collections page
This commit is contained in:
59
packages/server/src/collection/collection.ts
Normal file
59
packages/server/src/collection/collection.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
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 }
|
Reference in New Issue
Block a user