Skip to content
Snippets Groups Projects
Commit 27399ae7 authored by Tormod Nygård's avatar Tormod Nygård
Browse files

Issue: Get and display post from DB (#3)

parent a4dafc2c
No related branches found
No related tags found
1 merge request!3Resolve "Frontend: Annonser form"
...@@ -6,7 +6,7 @@ export class Post implements Deserializable, Serializable { ...@@ -6,7 +6,7 @@ export class Post implements Deserializable, Serializable {
private title: string; private title: string;
private description: string; private description: string;
private timestamp: Date; private timestamp: Date;
private user: string; private owner: string;
private imageUrl: string; private imageUrl: string;
constructor(input: any = null) { constructor(input: any = null) {
...@@ -15,14 +15,14 @@ export class Post implements Deserializable, Serializable { ...@@ -15,14 +15,14 @@ export class Post implements Deserializable, Serializable {
this.title = input.title; this.title = input.title;
this.description = input.description; this.description = input.description;
this.timestamp = new Date(input.timestamp); this.timestamp = new Date(input.timestamp);
this.user = input.user; this.owner = input.owner;
this.imageUrl = input.imageUrl; this.imageUrl = input.imageUrl;
} else { } else {
this.id = 0; this.id = 0;
this.title = ""; this.title = "";
this.description = ""; this.description = "";
this.timestamp = new Date(); this.timestamp = new Date();
this.user = ""; this.owner = "";
this.imageUrl = ""; this.imageUrl = "";
} }
} }
...@@ -41,7 +41,7 @@ export class Post implements Deserializable, Serializable { ...@@ -41,7 +41,7 @@ export class Post implements Deserializable, Serializable {
title: this.title, title: this.title,
description: this.description, description: this.description,
timestamp: this.timestamp.valueOf(), timestamp: this.timestamp.valueOf(),
user: this.user, owner: this.owner,
imageUrl: this.imageUrl imageUrl: this.imageUrl
}; };
} }
...@@ -78,12 +78,12 @@ export class Post implements Deserializable, Serializable { ...@@ -78,12 +78,12 @@ export class Post implements Deserializable, Serializable {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
get getUser () { get getOwner () {
return this.user; return this.owner;
} }
set setUser (user: string) { set setOwner (owner: string) {
this.user = user; this.owner = owner;
} }
get getImageUrl () { get getImageUrl () {
......
<p>Post form!</p> <p>Post from DB:</p>
<p>Title: {{deserializedPost.getTitle}}</p> <p>Title: {{displayPost.getTitle}}</p>
<p>Description: {{deserializedPost.getDescription}}</p> <p>Description: {{displayPost.getDescription}}</p>
\ No newline at end of file <p>Timestamp: {{displayPost.getTimestamp}}</p>
<p>Owner: {{displayPost.getOwner}}</p>
\ No newline at end of file
...@@ -11,6 +11,7 @@ export class PostFormComponent implements OnInit { ...@@ -11,6 +11,7 @@ export class PostFormComponent implements OnInit {
serializedPost: Object = {}; serializedPost: Object = {};
deserializedPost: Post = new Post(); deserializedPost: Post = new Post();
displayPost: Post = new Post();
constructor(private postService: PostService) { } constructor(private postService: PostService) { }
...@@ -27,9 +28,11 @@ export class PostFormComponent implements OnInit { ...@@ -27,9 +28,11 @@ export class PostFormComponent implements OnInit {
this.serializedPost = post.serialize(); this.serializedPost = post.serialize();
this.deserializedPost.deserialize(post.serialize()); this.deserializedPost.deserialize(post.serialize());
this.postService.getPost("0").then((gettedPost: Post) => { this.postService.getPost(0)
console.log(gettedPost); .then((gettedPost: Post) => {
} this.displayPost = gettedPost;
); }).catch((err: any) => {
console.log(err);
});
} }
} }
...@@ -11,13 +11,16 @@ export class PostService { ...@@ -11,13 +11,16 @@ export class PostService {
constructor(private http: HttpClient) { } constructor(private http: HttpClient) { }
getPost(id: string): Promise<Post>{ getPost(id: number): Promise<Post> {
return new Promise<Post>( return new Promise<Post>(
(resolve, reject) => { (resolve, reject) => {
this.get_post(id).subscribe((data: any) => { this.get_post(id).subscribe((data: any) => {
try{ try {
const post = new Post(); 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); resolve(post);
} catch (err: any) { } catch (err: any) {
reject(err); reject(err);
...@@ -27,15 +30,15 @@ export class PostService { ...@@ -27,15 +30,15 @@ export class PostService {
); );
} }
get_post(id: string) { get_post(id: number) {
return this.http.get(this.postUrl + id); return this.http.get(this.postUrl + id);
} }
addPost(post: Post): Promise<boolean>{ addPost(post: Post): Promise<boolean> {
return new Promise<boolean>( return new Promise<boolean>(
(resolve, reject) => { (resolve, reject) => {
this.add_post(post).subscribe((data: any) => { this.add_post(post).subscribe((data: any) => {
try{ try {
resolve(data.status); resolve(data.status);
} catch (err: any) { } catch (err: any) {
reject(err); reject(err);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment