2024-08-10 17:10:57 +00:00
|
|
|
import {inject, injectable} from "tsyringe";
|
2024-08-13 22:19:57 +00:00
|
|
|
import { eq, type InferInsertModel } from "drizzle-orm";
|
2024-08-10 17:10:57 +00:00
|
|
|
import {DatabaseProvider} from "$lib/server/api/providers";
|
2024-08-13 22:19:57 +00:00
|
|
|
import { collections } from "../infrastructure/database/tables";
|
|
|
|
|
import { takeFirstOrThrow } from "../infrastructure/database/utils";
|
|
|
|
|
|
|
|
|
|
export type CreateCollection = InferInsertModel<typeof collections>;
|
|
|
|
|
export type UpdateCollection = Partial<CreateCollection>;
|
2024-08-10 17:10:57 +00:00
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
|
export class CollectionsRepository {
|
|
|
|
|
constructor(@inject(DatabaseProvider) private readonly db: DatabaseProvider) { }
|
|
|
|
|
|
|
|
|
|
async findAll() {
|
2024-08-13 22:19:57 +00:00
|
|
|
return this.db.query.collections.findMany();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneById(id: string) {
|
|
|
|
|
return this.db.query.collections.findFirst({
|
|
|
|
|
where: eq(collections.id, id)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneByUserId(userId: string) {
|
|
|
|
|
return this.db.query.collections.findFirst({
|
|
|
|
|
where: eq(collections.user_id, userId)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(data: CreateCollection) {
|
|
|
|
|
return this.db.insert(collections).values(data).returning().then(takeFirstOrThrow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(id: string, data: UpdateCollection) {
|
|
|
|
|
return this.db
|
|
|
|
|
.update(collections)
|
|
|
|
|
.set(data)
|
|
|
|
|
.where(eq(collections.id, id))
|
|
|
|
|
.returning()
|
|
|
|
|
.then(takeFirstOrThrow);
|
2024-08-10 17:10:57 +00:00
|
|
|
}
|
|
|
|
|
}
|