Skip to content

Commit db5cf48

Browse files
changed to rest api wip
1 parent 79cf5cd commit db5cf48

File tree

6 files changed

+52
-56
lines changed

6 files changed

+52
-56
lines changed

src/app/admin/_services/admin-api.service.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
import { Injectable, inject } from '@angular/core';
1+
import { inject, Injectable } from '@angular/core';
22

3-
import { Observable } from 'rxjs';
3+
import { map, Observable } from 'rxjs';
44
import { Post } from '../../supabase-types';
5+
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
56

67
@Injectable()
78
export class AdminApiService {
9+
http = inject(HttpClient);
10+
private readonly baseUrl =
11+
'https://aqdbdmepncxxuanlymwr.supabase.co/rest/v1/';
12+
private readonly apiKey =
13+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFxZGJkbWVwbmN4eHVhbmx5bXdyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDUwNTA0MjYsImV4cCI6MjA2MDYyNjQyNn0.RNtZZ4Of4LIP3XuS9lumHYdjRLVUGXARtAxaTJmF7lc';
14+
815
addPost(post: Post): void {}
916

10-
getPostById(id: string): Observable<Post | null> {
11-
return null as unknown as Observable<Post | null>;
17+
getPostById(id: string): Observable<Post> {
18+
const selectQuery = `
19+
*,
20+
author:profiles(id,username,avatar_url),
21+
post_tags!inner(tags(id,name,color,icon)),
22+
comments(id,content,created_at,is_deleted,is_reported,author:profiles(id,username,avatar_url))
23+
`
24+
.replace(/\s+/g, ' ')
25+
.trim();
26+
27+
const params = new HttpParams()
28+
.set('select', selectQuery)
29+
.set('id', `eq.${id}`);
30+
31+
const headers = new HttpHeaders({
32+
apikey: this.apiKey,
33+
Authorization: `Bearer ${this.apiKey}`,
34+
Accept: 'application/json',
35+
});
36+
37+
return this.http
38+
.get<Post[]>(`${this.baseUrl}posts`, { headers, params })
39+
.pipe(map((results) => results[0] ?? null));
1240
}
1341

1442
updatePost(id: string, post: Post): Promise<void> {

src/app/admin/_services/api.service.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/app/reader/_components/main-page/post/post.component.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</div>
1010
}
1111

12-
13-
1412
<div class="container mx-auto grid grid-cols-12 gap-8">
1513
<aside
1614
class="col-span-12 2xl:col-span-3 bg-white/0 backdrop-blur-lg border border-white/90 rounded-lg shadow-lg p-6 self-start -mt-16 mb-16 2xl:my-36"
@@ -19,7 +17,7 @@ <h2 class="text-xl font-bold mb-4">📌 Article Information</h2>
1917
<ul class="text-gray-700 bg-gray-50 space-y-2 p-4 rounded-2xl shadow-lg">
2018
<li><strong>Category:</strong> {{ post()?.category }}</li>
2119
<li><strong>Updated:</strong> {{ post()?.updated | date }}</li>
22-
<li><strong>Author:</strong> {{ post()?.author }}</li>
20+
<li><strong>Author:</strong> {{ post()?.author?.username }}</li>
2321
<li><strong>Reading Time:</strong> {{ post()?.readingTime }} min</li>
2422
<li><strong>Difficulty:</strong> ⭐⭐⭐☆</li>
2523
</ul>
@@ -49,7 +47,7 @@ <h2 class="text-xl font-bold mt-6 mb-4">📖 Table of Contents</h2>
4947
<p
5048
class="text-center [webkit-text-stroke:1px_white] [text-shadow:_2px_2px_4px_rgba(255,255,255,0.8)] text-gray-500"
5149
>
52-
{{ post()?.author }} - {{ post()?.created_at | date }}
50+
{{ post()?.author?.username }} - {{ post()?.created_at | date }}
5351
</p>
5452
<!-- TODO: Add social share buttons -->
5553

src/app/reader/_components/main-page/posts-list/posts-list.component.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DatePipe, NgOptimizedImage, NgStyle } from '@angular/common';
1+
import { NgOptimizedImage, NgStyle } from '@angular/common';
22
import {
33
CUSTOM_ELEMENTS_SCHEMA,
44
ChangeDetectionStrategy,
@@ -9,16 +9,12 @@ import {
99
ElementRef,
1010
signal,
1111
WritableSignal,
12-
OnInit,
13-
ChangeDetectorRef,
1412
} from '@angular/core';
1513
import { RouterModule } from '@angular/router';
1614
import { AboutMeComponent } from '../../../../shared/about-me/about-me.component';
1715
import { PostCardComponent } from './post-card/post-card.component';
1816
import { PostsStore } from './posts.store';
1917
import { TagsStore } from '../../../../shared/stores/tags.store';
20-
import { Post } from '../../../../types/supabase';
21-
import { ReaderApiService } from '../../../_services/reader-api.service';
2218

2319
@Component({
2420
selector: 'app-posts-list',
@@ -30,18 +26,16 @@ import { ReaderApiService } from '../../../_services/reader-api.service';
3026
NgOptimizedImage,
3127
NgStyle,
3228
],
33-
providers: [DatePipe],
3429
templateUrl: './posts-list.component.html',
3530
styleUrl: './posts-list.component.scss',
3631
changeDetection: ChangeDetectionStrategy.Default,
3732
schemas: [CUSTOM_ELEMENTS_SCHEMA],
3833
})
39-
export class PostsListComponent implements OnInit {
34+
export class PostsListComponent {
4035
scroll = viewChild<ElementRef<HTMLElement>>('scrollContainer');
4136

4237
postStore = inject(PostsStore);
4338
tagsStore = inject(TagsStore);
44-
cdr = inject(ChangeDetectorRef);
4539

4640
scrollProgress: WritableSignal<number> = signal(0);
4741
posts = this.postStore.posts;
@@ -52,10 +46,6 @@ export class PostsListComponent implements OnInit {
5246
this.initializeScrollingForMobileView();
5347
}
5448

55-
ngOnInit() {
56-
console.log(this.posts());
57-
}
58-
5949
onScroll(event: Event) {
6050
const target = event.target as HTMLElement;
6151
const scrollLeft = target.scrollLeft;

src/app/reader/_services/reader-api.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { inject, Injectable } from '@angular/core';
22
import { SupabaseService } from '../../services/supabase.service';
33
import { Comment, Post, Profile, Tag, PostTag } from '../../types/supabase';
44
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
5-
import { map, Observable } from 'rxjs';
5+
import { map, Observable, tap } from 'rxjs';
66

77
@Injectable({ providedIn: 'root' })
88
export class ReaderApiService {
99
supabaseService = inject(SupabaseService);
1010
http = inject(HttpClient);
1111
private readonly baseUrl =
12-
'https://aqdbdmepncxxuanlymwr.supabase.co/rest/v1/posts';
12+
'https://aqdbdmepncxxuanlymwr.supabase.co/rest/v1/';
1313
private readonly apiKey =
1414
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFxZGJkbWVwbmN4eHVhbmx5bXdyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDUwNTA0MjYsImV4cCI6MjA2MDYyNjQyNn0.RNtZZ4Of4LIP3XuS9lumHYdjRLVUGXARtAxaTJmF7lc';
1515

@@ -34,7 +34,7 @@ export class ReaderApiService {
3434
});
3535

3636
return this.http
37-
.get<Post[]>(this.baseUrl, { headers, params })
37+
.get<Post[]>(`${this.baseUrl}posts`, { headers, params })
3838
.pipe(map((results) => results[0] ?? null));
3939
}
4040

@@ -81,7 +81,7 @@ export class ReaderApiService {
8181
Accept: 'application/json',
8282
});
8383

84-
return this.http.get<Post[]>(this.baseUrl, { headers, params });
84+
return this.http.get<Post[]>(`${this.baseUrl}posts`, { headers, params });
8585
}
8686

8787
async getProfiles(): Promise<Profile[] | null> {
@@ -97,7 +97,7 @@ export class ReaderApiService {
9797
Authorization: `Bearer ${this.apiKey}`,
9898
Accept: 'application/json',
9999
});
100-
return this.http.get<Tag[]>(this.baseUrl, { headers });
100+
return this.http.get<Tag[]>(`${this.baseUrl}tags`, { headers });
101101
}
102102

103103
async getPostTags(postId: string): Promise<PostTag[] | null> {

src/app/shared/stores/tags.store.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
withState,
77
withMethods,
88
withHooks,
9-
withComputed
109
} from '@ngrx/signals';
1110
import { rxMethod } from '@ngrx/signals/rxjs-interop';
1211
import { pipe, switchMap, tap } from 'rxjs';
@@ -23,7 +22,7 @@ type TagsState = {
2322
const initialState: TagsState = {
2423
tags: [],
2524
loading: false,
26-
error: null
25+
error: null,
2726
};
2827

2928
export const TagsStore = signalStore(
@@ -37,23 +36,22 @@ export const TagsStore = signalStore(
3736
switchMap(() =>
3837
api.getTags().pipe(
3938
tapResponse({
40-
next: (tags) =>
41-
patchState(store, { tags, loading: false }),
39+
next: (tags) => patchState(store, { tags, loading: false }),
4240
error: (err) =>
4341
patchState(store, {
4442
error: 'Failed to fetch tags',
45-
loading: false
46-
})
47-
})
48-
)
49-
)
50-
)
51-
)
43+
loading: false,
44+
}),
45+
}),
46+
),
47+
),
48+
),
49+
),
5250
})),
5351

5452
withHooks(({ loadTags }) => ({
5553
onInit() {
5654
loadTags();
57-
}
58-
}))
55+
},
56+
})),
5957
);

0 commit comments

Comments
 (0)