11import { inject , Injectable } from '@angular/core' ;
22import { SupabaseService } from '../../services/supabase.service' ;
33import { Comment , Post , Profile , Tag , PostTag } from '../../types/supabase' ;
4+ import { HttpClient , HttpHeaders , HttpParams } from '@angular/common/http' ;
5+ import { map , Observable } from 'rxjs' ;
46
57@Injectable ( { providedIn : 'root' } )
68export class ReaderApiService {
79 supabaseService = inject ( SupabaseService ) ;
10+ http = inject ( HttpClient ) ;
11+ private readonly baseUrl =
12+ 'https://aqdbdmepncxxuanlymwr.supabase.co/rest/v1/posts' ;
13+ private readonly apiKey =
14+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFxZGJkbWVwbmN4eHVhbmx5bXdyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDUwNTA0MjYsImV4cCI6MjA2MDYyNjQyNn0.RNtZZ4Of4LIP3XuS9lumHYdjRLVUGXARtAxaTJmF7lc' ;
815
9- async getPost ( id : string ) : Promise < any | null > {
10- const { data, error } = await this . supabaseService . getClient
11- . from ( 'posts' )
12- . select (
13- `
16+ getPost ( id : string ) : Observable < Post > {
17+ const selectQuery = `
1418 *,
15- author:profiles ( id, username, avatar_url ),
16- tags:post_tags!inner (
17- tags ( id, name, color, icon )
18- ),
19- comments:comments (
20- id, content, created_at, is_deleted, is_reported,
21- author:profiles ( id, username, avatar_url )
22- )
23- ` ,
24- )
25- . eq ( 'id' , id )
26- . single ( ) ;
27-
28- return error ? null : data ;
19+ author:profiles(id,username,avatar_url),
20+ post_tags!inner(tags(id,name,color,icon)),
21+ comments(id,content,created_at,is_deleted,is_reported,author:profiles(id,username,avatar_url))
22+ `
23+ . replace ( / \s + / g, ' ' )
24+ . trim ( ) ;
25+
26+ const params = new HttpParams ( )
27+ . set ( 'select' , selectQuery )
28+ . set ( 'id' , `eq.${ id } ` ) ;
29+
30+ const headers = new HttpHeaders ( {
31+ apikey : this . apiKey ,
32+ Authorization : `Bearer ${ this . apiKey } ` ,
33+ Accept : 'application/json' ,
34+ } ) ;
35+
36+ return this . http
37+ . get < Post [ ] > ( this . baseUrl , { headers, params } )
38+ . pipe ( map ( ( results ) => results [ 0 ] ?? null ) ) ;
2939 }
3040
3141 async getComments ( postId : string ) : Promise < Comment [ ] > {
@@ -51,24 +61,47 @@ export class ReaderApiService {
5161 . eq ( 'post_id' , postId ) ;
5262 }
5363
54- async getPosts ( ) : Promise < Post [ ] | null > {
55- const { data : posts } = await this . supabaseService . getClient
56- . from ( 'posts' )
57- . select (
58- `
59- *,
60- author:profiles ( id, username, avatar_url ),
61- post_tags (
62- tags ( id, name, color, icon )
63- )
64- ` ,
65- )
66- . eq ( 'is_draft' , false )
67- . order ( 'created_at' , { ascending : false } ) ;
68-
69- return posts ;
64+ getPosts ( ) : Observable < Post [ ] | null > {
65+ const selectQuery = `
66+ *,
67+ author:profiles(id,username,avatar_url),
68+ post_tags(tags(id,name,color,icon))
69+ `
70+ . replace ( / \s + / g, ' ' )
71+ . trim ( ) ;
72+
73+ const params = new HttpParams ( )
74+ . set ( 'select' , selectQuery )
75+ . set ( 'is_draft' , 'eq.false' )
76+ . set ( 'order' , 'created_at.desc' ) ;
77+
78+ const headers = new HttpHeaders ( {
79+ apikey : this . apiKey ,
80+ Authorization : `Bearer ${ this . apiKey } ` ,
81+ Accept : 'application/json' ,
82+ } ) ;
83+
84+ return this . http . get < Post [ ] > ( this . baseUrl , { headers, params } ) ;
7085 }
7186
87+ // async getPosts(): Promise<Post[] | null> {
88+ // const { data: posts } = await this.supabaseService.getClient
89+ // .from('posts')
90+ // .select(
91+ // `
92+ // *,
93+ // author:profiles ( id, username, avatar_url ),
94+ // post_tags (
95+ // tags ( id, name, color, icon )
96+ // )
97+ // `,
98+ // )
99+ // .eq('is_draft', false)
100+ // .order('created_at', { ascending: false });
101+ //
102+ // return posts;
103+ // }
104+
72105 async getProfiles ( ) : Promise < Profile [ ] | null > {
73106 const { data : profiles , error } = await this . supabaseService . getClient
74107 . from ( 'profiles' )
0 commit comments