2024-11-08 21:57:13 +00:00
|
|
|
import type {db} from '$lib/server/api/packages/drizzle'
|
|
|
|
|
import {generateRandomAnimalName} from '$lib/utils/randomDataUtil'
|
|
|
|
|
import {inject, injectable} from 'tsyringe'
|
|
|
|
|
import {CollectionsRepository} from '../repositories/collections.repository'
|
2024-08-10 17:10:57 +00:00
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
|
export class CollectionsService {
|
2024-09-18 00:32:26 +00:00
|
|
|
constructor(@inject(CollectionsRepository) private readonly collectionsRepository: CollectionsRepository) {}
|
2024-08-10 17:10:57 +00:00
|
|
|
|
2024-08-23 02:26:22 +00:00
|
|
|
async findOneByUserId(userId: string) {
|
2024-09-18 00:32:26 +00:00
|
|
|
return this.collectionsRepository.findOneByUserId(userId)
|
2024-08-23 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAllByUserId(userId: string) {
|
2024-09-18 00:32:26 +00:00
|
|
|
return this.collectionsRepository.findAllByUserId(userId)
|
2024-08-23 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:07:22 +00:00
|
|
|
async findAllByUserIdWithDetails(userId: string) {
|
2024-09-18 00:32:26 +00:00
|
|
|
return this.collectionsRepository.findAllByUserIdWithDetails(userId)
|
2024-09-16 16:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-23 02:26:22 +00:00
|
|
|
async findOneById(id: string) {
|
2024-09-18 00:32:26 +00:00
|
|
|
return this.collectionsRepository.findOneById(id)
|
2024-08-23 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneByCuid(cuid: string) {
|
2024-09-18 00:32:26 +00:00
|
|
|
return this.collectionsRepository.findOneByCuid(cuid)
|
2024-08-23 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 00:32:26 +00:00
|
|
|
async createEmptyNoName(userId: string, trx: Parameters<Parameters<typeof db.transaction>[0]>[0] | null = null) {
|
|
|
|
|
return this.createEmpty(userId, null, trx)
|
2024-08-13 22:19:57 +00:00
|
|
|
}
|
2024-08-10 17:10:57 +00:00
|
|
|
|
2024-09-18 00:32:26 +00:00
|
|
|
async createEmpty(userId: string, name: string | null, trx: Parameters<Parameters<typeof db.transaction>[0]>[0] | null = null) {
|
|
|
|
|
if (!trx) {
|
|
|
|
|
return this.collectionsRepository.create({
|
|
|
|
|
user_id: userId,
|
|
|
|
|
name: name ?? generateRandomAnimalName(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.collectionsRepository.create(
|
|
|
|
|
{
|
|
|
|
|
user_id: userId,
|
|
|
|
|
name: name ?? generateRandomAnimalName(),
|
|
|
|
|
},
|
|
|
|
|
trx,
|
|
|
|
|
)
|
2024-08-13 22:19:57 +00:00
|
|
|
}
|
2024-09-18 00:32:26 +00:00
|
|
|
}
|