Added blog functionality, reworked backend api requests
This commit is contained in:
1 parent
a4b69c5b9a
commit
dae95810f2
16 files changed
+696
-171
No files matched your search
@@ -1,21 +1,70 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
interface Article {
|
||||
id: number;
|
||||
title: string;
|
||||
slug: string;
|
||||
content: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
views: number;
|
||||
authorId: number;
|
||||
authorName: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ArticleService {
|
||||
|
||||
private apiUrl = 'http://localhost:7020/api/articles'; // Replace with your API endpoint
|
||||
private apiUrl = '/api/articles';
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
getArticles(token: string): Observable<any[]> {
|
||||
return this.http.get<any[]>(this.apiUrl, {
|
||||
getAllArticles(): Observable<Article[]> {
|
||||
return this.http.get<Article[]>(this.apiUrl);
|
||||
}
|
||||
|
||||
getArticleBySlug(slug: string): Observable<Article |undefined> {
|
||||
return this.http.get<Article[]>(this.apiUrl).pipe(
|
||||
map((articles) => articles.find((article) => article.slug === slug))
|
||||
);
|
||||
}
|
||||
|
||||
getArticleById(id: number): Observable<Article |undefined> {
|
||||
return this.http.get<Article[]>(this.apiUrl).pipe(
|
||||
map((articles) => articles.find((article) => article.id === id))
|
||||
);
|
||||
}
|
||||
|
||||
postArticle(token: string, title: string, content: string ): Observable<any[]> {
|
||||
return this.http.post<any[]>(this.apiUrl, {
|
||||
title,
|
||||
content
|
||||
},{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
deleteArticle(id: number, token: string): Observable<boolean> {
|
||||
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||
return this.http.delete<any>(`${this.apiUrl}/${id}`, { headers }).pipe(
|
||||
map((response) => {
|
||||
// If the backend responds successfully, return true
|
||||
return !!(response && response.message === 'Article deleted');
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
editArticle(id: number, token: string | null, title: string, content: string): Observable<any> {
|
||||
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||
const body = { title, content };
|
||||
|
||||
return this.http.put<any>(`${this.apiUrl}/${id}`, body, { headers });
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import {Observable, tap} from 'rxjs';
|
||||
})
|
||||
export class AuthService {
|
||||
|
||||
private apiUrl = '/api/auth/login'; // Replace with your API endpoint
|
||||
private apiUrl = '/api/auth/login';
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Title } from '@angular/platform-browser';
|
||||
import { Meta } from '@angular/platform-browser';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UpdateMetaService {
|
||||
|
||||
constructor(private titleService: Title, private metaService: Meta) {}
|
||||
|
||||
updateMetaTags(meta: { title?: string; description?: string; image?: string; url?: string; color?: string }) {
|
||||
if (meta.title) {
|
||||
this.titleService.setTitle(meta.title);
|
||||
this.metaService.updateTag({ name: 'og:title', content: meta.title });
|
||||
}
|
||||
if (meta.description) {
|
||||
this.metaService.updateTag({ name: 'description', content: meta.description });
|
||||
this.metaService.updateTag({ name: 'og:description', content: meta.description });
|
||||
}
|
||||
if (meta.image) {
|
||||
this.metaService.updateTag({ name: 'og:image', content: meta.image });
|
||||
}
|
||||
if (meta.url) {
|
||||
this.metaService.updateTag({ name: 'og:url', content: meta.url });
|
||||
}
|
||||
if (meta.color) {
|
||||
this.metaService.updateTag({ name: 'theme-color', content: meta.color });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user