From d21804e64aaac14844616aa280409d635aaba50f Mon Sep 17 00:00:00 2001 From: Warren Quadland <59710771+wquadland@users.noreply.github.com> Date: Tue, 11 Apr 2023 18:23:58 -0400 Subject: [PATCH 1/8] Implement backend service and api route for DELETE (#46) --- backend/api/post.py | 7 +++++++ backend/entities/post_entity.py | 6 +++--- backend/entities/post_votes_entity.py | 2 +- backend/services/post.py | 11 ++++++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/backend/api/post.py b/backend/api/post.py index dc52f9c..8ced200 100644 --- a/backend/api/post.py +++ b/backend/api/post.py @@ -17,3 +17,10 @@ def create(post: Post, post_svc: PostService = Depends(), usr_svc: UserService = @api.get("", response_model=list[Post], tags=['Post']) def getAll(post_svc: PostService = Depends()): return post_svc.getAll() + +@api.delete("", tags=['Post']) +def delete(id: int, post_svc: PostService = Depends()): + try: + post_svc.delete(id) + except: + raise HTTPException(status_code=422, detail=str("Post not found")) diff --git a/backend/entities/post_entity.py b/backend/entities/post_entity.py index 728eba6..220cccb 100644 --- a/backend/entities/post_entity.py +++ b/backend/entities/post_entity.py @@ -20,7 +20,7 @@ class PostEntity(EntityBase): __tablename__ = 'posts' - id: Mapped[int] = mapped_column(Integer, primary_key=True) + post_id: Mapped[int] = mapped_column(Integer, primary_key=True) title: Mapped[str] = mapped_column(String(64), nullable = False, default = '') content: Mapped[str] = mapped_column(Text, nullable=False, default='') @@ -35,7 +35,7 @@ class PostEntity(EntityBase): def from_model(cls, model: Post, user: UserEntity ) -> Self: #user_svc: UserPostService = Depends() return cls( - id=model.id, + post_id=model.id, title = model.title, content=model.content, user = user, @@ -49,7 +49,7 @@ def from_model(cls, model: Post, user: UserEntity ) -> Self: def to_model(self) -> Post: vote_num = [vote.to_model() for vote in self.votes] return Post( - id=self.id, + id=self.post_id, title = self.title, content=self.content, user=self.user.to_model(), diff --git a/backend/entities/post_votes_entity.py b/backend/entities/post_votes_entity.py index 471e872..2881b54 100644 --- a/backend/entities/post_votes_entity.py +++ b/backend/entities/post_votes_entity.py @@ -5,5 +5,5 @@ "post_votes", EntityBase.metadata, Column('user_id', ForeignKey('user.id'), primary_key=True), - Column('post_id', ForeignKey('posts.id'), primary_key=True) + Column('post_id', ForeignKey('posts.post_id'), primary_key=True) ) \ No newline at end of file diff --git a/backend/services/post.py b/backend/services/post.py index 5ef6a30..17429df 100644 --- a/backend/services/post.py +++ b/backend/services/post.py @@ -23,4 +23,13 @@ def create(self, post: Post, user: UserEntity) -> Post: def getAll(self) -> list[Post]: query = select(PostEntity) entities = self._session.scalars(query).all() - return [entity.to_model() for entity in entities] \ No newline at end of file + return [entity.to_model() for entity in entities] + + def delete(self, id: int) -> None: + query = select(PostEntity).filter_by(post_id=id) + post = self._session.execute(query).scalar_one() + print(post) + if (post == None): + raise Exception("Post not found") + self._session.delete(post) + self._session.commit() \ No newline at end of file From 09d43e7d110dfa1e416e8a392a8c3deef4f6a6cb Mon Sep 17 00:00:00 2001 From: Aayush Mehra Date: Tue, 11 Apr 2023 18:26:22 -0400 Subject: [PATCH 2/8] Add frontend button for deleting posts for admin story (#47) --- .../app/navigation/navigation.component.ts | 5 +-- .../src/app/navigation/navigation.service.ts | 11 +++++-- frontend/src/app/post.service.ts | 6 ++-- .../app/viewforum/viewforum.component.html | 1 + .../src/app/viewforum/viewforum.component.ts | 32 ++++++++++++++++++- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/navigation/navigation.component.ts b/frontend/src/app/navigation/navigation.component.ts index 632a75e..1047a9b 100644 --- a/frontend/src/app/navigation/navigation.component.ts +++ b/frontend/src/app/navigation/navigation.component.ts @@ -10,6 +10,7 @@ import { AuthenticationService } from '../authentication.service'; import { Router } from '@angular/router'; import { Profile, ProfileService } from '../profile/profile.service'; import { PermissionService } from '../permission.service'; +import { Post, PostService } from '../post.service'; @Component({ selector: 'app-navigation', @@ -34,7 +35,8 @@ export class NavigationComponent implements OnInit, OnDestroy { private profileService: ProfileService, private breakpointObserver: BreakpointObserver, protected navigationService: NavigationTitleService, - protected errorDialog: MatDialog + protected errorDialog: MatDialog, + private postService: PostService ) { this.profile$ = profileService.profile$; this.checkinPermission$ = this.permission.check('checkin.create', 'checkin/'); @@ -73,5 +75,4 @@ export class NavigationComponent implements OnInit, OnDestroy { .pipe(map(result => result.matches)) .subscribe(isHandset => this.isHandset = isHandset); } - } \ No newline at end of file diff --git a/frontend/src/app/navigation/navigation.service.ts b/frontend/src/app/navigation/navigation.service.ts index ff77a56..6e18aa1 100644 --- a/frontend/src/app/navigation/navigation.service.ts +++ b/frontend/src/app/navigation/navigation.service.ts @@ -1,4 +1,4 @@ -import { HttpErrorResponse } from '@angular/common/http'; +import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; @@ -19,7 +19,9 @@ export class NavigationService { private _error: BehaviorSubject = new BehaviorSubject(null); public error$ = this._error.asObservable(); - constructor() {} + constructor( + private http: HttpClient, + ) {} setTitle(title: string) { this.title.next(title); @@ -37,4 +39,9 @@ export class NavigationService { this._error.next(`Response: ${e.status} ${e.statusText}\nEndpoint: ${e.url}`); } + deletePost(id: number) { + console.log(id) // test + return this.http.delete("" + id) + } + } \ No newline at end of file diff --git a/frontend/src/app/post.service.ts b/frontend/src/app/post.service.ts index b4010db..4fb7ec6 100644 --- a/frontend/src/app/post.service.ts +++ b/frontend/src/app/post.service.ts @@ -77,8 +77,8 @@ export class PostService { return throwError(() => new Error("Unable to Post from user")); } - // deletePost(id: number) { - // return this.http.delete("/api/forum/" + id) - // } + deletePost(id: number) { + return this.http.delete("/api/forum/" + id) + } } diff --git a/frontend/src/app/viewforum/viewforum.component.html b/frontend/src/app/viewforum/viewforum.component.html index 24a4866..ba7eb56 100644 --- a/frontend/src/app/viewforum/viewforum.component.html +++ b/frontend/src/app/viewforum/viewforum.component.html @@ -15,6 +15,7 @@

Forum

{{ post.user.first_name}} {{ post.user.last_name}} {{ post.timestamp }} + Make a Post diff --git a/frontend/src/app/viewforum/viewforum.component.ts b/frontend/src/app/viewforum/viewforum.component.ts index 31fe2c4..df956fe 100644 --- a/frontend/src/app/viewforum/viewforum.component.ts +++ b/frontend/src/app/viewforum/viewforum.component.ts @@ -1,6 +1,8 @@ import { Component } from '@angular/core'; import { Post, PostService } from '../post.service'; import { Observable } from 'rxjs'; +import { PermissionService } from '../permission.service'; +import { HttpErrorResponse } from '@angular/common/http'; @Component({ selector: 'app-getforum', @@ -8,14 +10,42 @@ import { Observable } from 'rxjs'; styleUrls: ['./viewforum.component.css'] }) export class viewforumComponent { + public adminPermission$: Observable; + public post$: Observable; public static Route = { path: 'viewforum', component: viewforumComponent }; - constructor(private postService: PostService) { + constructor( + private postService: PostService, + private permission: PermissionService, + ) { this.post$ = postService.getPosts() + this.adminPermission$ = this.permission.check('admin.view', 'admin/') + } + + onDelete(id: number): void { + this.postService + .deletePost(id) + .subscribe({ + next: () => this.onSuccess(), + error: (err) => this.onError(err) + }) + } + + private onSuccess(): void { + this.post$ = this.postService.getPosts() + } + + private onError(err: HttpErrorResponse) { + console.log(err) + if (err.error.detail) { + window.alert(err.error.detail); + } else { + window.alert("random error: " + JSON.stringify(err)); + } } } From ecddc27fd8d4316a0927aa7d49c442f79f39e793 Mon Sep 17 00:00:00 2001 From: angelinasu57 Date: Wed, 12 Apr 2023 09:05:35 -0400 Subject: [PATCH 3/8] Testing onDelete() --- frontend/src/app/viewforum/viewforum.component.html | 2 +- frontend/src/app/viewforum/viewforum.component.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/viewforum/viewforum.component.html b/frontend/src/app/viewforum/viewforum.component.html index ba7eb56..c48a1de 100644 --- a/frontend/src/app/viewforum/viewforum.component.html +++ b/frontend/src/app/viewforum/viewforum.component.html @@ -15,7 +15,7 @@

Forum

{{ post.user.first_name}} {{ post.user.last_name}} {{ post.timestamp }} - + Make a Post diff --git a/frontend/src/app/viewforum/viewforum.component.ts b/frontend/src/app/viewforum/viewforum.component.ts index df956fe..0308491 100644 --- a/frontend/src/app/viewforum/viewforum.component.ts +++ b/frontend/src/app/viewforum/viewforum.component.ts @@ -11,8 +11,8 @@ import { HttpErrorResponse } from '@angular/common/http'; }) export class viewforumComponent { public adminPermission$: Observable; - public post$: Observable; + public static Route = { path: 'viewforum', component: viewforumComponent @@ -35,7 +35,7 @@ export class viewforumComponent { }) } - private onSuccess(): void { + private onSuccess(): void { // get new posts after deletion this.post$ = this.postService.getPosts() } From 82f0258bad76fd63b2b1d278a9bd2184445a2a58 Mon Sep 17 00:00:00 2001 From: angelinasu57 Date: Wed, 12 Apr 2023 09:47:59 -0400 Subject: [PATCH 4/8] Changed deletePost route to /viewforum. (Buggy) --- frontend/src/app/post.service.ts | 3 ++- frontend/src/app/viewforum/viewforum.component.ts | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/post.service.ts b/frontend/src/app/post.service.ts index 4fb7ec6..f1ba9a3 100644 --- a/frontend/src/app/post.service.ts +++ b/frontend/src/app/post.service.ts @@ -78,7 +78,8 @@ export class PostService { } deletePost(id: number) { - return this.http.delete("/api/forum/" + id) + return this.http.delete("/viewforum/post/" + id) + // return this.http.delete("/api/post/" + id) // what we had before (from the backend routes) } } diff --git a/frontend/src/app/viewforum/viewforum.component.ts b/frontend/src/app/viewforum/viewforum.component.ts index 0308491..eab3b6e 100644 --- a/frontend/src/app/viewforum/viewforum.component.ts +++ b/frontend/src/app/viewforum/viewforum.component.ts @@ -29,7 +29,7 @@ export class viewforumComponent { onDelete(id: number): void { this.postService .deletePost(id) - .subscribe({ + .subscribe({ // stopping here when we use the /api/posts + id route) next: () => this.onSuccess(), error: (err) => this.onError(err) }) @@ -37,14 +37,15 @@ export class viewforumComponent { private onSuccess(): void { // get new posts after deletion this.post$ = this.postService.getPosts() + console.log("going to onSuccess") // test } private onError(err: HttpErrorResponse) { - console.log(err) if (err.error.detail) { window.alert(err.error.detail); } else { - window.alert("random error: " + JSON.stringify(err)); + console.log("onDelete going to error"); // test (going here when we use the /viewforum/post + id route) + console.log("random error: " + JSON.stringify(err)); // see what error is } } From d4d0090954ccd239d20310b2e052c1d3ba77a513 Mon Sep 17 00:00:00 2001 From: wastilla Date: Wed, 12 Apr 2023 13:30:07 -0400 Subject: [PATCH 5/8] Connect backend delete services to frontend componenets --- backend/api/post.py | 6 +++--- backend/services/post.py | 5 +++-- frontend/src/app/post.service.ts | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/api/post.py b/backend/api/post.py index 8ced200..b16f64a 100644 --- a/backend/api/post.py +++ b/backend/api/post.py @@ -18,9 +18,9 @@ def create(post: Post, post_svc: PostService = Depends(), usr_svc: UserService = def getAll(post_svc: PostService = Depends()): return post_svc.getAll() -@api.delete("", tags=['Post']) -def delete(id: int, post_svc: PostService = Depends()): +@api.delete("/{id}", tags=['Post']) +def delete(id: int, post_svc: PostService = Depends()) -> bool: try: - post_svc.delete(id) + return post_svc.delete(id=id) except: raise HTTPException(status_code=422, detail=str("Post not found")) diff --git a/backend/services/post.py b/backend/services/post.py index 17429df..9d626d8 100644 --- a/backend/services/post.py +++ b/backend/services/post.py @@ -25,11 +25,12 @@ def getAll(self) -> list[Post]: entities = self._session.scalars(query).all() return [entity.to_model() for entity in entities] - def delete(self, id: int) -> None: + def delete(self, id: int) -> bool: query = select(PostEntity).filter_by(post_id=id) post = self._session.execute(query).scalar_one() print(post) if (post == None): raise Exception("Post not found") self._session.delete(post) - self._session.commit() \ No newline at end of file + self._session.commit() + return True \ No newline at end of file diff --git a/frontend/src/app/post.service.ts b/frontend/src/app/post.service.ts index f1ba9a3..855611c 100644 --- a/frontend/src/app/post.service.ts +++ b/frontend/src/app/post.service.ts @@ -78,7 +78,7 @@ export class PostService { } deletePost(id: number) { - return this.http.delete("/viewforum/post/" + id) + return this.http.delete("/api/post/" + id) // return this.http.delete("/api/post/" + id) // what we had before (from the backend routes) } From ba757ebaed49212925a65a8d23591bc7480aa5f9 Mon Sep 17 00:00:00 2001 From: wastilla Date: Wed, 12 Apr 2023 13:52:00 -0400 Subject: [PATCH 6/8] Remove unneccessary print statements --- backend/services/post.py | 1 - frontend/src/app/viewforum/viewforum.component.ts | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/backend/services/post.py b/backend/services/post.py index 9d626d8..af80787 100644 --- a/backend/services/post.py +++ b/backend/services/post.py @@ -28,7 +28,6 @@ def getAll(self) -> list[Post]: def delete(self, id: int) -> bool: query = select(PostEntity).filter_by(post_id=id) post = self._session.execute(query).scalar_one() - print(post) if (post == None): raise Exception("Post not found") self._session.delete(post) diff --git a/frontend/src/app/viewforum/viewforum.component.ts b/frontend/src/app/viewforum/viewforum.component.ts index eab3b6e..431ed30 100644 --- a/frontend/src/app/viewforum/viewforum.component.ts +++ b/frontend/src/app/viewforum/viewforum.component.ts @@ -37,15 +37,13 @@ export class viewforumComponent { private onSuccess(): void { // get new posts after deletion this.post$ = this.postService.getPosts() - console.log("going to onSuccess") // test } private onError(err: HttpErrorResponse) { if (err.error.detail) { window.alert(err.error.detail); } else { - console.log("onDelete going to error"); // test (going here when we use the /viewforum/post + id route) - console.log("random error: " + JSON.stringify(err)); // see what error is + console.log(JSON.stringify(err)); // see what error is } } From 317f35329c6e2dd97b4e1544eb7ee0ac9fe7b968 Mon Sep 17 00:00:00 2001 From: wastilla Date: Wed, 12 Apr 2023 13:54:36 -0400 Subject: [PATCH 7/8] Change back button name to more descriptive --- frontend/src/app/makeforum/makeforum.component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/app/makeforum/makeforum.component.html b/frontend/src/app/makeforum/makeforum.component.html index 95c78e7..0d235ca 100644 --- a/frontend/src/app/makeforum/makeforum.component.html +++ b/frontend/src/app/makeforum/makeforum.component.html @@ -16,4 +16,4 @@

Make a Forum Post With Resources

- + From 7c2b4ac655cfc66e7237b95d078d8094a475d2a5 Mon Sep 17 00:00:00 2001 From: wastilla Date: Wed, 12 Apr 2023 13:56:11 -0400 Subject: [PATCH 8/8] Change button name to more descriptive (last commit did not actually change it) --- frontend/src/app/makeforum/makeforum.component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/app/makeforum/makeforum.component.html b/frontend/src/app/makeforum/makeforum.component.html index 0d235ca..b21f0a6 100644 --- a/frontend/src/app/makeforum/makeforum.component.html +++ b/frontend/src/app/makeforum/makeforum.component.html @@ -16,4 +16,4 @@

Make a Forum Post With Resources

- +