Skip to content

Commit

Permalink
get books api call working
Browse files Browse the repository at this point in the history
  • Loading branch information
hisanhunais committed Jun 1, 2018
1 parent c061bd4 commit 7b33c04
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 27 deletions.
15 changes: 15 additions & 0 deletions server/models/book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const bookSchema = new Schema({
isbn: Number,
name: String,
category: String,
author: String,
publisher: String,
edition: Number,
noOfCopies: Number
});

module.exports = mongoose.model('book', bookSchema, 'books');
13 changes: 13 additions & 0 deletions server/models/lab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const labSchema = new Schema({
name: String,
description: String,
location: String,
noOfComputers: Number,
projector: String
});

module.exports = mongoose.model('lab', labSchema, 'labs');
55 changes: 32 additions & 23 deletions server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const express = require('express');
const router = express.Router();

const mongoose = require('mongoose');
const Video = require('../models/video');
const Book = require('../models/book');

const db = "mongodb://hisan:[email protected]:21339/videoplayer";
//const db = "mongodb://hisan:[email protected]:21339/videoplayer";
const db = "mongodb://localhost:27017/library";
mongoose.Promise = global.Promise;

mongoose.connect(db, function(err){
Expand All @@ -14,50 +15,58 @@ mongoose.connect(db, function(err){
}
});

router.get('/videos', function(req, res){
console.log("Get request for all videos");
Video.find({})
.exec(function(err, videos){
router.get('/', function(req,res){
res.send('api works');
})

router.get('/books', function(req, res){
console.log("Get request for all books");
Book.find({})
.exec(function(err, books){
if(err)
{
console.log("Error retrieving videos");
console.log("Error retrieving books");
}
else
{
res.json(videos);
res.json(books);
}
});
});

router.get('/videos/:id', function(req, res){
console.log("Get request for a single video");
Video.findById(req.params.id)
.exec(function(err, video){
router.get('/books/:id', function(req, res){
console.log("Get request for a single book");
Book.findById(req.params.id)
.exec(function(err, book){
if(err)
{
console.log("Error retrieving video");
console.log("Error retrieving book");
}
else
{
res.json(video);
res.json(book);
}
});
});

router.post('/video', function(req, res){
console.log("Post a Video");
var newVideo = new Video();
newVideo.title = req.body.title;
newVideo.url = req.body.url;
newVideo.description = req.body.description;
newVideo.save(function(err, insertedVideo){
router.post('/book', function(req, res){
console.log("Post a Book");
var newBook = new Book();
newBook.isbn = req.body.isbn;
newBook.name = req.body.name;
newBook.category = req.body.category;
newBook.author = req.body.author;
newBook.publisher = req.body.publisher;
newBook.edition = req.body.edition;
newBook.noOfCopies = req.body.noOfCopies;
newBook.save(function(err, insertedBook){
if(err)
{
console.log("Error saving video");
console.log("Error saving book");
}
else
{
res.json(insertedVideo);
res.json(insertedBook);
}
});
});
Expand Down
106 changes: 106 additions & 0 deletions server/routes/api2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const express = require('express');
const router = express.Router();

const mongoose = require('mongoose');
const Video = require('../models/video');

//const db = "mongodb://hisan:[email protected]:21339/videoplayer";
const db = "mongodb://localhost:27017/library";
mongoose.Promise = global.Promise;

mongoose.connect(db, function(err){
if(err)
{
console.error("Error : " + err);
}
});

router.get('/videos', function(req, res){
console.log("Get request for all videos");
Video.find({})
.exec(function(err, videos){
if(err)
{
console.log("Error retrieving videos");
}
else
{
res.json(videos);
}
});
});

router.get('/videos/:id', function(req, res){
console.log("Get request for a single video");
Video.findById(req.params.id)
.exec(function(err, video){
if(err)
{
console.log("Error retrieving video");
}
else
{
res.json(video);
}
});
});

router.post('/video', function(req, res){
console.log("Post a Video");
var newVideo = new Video();
newVideo.title = req.body.title;
newVideo.url = req.body.url;
newVideo.description = req.body.description;
newVideo.save(function(err, insertedVideo){
if(err)
{
console.log("Error saving video");
}
else
{
res.json(insertedVideo);
}
});
});

router.put('/video/:id', function(req, res){
console.log("Update a Video");
Video.findByIdAndUpdate(req.params.id,
{
$set: {title: req.body.title, url: req.body.url, description: req.body.description}
},
{
new: true
},
function(err, insertedVideo)
{
if(err)
{
res.send("Error Updating video");
}
else
{
res.json(insertedVideo);
}
}
);
});

router.delete('/video/:id', function(req, res){
console.log("Delete a Video");
Video.findByIdAndRemove(req.params.id,
function(err, deletedVideo)
{
if(err)
{
res.send("Error Deleting video");
}
else
{
res.json(deletedVideo);
}
}
);
});

module.exports = router;
4 changes: 2 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { VideoCenterComponent } from './video-center/video-center.component';
import { BooksComponent } from './books/books.component';

const routes: Routes = [
{path: '', redirectTo:'/home', pathMatch:'full'},
{path: 'home', component: HomeComponent},
{path: 'videos', component: VideoCenterComponent}
{path: 'books', component: BooksComponent}
];

@NgModule({
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular bl
</div>
<ul class="nav navbar-nav">
<li><a routerLink="/home" routerLinkActive="active">Home</a></li>
<li><a routerLink="/videos" routerLinkActive="active">Playlist</a></li>
<li><a routerLink="/books" routerLinkActive="active">Books</a></li>
</ul>
</div>
</nav>
Expand Down
6 changes: 5 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { VideoCenterComponent } from './video-center/video-center.component';
import { VideoListComponent } from './video-list/video-list.component';
import { VideoDetailComponent } from './video-detail/video-detail.component';
import { SafePipe } from './safe.pipe';
import { LabCenterComponent } from './lab-center/lab-center.component';
import { BooksComponent } from './books/books.component';

@NgModule({
declarations: [
Expand All @@ -18,7 +20,9 @@ import { SafePipe } from './safe.pipe';
VideoCenterComponent,
VideoListComponent,
VideoDetailComponent,
SafePipe
SafePipe,
LabCenterComponent,
BooksComponent
],
imports: [
BrowserModule,
Expand Down
Empty file.
25 changes: 25 additions & 0 deletions src/app/books/books.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="row">
<div class = "col-md-12" id="loadSection">
<div class="table-responsive">
<!-- <button type="button" class="btn btn-md main-color-bg" data-toggle="modal" data-target="#addItem">
<span class="glyphicon glyphicon-plus-sign"> Add
</button> -->
<br><br>
<table class="table table-bordered" id="item_table">
<thead>
<tr>
<td width="20%">Name</td>
<td width="40%">Description</td>
<td width="10%">Price (Rs)</td>
<td width="10%">Image</td>
<td width="10%"></td>
<td width="10%"></td>
</tr>
</thead>
<tbody>

</tbody>
</table>
</div>
</div>
</div>
25 changes: 25 additions & 0 deletions src/app/books/books.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { BooksComponent } from './books.component';

describe('BooksComponent', () => {
let component: BooksComponent;
let fixture: ComponentFixture<BooksComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BooksComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(BooksComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/books/books.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'books',
templateUrl: './books.component.html',
styleUrls: ['./books.component.css']
})
export class BooksComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}
Empty file.
28 changes: 28 additions & 0 deletions src/app/lab-center/lab-center.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="row">
<div class="col-sm-9">
<div *ngIf= "!hidenewLab">
<h2>New Lab</h2>
<form #form="ngForm" (ngSubmit)= "onSubmitAddLab(form.value)" class="well">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" required name="name" ngModel>
</div>
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" required name="description" ngModel>
</div>
<div class="form-group">
<label>Location</label>
<input type="text" class="form-control" name="location" ngModel>
</div>
<button type="submit" class="btn btn-success">Save</button>
</form>
</div>
<video-detail (updateLabEvent)="onUpdateLabEvent($event)" (deleteLabEvent)="onDeleteLabEvent($event)" *ngIf="selectedLab && hidenewLab" [video]="selectedVideo"></video-detail>
</div>
<div class="col-sm-3">
<button style="margin-bottom:2px;" (click)="newVideo()" type="button" class="btn btn-success btn-block">+ New Video</button>
<video-list (SelectVideo)="onSelectVideo($event)" [videos]="videos"></video-list>
</div>
</div>

Loading

0 comments on commit 7b33c04

Please sign in to comment.