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

Issue: Basic form for post (#3)

parent 862fa58b
No related branches found
No related tags found
1 merge request!3Resolve "Frontend: Annonser form"
...@@ -8,22 +8,28 @@ export class Post implements Deserializable, Serializable { ...@@ -8,22 +8,28 @@ export class Post implements Deserializable, Serializable {
private timestamp: Date; private timestamp: Date;
private owner: string; private owner: string;
private imageUrl: string; private imageUrl: string;
private price: number;
private categoryid: number;
constructor(input: any = null) { constructor(input: any = null) {
if (input) { if (input) {
this.id = input.id; this.id = input.id;
this.title = input.title; this.title = input.title;
this.description = input.description; this.description = input.description;
this.timestamp = new Date(input.timestamp); this.timestamp = input.timestamp;
this.owner = input.owner; this.owner = input.owner;
this.imageUrl = input.imageUrl; this.imageUrl = input.imageUrl;
this.price = input.price;
this.categoryid = input.categoryid;
} else { } else {
this.id = 0; this.id = 0;
this.title = ""; this.title = null;
this.description = ""; this.description = null;
this.timestamp = new Date(); this.timestamp = new Date();
this.owner = ""; this.owner = null;
this.imageUrl = ""; this.imageUrl = null;
this.price = null;
this.categoryid = null;
} }
} }
...@@ -42,55 +48,73 @@ export class Post implements Deserializable, Serializable { ...@@ -42,55 +48,73 @@ export class Post implements Deserializable, Serializable {
description: this.description, description: this.description,
timestamp: this.timestamp.valueOf(), timestamp: this.timestamp.valueOf(),
owner: this.owner, owner: this.owner,
imageUrl: this.imageUrl imageUrl: this.imageUrl,
price: this.price,
categoryid: this.categoryid
}; };
} }
get getId () { get getId() {
return this.id; return this.id;
} }
set setId (id: number) { set setId(id: number) {
this.id = id; this.id = id;
} }
get getTitle () { get getTitle() {
return this.title; return this.title;
} }
set setTitle (title: string) { set setTitle(title: string) {
this.title = title; this.title = title;
} }
get getDescription () { get getDescription() {
return this.description; return this.description;
} }
set setDescription (description: string) { set setDescription(description: string) {
this.description = description; this.description = description;
} }
get getTimestamp () { get getTimestamp() {
return this.timestamp; return this.timestamp;
} }
set setTimestamp (timestamp: Date) { set setTimestamp(timestamp: Date) {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
get getOwner () { get getOwner() {
return this.owner; return this.owner;
} }
set setOwner (owner: string) { set setOwner(owner: string) {
this.owner = owner; this.owner = owner;
} }
get getImageUrl () { get getImageUrl() {
return this.imageUrl; return this.imageUrl;
} }
set setImageUrl (imageUrl: string) { set setImageUrl(imageUrl: string) {
this.imageUrl = imageUrl; this.imageUrl = imageUrl;
} }
get getPrice() {
return this.price;
}
set setPrice(price: number) {
this.price = price;
}
get getCategory() {
return this.categoryid;
}
set setCategory(categoryid: number) {
this.categoryid = categoryid;
}
} }
\ No newline at end of file
<p>Post from DB:</p> <h3>Lag annonse</h3>
<p>Title: {{displayPost.getTitle}}</p> <label for="title">Title</label>
<p>Description: {{displayPost.getDescription}}</p> <input id="title" [(ngModel)]="title">
<p>Timestamp: {{displayPost.getTimestamp}}</p>
<p>Owner: {{displayPost.getOwner}}</p>
<app-text-input></app-text-input> <label for="description">Description</label>
\ No newline at end of file
<input id="description" [(ngModel)]="description">
<label for="price">Pris</label>
<input id="price" [(ngModel)]="price">
<label for="category">Kategori</label>
<select id="category" [(ngModel)]="categoryid">
<option value="1">Antikviteter og Kunst</option>
<option value="2">Dyr og Utstyr</option>
<option value="3">Elektronikk og Hvitevarer</option>
</select>
<button (click)="publishPost()">Publiser</button>
\ No newline at end of file
...@@ -9,30 +9,33 @@ import { PostService } from '../post.service'; ...@@ -9,30 +9,33 @@ import { PostService } from '../post.service';
}) })
export class PostFormComponent implements OnInit { export class PostFormComponent implements OnInit {
serializedPost: Object = {}; title: string = "";
deserializedPost: Post = new Post(); description: string = "";
displayPost: Post = new Post(); price: number = 0;
categoryid: number;
constructor(private postService: PostService) { } constructor(private postService: PostService) { }
ngOnInit(): void { ngOnInit(): void {
let post = new Post({
id: 0,
title: "TestAnnonse",
description: "Beskrivelse",
timestamp: 1612952332000,
owner: "Admin",
imageUrl: "url"
});
this.serializedPost = post.serialize(); }
this.deserializedPost.deserialize(post.serialize());
this.postService.getPost(1) publishPost() {
.then((gettedPost: Post) => { let newPost = new Post({
this.displayPost = gettedPost; title: this.title,
}).catch((err: any) => { description: this.description,
console.log(err); timestamp: new Date(),
owner: "admin",
imageUrl: "",
price: this.price,
categoryid: this.categoryid
});
console.log(newPost);
this.postService.addPost(newPost).then(status => {
// Flytte til annonsevisning
console.log("Post was added: " + status);
}).catch(error => {
console.log("Error adding post: " + error);
}); });
} }
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core'; ...@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { PostFormComponent } from './post-form/post-form.component'; import { PostFormComponent } from './post-form/post-form.component';
import { SharedModule } from '../shared/shared.module'; import { SharedModule } from '../shared/shared.module';
import { FormsModule } from '@angular/forms';
...@@ -11,7 +12,9 @@ import { SharedModule } from '../shared/shared.module'; ...@@ -11,7 +12,9 @@ import { SharedModule } from '../shared/shared.module';
], ],
imports: [ imports: [
CommonModule, CommonModule,
SharedModule SharedModule,
FormsModule
] ]
}) })
export class PostModule { } export class PostModule { }
...@@ -17,3 +17,4 @@ describe('PostService', () => { ...@@ -17,3 +17,4 @@ describe('PostService', () => {
expect(service).toBeTruthy(); expect(service).toBeTruthy();
}); });
}); });
...@@ -19,12 +19,16 @@ export class PostService { ...@@ -19,12 +19,16 @@ export class PostService {
const post = new Post(); const post = new Post();
post.deserialize(data.data[0]); post.deserialize(data.data[0]);
if (post.getId == 0) { if (post.getId == 0) {
reject("Could not find with Post with id: " + id); reject("Could not find Post with id: " + id);
} }
resolve(post); resolve(post);
} catch (err: any) { } catch (err: any) {
reject(err); reject(err);
} }
},
(err: any) => {
console.log(err.message);
reject(err);
}); });
} }
); );
...@@ -34,15 +38,20 @@ export class PostService { ...@@ -34,15 +38,20 @@ export class PostService {
return this.http.get(this.postUrl + id); return this.http.get(this.postUrl + id);
} }
addPost(post: Post): Promise<boolean> { addPost(post: Post): Promise<string> {
return new Promise<boolean>( return new Promise<string>(
(resolve, reject) => { (resolve, reject) => {
this.add_post(post).subscribe((data: any) => { this.add_post(post).subscribe((data: any) => {
try { try {
console.log(data);
resolve(data.status); resolve(data.status);
} catch (err: any) { } catch (err: any) {
reject(err); reject(err);
} }
},
(err: any) => {
console.log(err.message);
reject(err);
}); });
} }
); );
......
<label for="textField">{{label}}</label>
<input type="text" id="textField"> <input type="text" id="textField">
\ No newline at end of file
import { Component, OnInit } from '@angular/core'; import { Component, Input, OnInit, Output } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
@Component({ @Component({
selector: 'app-text-input', selector: 'app-text-input',
templateUrl: './text-input.component.html', templateUrl: './text-input.component.html',
styleUrls: ['./text-input.component.scss'] styleUrls: ['./text-input.component.scss']
}) })
export class TextInputComponent implements OnInit { export class TextInputComponent {
@Input()
label: string = "";
constructor() { } constructor() { }
ngOnInit(): void {
}
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"baseUrl": "./", "baseUrl": "./",
"outDir": "./dist/out-tsc", "outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": false,
"noImplicitReturns": true, "noImplicitReturns": true,
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"sourceMap": true, "sourceMap": true,
......
...@@ -8,25 +8,25 @@ interface IPost { ...@@ -8,25 +8,25 @@ interface IPost {
description: string; description: string;
timestamp: number; timestamp: number;
owner: string; owner: string;
category: string; categoryid: number;
imageUrl: string; imageUrl: string;
} }
/* ============================= CREATE ============================= */ /* ============================= CREATE ============================= */
// Create posts `/api/post/` // Create posts `/api/post/`
//'{"title":"test3","description":"test3","timestamp":123123,"owner":"test3","category":"test3","imageUrl":"test3"}' //'{"title":"test3","description":"test3","timestamp":123123,"owner":"test3","category":"test3","imageUrl":"test3"}'
router.route('/').post(async (request: Request, response: Response) => { router.route('/').post(async (request: Request, response: Response) => {
const {title, description, timestamp, owner, category, imageUrl} = request.body; const {title, description, timestamp, owner, categoryid, imageUrl} = request.body;
try { try {
const post: IPost = { const post: IPost = {
"title": title, "title": title,
"description": description, "description": description,
"timestamp": timestamp, "timestamp": timestamp,
"owner": owner, "owner": owner,
"category": category, "categoryid": categoryid,
"imageUrl": imageUrl "imageUrl": imageUrl
}; };
if (Object.values(post).filter(p => p == undefined).length > 0) return response.status(500).send("Error"); if (Object.values(post).filter(p => p == undefined).length > 0) return response.status(500).send("Error");
const input = (`INSERT INTO post(title, description, timestamp, owner, category, imageUrl) VALUES (?,?,?,?,?,?)`) const input = (`INSERT INTO post(title, description, timestamp, owner, categoryid, imageUrl) VALUES (?,?,?,?,?,?)`)
return response.status(200).json( return response.status(200).json(
await query(input,Object.values(post)) await query(input,Object.values(post))
); );
......
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