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

Issue: Added post detail component (#4)

parent 11ba2e08
No related branches found
No related tags found
No related merge requests found
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PostDetailsComponent } from './posts/post-details/post-details.component';
import { PostFormComponent } from './posts/post-form/post-form.component';
const routes: Routes = [
{ path: 'annonse/ny', component: PostFormComponent }
{ path: 'annonse/ny', component: PostFormComponent },
{ path: 'annonse/:id', component: PostDetailsComponent }
];
@NgModule({
......
<h3>{{post.getTitle}}</h3>
<img [src]="post.getImageUrl">
<p>{{post.getDescription}}</p>
<br>
<p>Publiseringstidspunkt: {{post.getTimestamp}}</p>
<p>Eier: {{post.getOwner}}</p>
\ No newline at end of file
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { PostDetailsComponent } from './post-details.component';
describe('PostDetailsComponent', () => {
let component: PostDetailsComponent;
let fixture: ComponentFixture<PostDetailsComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ PostDetailsComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(PostDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit } from '@angular/core';
import { Post } from 'src/app/models/post.model';
import { PostService } from '../post.service';
import { ActivatedRoute } from '@angular/router'
@Component({
selector: 'app-post-details',
templateUrl: './post-details.component.html',
styleUrls: ['./post-details.component.scss']
})
export class PostDetailsComponent implements OnInit {
post: Post = new Post();
constructor(private postService: PostService, private activatedRoute: ActivatedRoute) { }
ngOnInit(): void {
// Gets id parameter from URL
this.activatedRoute.params.subscribe(params => {
const id = params["id"];
// Gets Post with id from database
this.postService.getPost(id).then(post => {
this.post = post;
});
});
}
}
......@@ -3,12 +3,14 @@ import { CommonModule } from '@angular/common';
import { PostFormComponent } from './post-form/post-form.component';
import { SharedModule } from '../shared/shared.module';
import { FormsModule } from '@angular/forms';
import { PostDetailsComponent } from './post-details/post-details.component';
@NgModule({
declarations: [
PostFormComponent
PostFormComponent,
PostDetailsComponent
],
imports: [
CommonModule,
......
......@@ -36,7 +36,7 @@ router.route('/').post(async (request: Request, response: Response) => {
router.route('/').get(async (_: Request, response: Response) => {
try {
//response.status(200).json(await query("SELECT * FROM post;",""));
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.navn, p.imageUrl
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid;`
response.status(200).json(await query(input,""));
......@@ -50,7 +50,7 @@ router.route('/:id').get(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
//response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.navn, p.imageUrl
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid WHERE p.id=?;`
response.status(200).json(await query(input,[postId]));
......
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