|
1 |
| -import { Injectable } from '@nestjs/common'; |
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { ConfigService } from '@nestjs/config'; |
| 3 | +import { |
| 4 | + CreateQuestionDto, |
| 5 | + QuestionDto, |
| 6 | + UpdateQuestionDto, |
| 7 | +} from '@repo/dtos/questions'; |
| 8 | +import { createClient, SupabaseClient } from '@supabase/supabase-js'; |
2 | 9 |
|
3 | 10 | @Injectable()
|
4 | 11 | export class QuestionsService {
|
5 |
| - private readonly questions = [ |
6 |
| - { id: '1', title: 'What is NestJS?', userId: '1' }, |
7 |
| - // ...other questions |
8 |
| - ]; |
9 |
| - |
10 |
| - findById(id: string) { |
11 |
| - console.log('hello'); |
12 |
| - return this.questions.find((question) => question.id === id); |
| 12 | + private supabase: SupabaseClient; |
| 13 | + private readonly logger = new Logger(QuestionsService.name); |
| 14 | + |
| 15 | + private readonly QUESTIONS_TABLE = 'question_bank'; |
| 16 | + |
| 17 | + constructor(private configService: ConfigService) { |
| 18 | + const supabaseUrl = this.configService.get<string>('SUPABASE_URL'); |
| 19 | + const supabaseKey = this.configService.get<string>('SUPABASE_KEY'); |
| 20 | + |
| 21 | + if (!supabaseUrl || !supabaseKey) { |
| 22 | + throw new Error('Supabase URL and key must be provided'); |
| 23 | + } |
| 24 | + |
| 25 | + this.supabase = createClient(supabaseUrl, supabaseKey); |
| 26 | + } |
| 27 | + |
| 28 | + private handleError(operation: string, error: unknown): never { |
| 29 | + this.logger.error(`Error during ${operation}:`, error); |
| 30 | + throw error; |
| 31 | + } |
| 32 | + |
| 33 | + async findAll(includeDeleted: boolean = false): Promise<QuestionDto[]> { |
| 34 | + const query = this.supabase.from(this.QUESTIONS_TABLE).select(); |
| 35 | + |
| 36 | + if (!includeDeleted) { |
| 37 | + query.is('deleted_at', null); |
| 38 | + } |
| 39 | + |
| 40 | + const { data, error } = await query; |
| 41 | + |
| 42 | + if (error) { |
| 43 | + this.handleError('fetch questions', error); |
| 44 | + } |
| 45 | + |
| 46 | + this.logger.log( |
| 47 | + `fetched ${data.length} questions, includeDeleted: ${includeDeleted}`, |
| 48 | + ); |
| 49 | + return data; |
| 50 | + } |
| 51 | + |
| 52 | + async findById(id: string): Promise<QuestionDto> { |
| 53 | + const { data, error } = await this.supabase |
| 54 | + .from(this.QUESTIONS_TABLE) |
| 55 | + .select() |
| 56 | + .eq('id', id) |
| 57 | + .single(); |
| 58 | + |
| 59 | + if (error) { |
| 60 | + this.handleError('fetch question by id', error); |
| 61 | + } |
| 62 | + |
| 63 | + this.logger.log(`fetched question with id ${id}`); |
| 64 | + return data; |
| 65 | + } |
| 66 | + |
| 67 | + async create(question: CreateQuestionDto): Promise<QuestionDto> { |
| 68 | + const { data, error } = await this.supabase |
| 69 | + .from(this.QUESTIONS_TABLE) |
| 70 | + .insert(question) |
| 71 | + .select() |
| 72 | + .single<QuestionDto>(); |
| 73 | + |
| 74 | + if (error) { |
| 75 | + this.handleError('create question', error); |
| 76 | + } |
| 77 | + |
| 78 | + this.logger.log(`created question ${data.id}`); |
| 79 | + return data; |
| 80 | + } |
| 81 | + |
| 82 | + async update(question: UpdateQuestionDto): Promise<QuestionDto> { |
| 83 | + const updatedQuestion = { |
| 84 | + ...question, |
| 85 | + updated_at: new Date(), |
| 86 | + }; |
| 87 | + |
| 88 | + const { data, error } = await this.supabase |
| 89 | + .from(this.QUESTIONS_TABLE) |
| 90 | + .update(updatedQuestion) |
| 91 | + .eq('id', question.id) |
| 92 | + .select() |
| 93 | + .single(); |
| 94 | + |
| 95 | + if (error) { |
| 96 | + this.handleError('update question', error); |
| 97 | + } |
| 98 | + |
| 99 | + this.logger.log(`updated question with id ${question.id}`); |
| 100 | + return data; |
| 101 | + } |
| 102 | + |
| 103 | + async deleteById(id: string): Promise<QuestionDto> { |
| 104 | + const { data, error } = await this.supabase |
| 105 | + .from(this.QUESTIONS_TABLE) |
| 106 | + .update({ deleted_at: new Date() }) |
| 107 | + .eq('id', id) |
| 108 | + .select() |
| 109 | + .single(); |
| 110 | + |
| 111 | + if (error) { |
| 112 | + this.handleError('delete question', error); |
| 113 | + } |
| 114 | + |
| 115 | + this.logger.log(`deleted question with id ${id}`); |
| 116 | + return data; |
13 | 117 | }
|
14 | 118 | }
|
0 commit comments