Skip to content
Snippets Groups Projects
Commit 16945f51 authored by GustafWallstrom's avatar GustafWallstrom
Browse files

Added component 'show-post' to show posts from database for user who are logged in

parent a3922f8d
No related branches found
No related tags found
No related merge requests found
......@@ -7,13 +7,15 @@ import { HttpClientModule } from '@angular/common/http';
import { RootComponent } from './root/root.component';
import { LoginComponent } from './login/login.component';
import { HomeComponent } from './home/home.component';
import { ShowPostComponent } from './show-post/show-post.component';
@NgModule({
declarations: [
RootComponent,
LoginComponent,
HomeComponent
HomeComponent,
ShowPostComponent
],
imports: [
BrowserModule,
......
......@@ -12,43 +12,13 @@
</li>
</ul>
</nav>
<h3 class="text-muted">Angular Blog App</h3>
<h3 class="text-muted">diSCore App</h3>
</header>
<main role="main">
<div class="jumbotron">
<h1 class="display-3">Lorem ipsum</h1>
<p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p><a class="btn btn-lg btn-success" href="#" role="button">Read More ...</a></p>
</div>
<div class="row marketing">
<div class="col-lg-6">
<h4>Subheading</h4>
<p>Donec id elit non mi porta gravida at eget metus. Maecenas faucibus mollis interdum.</p>
<h4>Subheading</h4>
<p>Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Cras mattis consectetur purus sit amet fermentum.</p>
<h4>Subheading</h4>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna.</p>
</div>
<div class="col-lg-6">
<h4>Subheading</h4>
<p>Donec id elit non mi porta gravida at eget metus. Maecenas faucibus mollis interdum.</p>
<h4>Subheading</h4>
<p>Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Cras mattis consectetur purus sit amet fermentum.</p>
<h4>Subheading</h4>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna.</p>
</div>
</div>
<app-show-post></app-show-post>
</main>
<footer class="footer">
<p>&copy; Company 2017</p>
<p>&copy; Author: Gustaf Wallström 2020, a project in the course TDDD27 - Advanced Web Programming</p>
</footer>
\ No newline at end of file
export class Post {
constructor(){
this.id = '';
this.title = '';
this.description = '';
}
public id;
public title;
public description;
}
\ No newline at end of file
<!-- <a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">Bär ner mig till sjön</h5>
<small>3 days ago</small>
</div>
<p class="mb-1">Bär ner mig till sjön.</p>
<small>Bär ner mig till sjön</small>
</a> -->
<div class="list-group">
<a *ngFor="let post of posts" href="#" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{post.title}}</h5>
<small>3 days ago</small>
</div>
<p class="mb-1">{{post.description}}</p>
<small>read more...</small>
</a>
</div>
\ No newline at end of file
import { Component, OnInit } from '@angular/core';
import { ShowPostService } from './show-post.service';
import { Post } from '../models/post.model';
@Component({
selector: 'app-show-post',
templateUrl: './show-post.component.html',
providers: [ ShowPostService ]
})
export class ShowPostComponent implements OnInit {
public posts : any [];
constructor(private showPostService: ShowPostService) {
}
ngOnInit(){
this.getAllPost();
}
getAllPost(){
this.showPostService.getAllPost().subscribe(result => {
this.posts = result['data'];
});
}
}
\ No newline at end of file
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Post } from '../models/post.model';
@Injectable()
export class ShowPostService {
constructor(private http: HttpClient){
}
getAllPost(){
return this.http.post('/api/post/getAllPost',{})
}
}
\ No newline at end of file
......@@ -2,68 +2,86 @@ const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const mongoose = require("mongoose");
const uri =
"mongodb+srv://gustaf:discorediscore@discore-w4mrp.mongodb.net/DiscGolfScores?retryWrites=true&w=majority";
"mongodb+srv://gustaf:discorediscore@discore-w4mrp.mongodb.net/DiscGolfScores?retryWrites=true&w=majority";
const User = require("./model/user");
const Post = require('./model/post');
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false,
})
bodyParser.urlencoded({
extended: false,
})
);
app.post("/api/user/login", (req, res) => {
mongoose.connect(
uri,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
function (err) {
if (err) throw err;
User.find(
{
username: req.body.username,
password: req.body.password,
},
function (err, user) {
if (err) throw err;
if (user.length === 1) {
return res.status(200).json({
status: "success",
data: user,
});
} else {
return res.status(200).json({
status: "fail",
message: "Login Failed",
});
}
}
);
}
);
mongoose.connect(
uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
},
function (err) {
if (err) throw err;
User.find({
username: req.body.username,
password: req.body.password,
},
function (err, user) {
if (err) throw err;
if (user.length === 1) {
return res.status(200).json({
status: "success",
data: user,
});
} else {
return res.status(200).json({
status: "fail",
message: "Login Failed",
});
}
}
);
}
);
});
app.post("/api/user/create", (req, res) => {
mongoose.connect(url, function (err) {
if (err) throw err;
const user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password,
});
user.save((err, res) => {
if (err) throw err;
return res.status(200).json({
status: "success",
data: res,
});
});
});
mongoose.connect(uri, function (err) {
if (err) throw err;
const user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password,
});
user.save((err, res) => {
if (err) throw err;
return res.status(200).json({
status: "success",
data: res,
});
});
});
});
app.listen(3000, () => console.log("Server running on port 3000!"));
app.post('/api/post/getAllPost', (req, res) => {
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
}, function (err) {
if (err) throw err;
Post.find({}, [], {
sort: {
_id: -1
}
}, (err, doc) => {
if (err) throw err;
return res.status(200).json({
status: 'success',
data: doc
})
})
});
})
app.listen(3000, () => console.log("Server running on port 3000!"));
\ No newline at end of file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// create a schema
const postSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
}
}, {
collection: 'post'
});
const Post = mongoose.model('Post', postSchema);
module.exports = Post;
\ No newline at end of file
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