Skip to content

Commit f843d0b

Browse files
committed
first commit: basic structure
0 parents  commit f843d0b

17 files changed

+372
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Setup
2+
3+
### To start server
4+
1. `cd AngularVehicleApp`
5+
2. `npm install`
6+
3. `nodemon server.js`
7+
8+
### To start client
9+
1. `cd AngularVehicleApp/client`
10+
2. `npm install`
11+
3. `npm start`
12+

client/app/app.component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app',
5+
template: '<h1> hello vehicles!</h1><vehicle></vehicle>'
6+
})
7+
export class AppComponent { }

client/app/app.module.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NgModule } from '@angular/core';
2+
import {BrowserModule} from '@angular/platform-browser';
3+
import { AppComponent } from './app.component';
4+
import { VehicleComponent } from './vehicle.component';
5+
import { HttpModule } from '@angular/http';
6+
import { FormsModule } from '@angular/forms';
7+
8+
@NgModule({
9+
imports: [BrowserModule, HttpModule, FormsModule],
10+
declarations: [AppComponent, VehicleComponent],
11+
bootstrap: [AppComponent]
12+
})
13+
export class AppModule {}

client/app/app.service.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Injectable } from '@angular/core';
2+
import { Http, Headers } from '@angular/http';
3+
import 'rxjs/add/operator/map';
4+
5+
@Injectable()
6+
export class VehicleService {
7+
8+
constructor(private http: Http) {}
9+
10+
getVehicles() {
11+
return this.http.get('/api/vehicle')
12+
.map(response => response.json());
13+
}
14+
15+
addVehicle(vehicle) {
16+
var headers = new Headers();
17+
headers.append('Content-Type', 'application/json');
18+
return this.http.post("/api/vehicle", JSON.stringify(vehicle), { headers: headers }) .map(response => response.json());
19+
}
20+
21+
viewVehicle(id) {
22+
console.log('in vehicle service' + id);
23+
return this.http.get('/api/vehicle/'+id)
24+
.map(response => response.json());
25+
}
26+
27+
removeVehicle(id) {
28+
return this.http.delete("/api/vehicles/"+ id)
29+
.map(response => response.json());
30+
}
31+
32+
}

client/app/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2+
3+
import { AppModule } from './app.module';
4+
5+
var platform = platformBrowserDynamic();
6+
7+
platform.bootstrapModule(AppModule);

client/app/vehicle.component.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Component } from '@angular/core';
2+
import { VehicleService } from './app.service';
3+
4+
@Component({
5+
selector: 'vehicle',
6+
moduleId: module.id,
7+
templateUrl: 'vehicle.html',
8+
providers: [VehicleService]
9+
})
10+
export class VehicleComponent {
11+
vehicles: Array<any>;
12+
name: string;
13+
specificVehicleName: string;
14+
15+
constructor(private vehicleService: VehicleService) {
16+
vehicleService.getVehicles().subscribe(response => {
17+
this.vehicles = response
18+
})
19+
}
20+
21+
viewVehicle(id){
22+
console.log('vehicle component with ' + id);
23+
this.vehicleService.viewVehicle(id).subscribe(data => {
24+
this.specificVehicleName = data.name;
25+
});
26+
}
27+
28+
addVehicle(){
29+
var vehicle = {
30+
name: this.name
31+
}
32+
this.vehicleService.addVehicle(vehicle)
33+
.subscribe(data => {
34+
console.log('Success adding' + data)
35+
this.vehicles.push(vehicle);
36+
})
37+
}
38+
39+
removeVehicle(id) {
40+
this.vehicleService.removeVehicle(id)
41+
.subscribe(data => {
42+
console.log('Success deleting ' + data);
43+
44+
})
45+
}
46+
47+
}

client/app/vehicle.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="container">
2+
<hr>
3+
<h3>Enter a vehicle</h3>
4+
<input type="text" name="name" [(ngModel)]="name" />
5+
<br><br>
6+
<button (click)="addVehicle()">Add a vehicle</button>
7+
</div>
8+
<div class="vehicles">
9+
<li *ngFor="let vehicle of vehicles">{{vehicle.name}}
10+
<button (click)="removeVehicle(vehicle._id)">Delete vehicle</button>
11+
</li>
12+
</div>
13+
<div class="specific">
14+
{{specificVehicleName}}
15+
</div>

client/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "angular-vehicle",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
6+
"lite": "lite-server",
7+
"tsc": "tsc",
8+
"tsc:w": "tsc -w"
9+
},
10+
"licenses": [{
11+
"type": "MIT",
12+
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
13+
}],
14+
"dependencies": {
15+
"@angular/common": "2.1.1",
16+
"@angular/compiler": "2.1.1",
17+
"@angular/core": "2.1.1",
18+
"@angular/forms": "2.1.1",
19+
"@angular/http": "2.1.1",
20+
"@angular/platform-browser": "2.1.1",
21+
"@angular/platform-browser-dynamic": "2.1.1",
22+
"@angular/router": "3.1.1",
23+
"@angular/upgrade": "2.1.1",
24+
"angular-in-memory-web-api": "0.1.13",
25+
"core-js": "2.4.1",
26+
"reflect-metadata": "0.1.8",
27+
"rxjs": "5.0.0-beta.12",
28+
"systemjs": "0.19.39",
29+
"zone.js": "0.6.25"
30+
},
31+
"devDependencies": {
32+
"@types/core-js": "0.9.34",
33+
"@types/node": "6.0.45",
34+
"concurrently": "3.0.0",
35+
"lite-server": "2.2.2",
36+
"typescript": "2.0.3"
37+
}
38+
}

client/systemjs.config.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* System configuration for Angular samples
3+
* Adjust as necessary for your application needs.
4+
*/
5+
(function(global) {
6+
System.config({
7+
paths: {
8+
// paths serve as alias
9+
'npm:': 'node_modules/'
10+
},
11+
// map tells the System loader where to look for things
12+
map: {
13+
// our app is within the app folder
14+
app: 'app',
15+
// angular bundles
16+
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
17+
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
18+
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
19+
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
20+
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
21+
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
22+
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
23+
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
24+
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
25+
// other libraries
26+
'rxjs': 'npm:rxjs',
27+
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
28+
},
29+
// packages tells the System loader how to load when no filename and/or no extension
30+
packages: {
31+
app: {
32+
main: './main.js',
33+
defaultExtension: 'js'
34+
},
35+
rxjs: {
36+
defaultExtension: 'js'
37+
}
38+
}
39+
});
40+
})(this);

client/tsconfig.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"sourceMap": true,
7+
"emitDecoratorMetadata": true,
8+
"experimentalDecorators": true,
9+
"removeComments": false,
10+
"noImplicitAny": false
11+
}
12+
}

client/views/detail.ejs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%=vehicle.name %>

client/views/index.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
3+
<head>
4+
<title>Angular Vehicle Application</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
8+
<!-- 1. Load libraries -->
9+
<!-- Polyfill(s) for older browsers -->
10+
<script src="node_modules/core-js/client/shim.min.js"></script>
11+
<script src="node_modules/zone.js/dist/zone.js"></script>
12+
<script src="node_modules/reflect-metadata/Reflect.js"></script>
13+
<script src="node_modules/systemjs/dist/system.src.js"></script>
14+
<!-- 2. Configure SystemJS -->
15+
<script src="systemjs.config.js"></script>
16+
<script>
17+
System.import('app').catch(function(err) {
18+
console.error(err);
19+
});
20+
</script>
21+
</head>
22+
<!-- 3. Display the application -->
23+
24+
<body class="container text-center">
25+
<app>Loading...</app>
26+
</body>
27+
28+
</html>

models/model.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
var VehicleSchema = new Schema({
4+
name: String
5+
});
6+
7+
var Model = mongoose.model('Vehcile', VehicleSchema);
8+
9+
module.exports = Model;

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "angularvehicle",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node server.js"
9+
},
10+
"author": "",
11+
"license": "",
12+
"dependencies": {
13+
"body-parser": "^1.15.2",
14+
"ejs": "^2.5.2",
15+
"express": "^4.14.0",
16+
"mongoose": "^4.6.5",
17+
"morgan": "^1.7.0",
18+
"path": "^0.12.7"
19+
}
20+
}

routes/api.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
var Model = require('../models/model.js');
4+
5+
router.get('/vehicles/:id', function(request, response) {
6+
console.log('getting a vehicle');
7+
var id = request.params.id;
8+
Model.findById(id, function(err, res) {
9+
if (err) {
10+
return response.send(err);
11+
} else {
12+
var vehicle = res;
13+
response.json(vehicle);
14+
}
15+
});
16+
});
17+
18+
19+
router.get('/vehicle', function(request, response) {
20+
Model.find({}, function(err, resources) {
21+
if (err) {
22+
response.send(err).status(404);
23+
} else {
24+
response.send(resources).status(200);
25+
}
26+
});
27+
});
28+
29+
router.delete('/vehicles/:id', function(request, response) {
30+
var id = request.params.id;
31+
Model.remove({ _id: id }, function(err, resource) {
32+
if (err) {
33+
return response.send(err);
34+
} else {
35+
response.send(resource);
36+
}
37+
})
38+
});
39+
40+
router.post('/vehicle', function(request, response) {
41+
var vehicle = new Model(request.body);
42+
vehicle.save(function(err, resource) {
43+
if (err) {
44+
response.send(err).status(501);
45+
} else {
46+
response.json(resource).status(201);
47+
}
48+
});
49+
});
50+
51+
52+
module.exports = router;

routes/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
router.get('/', function(request, response) {
5+
response.render('index.html');
6+
});
7+
8+
module.exports = router;

0 commit comments

Comments
 (0)