Deleted unused e2e tests, mocks (`cuid2`, `jose`, `ml-kem`, `sha3`), and their associated jest configurations. Simplified services by ensuring proper dependency imports, resolving circular references, and improving TypeScript type usage for enhanced maintainability and testability. Upgraded Dockerfile base image to match new development standards.
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
Logger,
|
|
NotFoundException,
|
|
} from "@nestjs/common";
|
|
import { FavoritesRepository } from "./repositories/favorites.repository";
|
|
|
|
@Injectable()
|
|
export class FavoritesService {
|
|
private readonly logger = new Logger(FavoritesService.name);
|
|
|
|
constructor(private readonly favoritesRepository: FavoritesRepository) {}
|
|
|
|
async addFavorite(userId: string, contentId: string) {
|
|
this.logger.log(`Adding favorite: user ${userId}, content ${contentId}`);
|
|
|
|
const content = await this.favoritesRepository.findContentById(contentId);
|
|
if (!content) {
|
|
throw new NotFoundException("Content not found");
|
|
}
|
|
|
|
try {
|
|
return await this.favoritesRepository.add(userId, contentId);
|
|
} catch (_error) {
|
|
throw new ConflictException("Content already in favorites");
|
|
}
|
|
}
|
|
|
|
async removeFavorite(userId: string, contentId: string) {
|
|
this.logger.log(`Removing favorite: user ${userId}, content ${contentId}`);
|
|
const result = await this.favoritesRepository.remove(userId, contentId);
|
|
|
|
if (result.length === 0) {
|
|
throw new NotFoundException("Favorite not found");
|
|
}
|
|
|
|
return result[0];
|
|
}
|
|
|
|
async getUserFavorites(userId: string, limit: number, offset: number) {
|
|
return await this.favoritesRepository.findByUserId(userId, limit, offset);
|
|
}
|
|
}
|