feat: add FavoritesModule with service, controller, and CRUD endpoints
Implemented FavoritesModule to manage user favorites. Includes service methods for adding, removing, and listing favorites, along with appropriate database integrations and API endpoints.
This commit is contained in:
63
backend/src/favorites/favorites.service.ts
Normal file
63
backend/src/favorites/favorites.service.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { DatabaseService } from "../database/database.service";
|
||||
import { contents, favorites } from "../database/schemas";
|
||||
|
||||
@Injectable()
|
||||
export class FavoritesService {
|
||||
constructor(private readonly databaseService: DatabaseService) {}
|
||||
|
||||
async addFavorite(userId: string, contentId: string) {
|
||||
// Vérifier si le contenu existe
|
||||
const content = await this.databaseService.db
|
||||
.select()
|
||||
.from(contents)
|
||||
.where(eq(contents.id, contentId))
|
||||
.limit(1);
|
||||
|
||||
if (content.length === 0) {
|
||||
throw new NotFoundException("Content not found");
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.databaseService.db
|
||||
.insert(favorites)
|
||||
.values({ userId, contentId })
|
||||
.returning();
|
||||
} catch (_error) {
|
||||
// Probablement une violation de clé primaire (déjà en favori)
|
||||
throw new ConflictException("Content already in favorites");
|
||||
}
|
||||
}
|
||||
|
||||
async removeFavorite(userId: string, contentId: string) {
|
||||
const result = await this.databaseService.db
|
||||
.delete(favorites)
|
||||
.where(and(eq(favorites.userId, userId), eq(favorites.contentId, contentId)))
|
||||
.returning();
|
||||
|
||||
if (result.length === 0) {
|
||||
throw new NotFoundException("Favorite not found");
|
||||
}
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
async getUserFavorites(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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user