|
1 | 1 | import { inject, Injectable } from '@angular/core'; |
2 | 2 | import { Observable } from 'rxjs'; |
3 | | -import { Comment } from '../../shared/_models/comment.inteface'; |
4 | 3 | import { SupabaseService } from '../../services/supabase.service'; |
5 | | -import { Post } from '../../types/supabase'; |
| 4 | +import { Comment, Post } from '../../types/supabase'; |
6 | 5 |
|
7 | 6 | @Injectable({ providedIn: 'root' }) |
8 | 7 | export class ReaderApiService { |
9 | 8 | supabaseService = inject(SupabaseService); |
10 | 9 |
|
11 | 10 | async getPost(id: string): Promise<Post | null> { |
12 | | - return null; |
| 11 | + const { data: post, error } = await this.supabaseService.getClient |
| 12 | + .from('posts') |
| 13 | + .select( |
| 14 | + ` |
| 15 | + *, |
| 16 | + author:profiles ( id, username, avatar_url ), |
| 17 | + post_tags ( |
| 18 | + tags ( id, name, color, icon ) |
| 19 | + ) |
| 20 | + `, |
| 21 | + ) |
| 22 | + .eq('id', id) |
| 23 | + .single(); |
| 24 | + return error ? null : post; |
13 | 25 | } |
14 | 26 |
|
15 | | - getComments(postId: string): Observable<Comment[]> { |
16 | | - return [] as unknown as Observable<Comment[]>; |
| 27 | + async getComments(postId: string): Promise<Comment[]> { |
| 28 | + const { data: comments, error } = await this.supabaseService.getClient |
| 29 | + .from('comments') |
| 30 | + .select('*') |
| 31 | + .eq('post_id', postId) |
| 32 | + .order('created_at', { ascending: true }); |
| 33 | + return error ? [] : comments; |
17 | 34 | } |
18 | 35 |
|
19 | | - addComment(postId: string, comment: Comment): void {} |
| 36 | + async addComment(postId: string, comment: Comment): Promise<void> { |
| 37 | + const { error } = await this.supabaseService.getClient |
| 38 | + .from('comments') |
| 39 | + .insert({ ...comment, post_id: postId }); |
| 40 | + } |
20 | 41 |
|
21 | | - deleteComment(commentId: string, postId: string): void {} |
| 42 | + async deleteComment(commentId: string, postId: string): Promise<void> { |
| 43 | + const { error } = await this.supabaseService.getClient |
| 44 | + .from('comments') |
| 45 | + .delete() |
| 46 | + .eq('id', commentId) |
| 47 | + .eq('post_id', postId); |
| 48 | + } |
22 | 49 |
|
23 | 50 | async getPosts(): Promise<Post[] | null> { |
24 | 51 | const { data: posts } = await this.supabaseService.getClient |
|
0 commit comments