From 27399ae7d03489cb3abce6128f75b411b55b816b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tormod=20Nyg=C3=A5rd?= <tormodny@stud.ntnu.no> Date: Thu, 11 Feb 2021 11:20:34 +0100 Subject: [PATCH] Issue: Get and display post from DB (#3) --- client/src/app/models/post.model.ts | 16 ++++++++-------- .../app/posts/post-form/post-form.component.html | 8 +++++--- .../app/posts/post-form/post-form.component.ts | 11 +++++++---- client/src/app/posts/post.service.ts | 15 +++++++++------ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/client/src/app/models/post.model.ts b/client/src/app/models/post.model.ts index fc31ccd..cf9fd4d 100644 --- a/client/src/app/models/post.model.ts +++ b/client/src/app/models/post.model.ts @@ -6,7 +6,7 @@ export class Post implements Deserializable, Serializable { private title: string; private description: string; private timestamp: Date; - private user: string; + private owner: string; private imageUrl: string; constructor(input: any = null) { @@ -15,14 +15,14 @@ export class Post implements Deserializable, Serializable { this.title = input.title; this.description = input.description; this.timestamp = new Date(input.timestamp); - this.user = input.user; + this.owner = input.owner; this.imageUrl = input.imageUrl; } else { this.id = 0; this.title = ""; this.description = ""; this.timestamp = new Date(); - this.user = ""; + this.owner = ""; this.imageUrl = ""; } } @@ -41,7 +41,7 @@ export class Post implements Deserializable, Serializable { title: this.title, description: this.description, timestamp: this.timestamp.valueOf(), - user: this.user, + owner: this.owner, imageUrl: this.imageUrl }; } @@ -78,12 +78,12 @@ export class Post implements Deserializable, Serializable { this.timestamp = timestamp; } - get getUser () { - return this.user; + get getOwner () { + return this.owner; } - set setUser (user: string) { - this.user = user; + set setOwner (owner: string) { + this.owner = owner; } get getImageUrl () { diff --git a/client/src/app/posts/post-form/post-form.component.html b/client/src/app/posts/post-form/post-form.component.html index 7ed27b3..d3731fb 100644 --- a/client/src/app/posts/post-form/post-form.component.html +++ b/client/src/app/posts/post-form/post-form.component.html @@ -1,4 +1,6 @@ -<p>Post form!</p> +<p>Post from DB:</p> -<p>Title: {{deserializedPost.getTitle}}</p> -<p>Description: {{deserializedPost.getDescription}}</p> \ No newline at end of file +<p>Title: {{displayPost.getTitle}}</p> +<p>Description: {{displayPost.getDescription}}</p> +<p>Timestamp: {{displayPost.getTimestamp}}</p> +<p>Owner: {{displayPost.getOwner}}</p> \ No newline at end of file diff --git a/client/src/app/posts/post-form/post-form.component.ts b/client/src/app/posts/post-form/post-form.component.ts index 3b85617..c56ea19 100644 --- a/client/src/app/posts/post-form/post-form.component.ts +++ b/client/src/app/posts/post-form/post-form.component.ts @@ -11,6 +11,7 @@ export class PostFormComponent implements OnInit { serializedPost: Object = {}; deserializedPost: Post = new Post(); + displayPost: Post = new Post(); constructor(private postService: PostService) { } @@ -27,9 +28,11 @@ export class PostFormComponent implements OnInit { this.serializedPost = post.serialize(); this.deserializedPost.deserialize(post.serialize()); - this.postService.getPost("0").then((gettedPost: Post) => { - console.log(gettedPost); - } - ); + this.postService.getPost(0) + .then((gettedPost: Post) => { + this.displayPost = gettedPost; + }).catch((err: any) => { + console.log(err); + }); } } diff --git a/client/src/app/posts/post.service.ts b/client/src/app/posts/post.service.ts index 2980389..2314d5d 100644 --- a/client/src/app/posts/post.service.ts +++ b/client/src/app/posts/post.service.ts @@ -11,13 +11,16 @@ export class PostService { constructor(private http: HttpClient) { } - getPost(id: string): Promise<Post>{ + getPost(id: number): Promise<Post> { return new Promise<Post>( (resolve, reject) => { this.get_post(id).subscribe((data: any) => { - try{ + try { const post = new Post(); - post.deserialize(data); + post.deserialize(data.data[0]); + if (post.getId == 0) { + reject("Could not find with Post with id: " + id); + } resolve(post); } catch (err: any) { reject(err); @@ -27,15 +30,15 @@ export class PostService { ); } - get_post(id: string) { + get_post(id: number) { return this.http.get(this.postUrl + id); } - addPost(post: Post): Promise<boolean>{ + addPost(post: Post): Promise<boolean> { return new Promise<boolean>( (resolve, reject) => { this.add_post(post).subscribe((data: any) => { - try{ + try { resolve(data.status); } catch (err: any) { reject(err); -- GitLab