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 {
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 () {
......
<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
......@@ -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);
});
}
}
......@@ -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);
......
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