From c4f5d492b2bd9d41c4c423a3240384c591fdf4f6 Mon Sep 17 00:00:00 2001 From: angelinasu57 Date: Tue, 25 Apr 2023 13:11:45 -0400 Subject: [PATCH] Create deleted post functions, adjusted admin tab to call deleted posts. --- frontend/src/app/admin/admin.component.html | 14 ++++++++++++- frontend/src/app/admin/admin.component.ts | 13 +++++++++++- frontend/src/app/post.service.ts | 20 +++++++++++++++++-- .../src/app/viewforum/viewforum.component.ts | 8 ++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/admin/admin.component.html b/frontend/src/app/admin/admin.component.html index 7056472..5501441 100644 --- a/frontend/src/app/admin/admin.component.html +++ b/frontend/src/app/admin/admin.component.html @@ -8,4 +8,16 @@ - \ No newline at end of file + +
+

+ List of Deleted Posts +

+
+
{{ post.user.first_name}} {{ post.user.last_name}} {{ post.timestamp }}
+

{{ post.title }}

+

{{ post.content }}

+
+ +
+
\ No newline at end of file diff --git a/frontend/src/app/admin/admin.component.ts b/frontend/src/app/admin/admin.component.ts index 3a28fe3..bc70656 100644 --- a/frontend/src/app/admin/admin.component.ts +++ b/frontend/src/app/admin/admin.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs'; import { Profile, ProfileService } from '../profile/profile.service'; +import { Post, PostService } from '../post.service'; @Component({ selector: 'app-admin', @@ -10,16 +11,26 @@ import { Profile, ProfileService } from '../profile/profile.service'; export class AdminComponent implements OnInit { public profile$: Observable; + public deletedPost$: Observable; + public links = [ { label: 'Users', path: '/admin/users' }, { label: 'Roles', path: '/admin/roles' }, ]; - constructor(public profileService: ProfileService) { + constructor( + public profileService: ProfileService, + private postService: PostService, + ) { this.profile$ = profileService.profile$; + this.deletedPost$ = postService.getDeletedPosts(); } ngOnInit(): void {} + showDeletedPosts() { + return this.deletedPost$ + } + } \ No newline at end of file diff --git a/frontend/src/app/post.service.ts b/frontend/src/app/post.service.ts index 14ccf07..ba441f8 100644 --- a/frontend/src/app/post.service.ts +++ b/frontend/src/app/post.service.ts @@ -41,9 +41,15 @@ export class PostService { constructor(private http: HttpClient) {} posts: Post[] = []; + deletedPosts: Post[] = []; - getPost() { - return this.posts; + getPost(id: number) { + for (let i = 0; i < this.posts.length; i++) { + if (this.posts[i].id == id) { + return this.posts[i]; + } + } + return throwError(() => new Error("Post not found.")); } @@ -57,6 +63,16 @@ export class PostService { ); } + getDeletedPosts(): Observable{ + return this.http.get('/api/post').pipe( // need to fix routing @ warren + map((posts: Post[]) => { + // Sort the posts in descending order based on timestamp + posts.sort((a: Post, b: Post) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()); + return posts; + }) + ); + } + makePost(id: number, title: string, content: string, user: Profile, votes: [], timestamp: string): Observable { if ((title.length == 0) && (content.length == 0)) { diff --git a/frontend/src/app/viewforum/viewforum.component.ts b/frontend/src/app/viewforum/viewforum.component.ts index 9ddbf70..f9064e7 100644 --- a/frontend/src/app/viewforum/viewforum.component.ts +++ b/frontend/src/app/viewforum/viewforum.component.ts @@ -16,10 +16,12 @@ export class viewforumComponent { public adminPermission$: Observable; public post$: Observable; public displayPost$: Observable; + public deletedPost$: Observable public itemsPerPage: number = 5; // set the number of items to display per page public currentPage: number = 1; public numPosts: number; public numPages: number; + public postCopy: Post; // error since not being read public static Route = { path: 'viewforum', @@ -29,10 +31,12 @@ export class viewforumComponent { constructor( private postService: PostService, private permission: PermissionService, + private postCopy: Post, //error since not being read ) { this.numPosts = 0; //numPosts and numPages must be set to zero initially this.numPages = 0; this.post$ = postService.getPosts() + this.deletedPost$ = postService.getDeletedPosts() this.displayPost$ = this.post$.pipe( //paginating total number of posts map((items) => { const startIndex = (this.currentPage - 1) * this.itemsPerPage; @@ -51,6 +55,9 @@ export class viewforumComponent { onDelete(id: number): void { if(confirm("Are you sure you want to delete this post?")) { + let postCopy = this.postService.getPost(id); // make a copy of the post to be deleted + // this.deletedPost$.push(postCopy); // need to add this deleted post on the array, so we can refer to it later + this.postService .deletePost(id) .subscribe({ // stopping here when we use the /api/posts + id route) @@ -62,6 +69,7 @@ export class viewforumComponent { private onSuccess(): void { // get new posts after deletion this.post$ = this.postService.getPosts() + this.displayPost$ = this.post$.pipe( map((items) => { const startIndex = (this.currentPage - 1) * this.itemsPerPage;