Introduce repository pattern across multiple services, including `favorites`, `tags`, `sessions`, `reports`, `auth`, and more. Decouple crypto functionalities into modular services like `HashingService`, `JwtService`, and `EncryptionService`. Improve testability and maintainability by simplifying dependencies and consolidating utility logic.
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { DatabaseService } from "../../database/database.service";
|
|
import { contents, favorites } from "../../database/schemas";
|
|
|
|
@Injectable()
|
|
export class FavoritesRepository {
|
|
constructor(private readonly databaseService: DatabaseService) {}
|
|
|
|
async findContentById(contentId: string) {
|
|
const result = await this.databaseService.db
|
|
.select()
|
|
.from(contents)
|
|
.where(eq(contents.id, contentId))
|
|
.limit(1);
|
|
return result[0] || null;
|
|
}
|
|
|
|
async add(userId: string, contentId: string) {
|
|
return await this.databaseService.db
|
|
.insert(favorites)
|
|
.values({ userId, contentId })
|
|
.returning();
|
|
}
|
|
|
|
async remove(userId: string, contentId: string) {
|
|
return await this.databaseService.db
|
|
.delete(favorites)
|
|
.where(and(eq(favorites.userId, userId), eq(favorites.contentId, contentId)))
|
|
.returning();
|
|
}
|
|
|
|
async findByUserId(userId: string, limit: number, offset: number) {
|
|
const data = await this.databaseService.db
|
|
.select({
|
|
content: contents,
|
|
})
|
|
.from(favorites)
|
|
.innerJoin(contents, eq(favorites.contentId, contents.id))
|
|
.where(eq(favorites.userId, userId))
|
|
.limit(limit)
|
|
.offset(offset);
|
|
|
|
return data.map((item) => item.content);
|
|
}
|
|
}
|