-
-
Articles
-
-
No articles available.
-
-
-
- -
- ID {{ article.id }} - {{ article.title }}
-
-
+
+
+
+
0">
+
+ -
+ ID {{ user.id }} - {{ user.username }}
+
+
+
diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts
index 4b57372..f050a81 100644
--- a/src/app/admin/admin.component.ts
+++ b/src/app/admin/admin.component.ts
@@ -1,11 +1,13 @@
import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {ArticleService} from '../services/article.service';
+import {UserService} from '../services/user.service';
import {NgFor, NgIf, NgStyle} from '@angular/common';
import { Observer} from 'rxjs';
import {FormsModule} from '@angular/forms';
import {DeviceDetectorService} from '../services/device-detector.service';
import {Title} from '@angular/platform-browser';
+import {ModalComponent} from '../shared/modal/modal.component';
@Component({
selector: 'app-admin',
@@ -15,19 +17,36 @@ import {Title} from '@angular/platform-browser';
NgIf,
NgFor,
FormsModule,
- NgStyle
+ NgStyle,
+ ModalComponent
]
})
export class AdminComponent implements OnInit {
articles: any[] = [];
+ users: any[] = [];
noArticles: boolean = false;
+
isMobile: boolean = false;
+
title: string = '';
content: string = '';
+
+ modalTitle: string = '';
+ modalAction: string = '';
+ isModalOpen: boolean = false;
+ isViewingArticles: boolean = true;
+
+ userData = {
+ username: '',
+ password: '',
+ id: '',
+ };
+
token: string | null = localStorage.getItem('token');
constructor(
private articleService: ArticleService,
+ private userService: UserService,
private router: Router,
private deviceService: DeviceDetectorService,
private titleService: Title
@@ -41,7 +60,8 @@ export class AdminComponent implements OnInit {
this.navigateToLogin();
return;
}
- this.loadArticles()
+ this.loadArticles();
+ this.loadArticles();
}
navigateToLogin() {
@@ -77,11 +97,11 @@ export class AdminComponent implements OnInit {
(response) => {
alert('Article created successfully!');
this.loadArticles();
- console.log(response); // You can log the response or take further actions like redirecting
+ console.log(response);
},
(error) => {
alert('Error creating article');
- console.error(error); // Log the error for debugging
+ console.error(error);
}
);
}
@@ -105,7 +125,6 @@ export class AdminComponent implements OnInit {
return;
}
- // Prompt for title and content with placeholders
const newTitle = form.value.editTitle || form.value.editTitleMobile;
const newContent = form.value.editContent || form.value.editContentMobile;
@@ -121,11 +140,13 @@ export class AdminComponent implements OnInit {
},
(error) => {
alert(`Error updating article ${id}: ${error.message}`);
+ console.error('Error:', error)
}
);
},
(error: { message: any; }) => {
alert(`Failed to fetch article with ID ${id}: ${error.message}`);
+ console.error('Error:', error)
}
);
} else {
@@ -149,6 +170,7 @@ export class AdminComponent implements OnInit {
},
(error) => {
alert(`Error deleting article ${articleId}: ${error.message}`);
+ console.error('Error deleting article:', error)
}
);
} else {
@@ -157,7 +179,18 @@ export class AdminComponent implements OnInit {
}
navigateToArticle(slug: string): void {
- this.router.navigate([`/blog/${slug}`]);
+ this.router.navigate([`/blog/${slug}`]).then(
+ (success) => {
+ if (success) {
+ console.log('Navigation to article succeeded');
+ } else {
+ console.warn('Navigation to article failed');
+ }
+ },
+ (error) => {
+ console.error('Navigation error:', error);
+ }
+ );
}
loadArticles(): void {
@@ -176,8 +209,148 @@ export class AdminComponent implements OnInit {
this.articleService.getAllArticles().subscribe(articleObserver);
}
+ loadUsers() {
+ this.userService.getAllUsers(this.token).subscribe((data: any[]) => {
+ this.users = data;
+ });
+ }
+
+ toggleView() {
+ this.isViewingArticles = !this.isViewingArticles;
+ if (!this.isViewingArticles && this.users.length === 0) {
+ this.loadUsers();
+ this.loadArticles();
+ }
+ }
+
logout() {
localStorage.removeItem('token');
this.navigateToLogin();
}
+
+ openModal(action: string) {
+ this.isModalOpen = true;
+ this.modalAction = action;
+
+ switch (action) {
+ case 'create':
+ this.modalTitle = 'Create User';
+ break;
+ case 'edit':
+ this.modalTitle = 'Edit User';
+ break;
+ case 'delete':
+ this.modalTitle = 'Delete User';
+ break;
+ }
+ }
+
+ closeModal() {
+ this.isModalOpen = false;
+ this.userData = {
+ username: '',
+ password: '',
+ id: '',
+ };
+ }
+
+ createUser(form: any): void {
+ if (!this.token) {
+ this.navigateToLogin();
+ return;
+ }
+
+ const username = form.value.createUsername;
+ const password = form.value.createPassword;
+
+ if (!username || !password) {
+ alert('Both username and password are required!');
+ return;
+ }
+
+ this.userService.createUser(this.token, username, password).subscribe(
+ (response) => {
+ alert('User created successfully!');
+ this.loadUsers();
+ console.log(response);
+ },
+ (error) => {
+ alert('Error creating user');
+ console.error(error);
+ }
+ );
+ }
+
+ editUser(form: any): void {
+ if (!this.token) {
+ this.navigateToLogin();
+ return;
+ }
+ const userId = form.value.editUserId;
+
+ if (userId && !isNaN(Number(userId))) {
+ const id = Number(userId);
+
+ this.userService.getUserById(id, this.token).subscribe(
+ (user) => {
+ if (!user) {
+ alert(`User with ID ${id} not found.`);
+ return;
+ }
+
+ const newUsername = form.value.editUsername;
+ const newPassword = form.value.editPassword;
+
+ // Use current values if left blank
+ const updatedUsername = newUsername?.trim();
+ const updatedPassword = newPassword?.trim();
+
+ this.userService.editUser(id, this.token, updatedUsername, updatedPassword).subscribe(
+ (response) => {
+ alert(`User ${id} updated successfully!`);
+ console.log('Updated User:', response);
+ this.loadUsers();
+ },
+ (error) => {
+ alert(`Error updating user ${id}: ${error.message}`);
+ console.error('Error:', error)
+ }
+ );
+ },
+ (error: { message: any; }) => {
+ alert(`Failed to fetch user with ID ${id}: ${error.message}`);
+ console.error('Error:', error)
+ }
+ );
+ } else {
+ alert('Invalid input. Please enter a valid article ID.');
+ }
+ this.closeModal();
+ }
+
+ deleteUser(form: any): void {
+ if (!this.token) {
+ this.navigateToLogin();
+ return;
+ }
+
+ const userId = form.value.deleteUserId;
+
+ if (userId && !isNaN(Number(userId))) {
+ this.userService.deleteUser(userId, this.token).subscribe(
+ () => {
+ alert(`User ${userId} deleted successfully`);
+ this.loadUsers();
+ },
+ (error) => {
+ alert(`Error deleting user ${userId}: ${error.message}`);
+ console.error('Error deleting user:', error)
+ }
+ );
+ } else {
+ alert('Invalid input. Please enter a valid user ID.');
+ console.error('Invalid input:', userId);
+ }
+ this.closeModal();
+ }
}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index ecf59e0..5948380 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -1,9 +1,9 @@
import {Component, OnInit} from '@angular/core';
import {NgIf} from '@angular/common';
import {ActivatedRoute, Router, RouterOutlet} from '@angular/router';
-import {NavbarComponent} from './navbar/navbar.component';
-import {MobileNavbarComponent} from './mobile-navbar/mobile-navbar.component';
-import {FooterComponent} from './footer/footer.component';
+import {NavbarComponent} from './shared/navbar/navbar.component';
+import {MobileNavbarComponent} from './shared/mobile-navbar/mobile-navbar.component';
+import {FooterComponent} from './shared/footer/footer.component';
import {DeviceDetectorService} from './services/device-detector.service';
@Component({
diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts
index ca7c573..986d3fc 100644
--- a/src/app/app.routes.ts
+++ b/src/app/app.routes.ts
@@ -5,7 +5,7 @@ import {BlogComponent} from './blog/blog.component';
import {NotFoundComponent} from './not-found/not-found.component';
import {LoginComponent} from './login/login.component';
import {AdminComponent} from './admin/admin.component';
-import {ArticlePageComponent} from './article-page/article-page.component';
+import {ArticlePageComponent} from './blog/article-page/article-page.component';
export const routes: Routes = [
{ path: '', component: AboutComponent },
diff --git a/src/app/article-page/article-page.component.css b/src/app/blog/article-page/article-page.component.css
similarity index 100%
rename from src/app/article-page/article-page.component.css
rename to src/app/blog/article-page/article-page.component.css
diff --git a/src/app/article-page/article-page.component.html b/src/app/blog/article-page/article-page.component.html
similarity index 100%
rename from src/app/article-page/article-page.component.html
rename to src/app/blog/article-page/article-page.component.html
diff --git a/src/app/article-page/article-page.component.ts b/src/app/blog/article-page/article-page.component.ts
similarity index 92%
rename from src/app/article-page/article-page.component.ts
rename to src/app/blog/article-page/article-page.component.ts
index 6bffbba..774013d 100644
--- a/src/app/article-page/article-page.component.ts
+++ b/src/app/blog/article-page/article-page.component.ts
@@ -1,8 +1,8 @@
import {AfterViewInit, Component, Inject, OnDestroy, OnInit, PLATFORM_ID} from '@angular/core';
-import {DeviceDetectorService} from '../services/device-detector.service'
+import {DeviceDetectorService} from '../../services/device-detector.service'
import {DatePipe, isPlatformBrowser, NgIf} from '@angular/common';
-import {VantaBackgroundService} from '../services/vanta-background.service';
-import {ArticleService} from '../services/article.service';
+import {VantaBackgroundService} from '../../services/vanta-background.service';
+import {ArticleService} from '../../services/article.service';
import {ActivatedRoute} from '@angular/router';
import {Title} from '@angular/platform-browser';
diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts
index aa62558..f4d93b1 100644
--- a/src/app/login/login.component.ts
+++ b/src/app/login/login.component.ts
@@ -59,8 +59,14 @@ export class LoginComponent implements OnInit, OnDestroy, AfterViewInit {
}
);
},
- error: () => {
- this.errorMessage = 'Invalid credentials';
+ error: (err: any) => {
+ // Check if the error response contains a message
+ if (err.error && err.error.message) {
+ this.errorMessage = err.error.message;
+ } else {
+ this.errorMessage = 'An error occurred. Please try again later.';
+ }
+ console.error('Login error:', err);
},
};
diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts
new file mode 100644
index 0000000..cf253ef
--- /dev/null
+++ b/src/app/services/user.service.ts
@@ -0,0 +1,64 @@
+import { Injectable } from '@angular/core';
+import {HttpClient, HttpHeaders} from '@angular/common/http';
+import { Observable } from 'rxjs';
+import {map} from 'rxjs/operators';
+
+interface Username {
+ id: number;
+ username: string;
+ password: string;
+ createdAt: string;
+ updatedAt: string;
+}
+@Injectable({
+ providedIn: 'root'
+})
+export class UserService {
+ private apiUrl = '/api/users';
+
+ constructor(private http: HttpClient) {}
+
+ getAllUsers(token: string | null): Observable
{
+ const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
+ return this.http.get(`${this.apiUrl}/?timestamp=${new Date().getTime()}`, { headers });
+ }
+
+ createUser(token: string, username: string, password: string): Observable {
+ const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
+ return this.http.post(this.apiUrl, {
+ username,
+ password
+ }, { headers });
+ }
+
+ editUser(id: number, token: string | null, username: string, password: string): Observable {
+ const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
+
+ const body: any = {};
+ if (username) {
+ body.username = username
+ }
+ if (password) {
+ body.password = password;
+ }
+
+ return this.http.put(`${this.apiUrl}/${id}`, body, { headers });
+ }
+
+ deleteUser(id: number, token: string): Observable {
+ const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
+ return this.http.delete(`${this.apiUrl}/${id}`, { headers }).pipe(
+ map((response) => {
+ // If the backend responds successfully, return true
+ return !!(response && response.message === 'User deleted');
+ })
+ );
+ }
+
+ getUserById(id: number, token: string): Observable {
+ const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
+ return this.http.get(`${this.apiUrl}/${id}`, { headers }).pipe(
+ map(user => user)
+ );
+ }
+}
diff --git a/src/app/footer/footer.component.css b/src/app/shared/footer/footer.component.css
similarity index 100%
rename from src/app/footer/footer.component.css
rename to src/app/shared/footer/footer.component.css
diff --git a/src/app/footer/footer.component.html b/src/app/shared/footer/footer.component.html
similarity index 100%
rename from src/app/footer/footer.component.html
rename to src/app/shared/footer/footer.component.html
diff --git a/src/app/footer/footer.component.ts b/src/app/shared/footer/footer.component.ts
similarity index 100%
rename from src/app/footer/footer.component.ts
rename to src/app/shared/footer/footer.component.ts
diff --git a/src/app/mobile-navbar/mobile-navbar.component.css b/src/app/shared/mobile-navbar/mobile-navbar.component.css
similarity index 100%
rename from src/app/mobile-navbar/mobile-navbar.component.css
rename to src/app/shared/mobile-navbar/mobile-navbar.component.css
diff --git a/src/app/mobile-navbar/mobile-navbar.component.html b/src/app/shared/mobile-navbar/mobile-navbar.component.html
similarity index 100%
rename from src/app/mobile-navbar/mobile-navbar.component.html
rename to src/app/shared/mobile-navbar/mobile-navbar.component.html
diff --git a/src/app/mobile-navbar/mobile-navbar.component.ts b/src/app/shared/mobile-navbar/mobile-navbar.component.ts
similarity index 100%
rename from src/app/mobile-navbar/mobile-navbar.component.ts
rename to src/app/shared/mobile-navbar/mobile-navbar.component.ts
diff --git a/src/app/shared/modal/modal.component.css b/src/app/shared/modal/modal.component.css
new file mode 100644
index 0000000..a2d747a
--- /dev/null
+++ b/src/app/shared/modal/modal.component.css
@@ -0,0 +1,31 @@
+.modal-backdrop {
+ position: fixed;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
+ background: rgba(0, 0, 0, 0.5);
+ display: flex;
+ justify-content: center;
+ align-items: center;
+}
+
+.modal {
+ background: #fff;
+ padding: 20px;
+ border-radius: 25px;
+ min-width: 300px;
+ max-width: 300px;
+ position: relative;
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
+}
+
+.close-btn {
+ position: absolute;
+ top: 10px;
+ right: 10px;
+ background: none;
+ border: none;
+ font-size: 20px;
+ cursor: pointer;
+}
diff --git a/src/app/shared/modal/modal.component.html b/src/app/shared/modal/modal.component.html
new file mode 100644
index 0000000..cbf4872
--- /dev/null
+++ b/src/app/shared/modal/modal.component.html
@@ -0,0 +1,7 @@
+
+
+
+
{{ title }}
+
+
+
diff --git a/src/app/shared/modal/modal.component.ts b/src/app/shared/modal/modal.component.ts
new file mode 100644
index 0000000..4083ca7
--- /dev/null
+++ b/src/app/shared/modal/modal.component.ts
@@ -0,0 +1,21 @@
+import { Component, Input, Output, EventEmitter } from '@angular/core';
+import {NgIf} from '@angular/common';
+
+@Component({
+ selector: 'app-modal',
+ templateUrl: './modal.component.html',
+ styleUrls: ['./modal.component.css'],
+ imports: [
+ NgIf
+ ]
+})
+export class ModalComponent {
+ @Input() isOpen: boolean = false;
+ @Input() title: string = '';
+ @Output() onClose = new EventEmitter();
+
+ closeModal() {
+ this.isOpen = false;
+ this.onClose.emit();
+ }
+}
diff --git a/src/app/navbar/navbar.component.css b/src/app/shared/navbar/navbar.component.css
similarity index 100%
rename from src/app/navbar/navbar.component.css
rename to src/app/shared/navbar/navbar.component.css
diff --git a/src/app/navbar/navbar.component.html b/src/app/shared/navbar/navbar.component.html
similarity index 100%
rename from src/app/navbar/navbar.component.html
rename to src/app/shared/navbar/navbar.component.html
diff --git a/src/app/navbar/navbar.component.ts b/src/app/shared/navbar/navbar.component.ts
similarity index 100%
rename from src/app/navbar/navbar.component.ts
rename to src/app/shared/navbar/navbar.component.ts