Added blog functionality, reworked backend api requests

This commit is contained in:
itzMiney committed 2024-12-29 23:32:35 +01:00
1 parent a4b69c5b9a
commit dae95810f2
16 files changed
+696 -171

No files matched your search

+131 -53
View File
@@ -3,6 +3,7 @@ import {Router} from '@angular/router';
import {ArticleService} from '../services/article.service';
import {NgFor, NgIf} from '@angular/common';
import {Observer} from 'rxjs';
import {FormsModule} from '@angular/forms';
@Component({
selector: 'app-admin',
@@ -10,72 +11,33 @@ import {Observer} from 'rxjs';
styleUrls: ['./admin.component.css'],
imports: [
NgIf,
NgFor
NgFor,
FormsModule,
]
})
export class AdminComponent implements OnInit {
articles: any[] = [];
noArticles: boolean = false;
constructor(private articleService: ArticleService, private router: Router) {}
title: string = '';
content: string = '';
token: string | null = localStorage.getItem('token');
ngOnInit() {
this.fetchArticles();
}
constructor(
private articleService: ArticleService,
private router: Router
) {}
// Fetch articles from backend
fetchArticles() {
const token = localStorage.getItem('token');
if (!token) {
this.router.navigate(['/login']).then(
(success) => {
if (success) {
console.log('Navigation to login succeeded');
} else {
console.warn('Navigation to login failed');
}
},
(error) => {
console.error('Navigation error:', error);
}
);
ngOnInit() {
if (!this.token) {
this.navigateToLogin();
return;
}
const articleObserver: Observer<any> = {
next: (data) => {
this.articles = data;
this.noArticles = this.articles.length === 0;
},
error: (error) => {
console.error('Failed to load articles', error);
},
complete: () => {
console.log('Article loading complete');
}
};
this.articleService.getArticles(token).subscribe(articleObserver);
this.loadArticles()
}
createArticle() {
alert('Create article functionality goes here');
}
editArticle() {
alert('Delete article functionality goes here');
}
deleteArticle() {
alert('Delete article functionality goes here');
}
viewArticles() {
this.fetchArticles();
}
logout() {
localStorage.removeItem('token');
navigateToLogin() {
this.router.navigate(['/login']).then(
(success) => {
if (success) {
@@ -89,4 +51,120 @@ export class AdminComponent implements OnInit {
}
);
}
createArticle() {
if (!this.token) {
this.navigateToLogin();
return;
}
if (!this.title || !this.content) {
alert('Both title and content are required!');
return;
}
this.articleService.postArticle(this.token, this.title, this.content).subscribe(
(response) => {
alert('Article created successfully!');
this.loadArticles();
console.log(response); // You can log the response or take further actions like redirecting
},
(error) => {
alert('Error creating article');
console.error(error); // Log the error for debugging
}
);
}
editArticle(): void {
if (!this.token) {
this.navigateToLogin();
return;
}
const articleId = prompt('Enter the article ID to edit:');
if (articleId && !isNaN(Number(articleId))) {
const id = Number(articleId);
// Fetch the current article to get existing values
this.articleService.getArticleById(id).subscribe(
(article) => {
if (!article) {
alert(`Article with ID ${id} not found.`);
return;
}
// Prompt for title and content with placeholders
const newTitle = prompt('Enter the new title (Leave blank to leave as is):');
const newContent = prompt('Enter the new content (Leave blank to leave as is):');
// Use current values if left blank
const updatedTitle = newTitle?.trim() || article.title;
const updatedContent = newContent?.trim() || article.content;
this.articleService.editArticle(id, this.token, updatedTitle, updatedContent).subscribe(
(response) => {
alert(`Article ${id} updated successfully!`);
console.log('Updated Article:', response);
this.loadArticles();
},
(error) => {
alert(`Error updating article ${id}: ${error.message}`);
}
);
},
(error: { message: any; }) => {
alert(`Failed to fetch article with ID ${id}: ${error.message}`);
}
);
} else {
alert('Invalid input. Please enter a valid article ID.');
}
}
deleteArticle(): void {
if (!this.token) {
this.navigateToLogin();
return;
}
const articleId = prompt('Enter the article ID to delete:');
if (articleId && !isNaN(Number(articleId))) {
const id = Number(articleId);
this.articleService.deleteArticle(id, this.token).subscribe(
() => {
alert(`Article ${id} deleted successfully`);
this.loadArticles();
},
(error) => {
alert(`Error deleting article ${id}: ${error.message}`);
}
);
} else {
alert('Invalid input. Please enter a valid article ID.');
}
}
loadArticles(): void {
const articleObserver: Observer<any> = {
next: (data) => {
this.articles = data;
this.noArticles = this.articles.length === 0;
},
error: (error) => {
console.error('Failed to load articles', error);
},
complete: () => {
console.log('Article loading complete');
}
};
this.articleService.getAllArticles().subscribe(articleObserver);
}
logout() {
localStorage.removeItem('token');
this.navigateToLogin();
}
}