diff --git a/server/models/book.js b/server/models/book.js new file mode 100644 index 0000000..57c770f --- /dev/null +++ b/server/models/book.js @@ -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'); \ No newline at end of file diff --git a/server/models/lab.js b/server/models/lab.js new file mode 100644 index 0000000..fb51e1d --- /dev/null +++ b/server/models/lab.js @@ -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'); \ No newline at end of file diff --git a/server/routes/api.js b/server/routes/api.js index 56c3ccf..e8c5409 100644 --- a/server/routes/api.js +++ b/server/routes/api.js @@ -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:Sacrificer1!@ds221339.mlab.com:21339/videoplayer"; +//const db = "mongodb://hisan:Sacrificer1!@ds221339.mlab.com:21339/videoplayer"; +const db = "mongodb://localhost:27017/library"; mongoose.Promise = global.Promise; mongoose.connect(db, function(err){ @@ -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); } }); }); diff --git a/server/routes/api2.js b/server/routes/api2.js new file mode 100644 index 0000000..454d622 --- /dev/null +++ b/server/routes/api2.js @@ -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:Sacrificer1!@ds221339.mlab.com: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; \ No newline at end of file diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 35ce8ac..06a3188 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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({ diff --git a/src/app/app.component.html b/src/app/app.component.html index 04a9020..c5c1cf9 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -25,7 +25,7 @@
Name | +Description | +Price (Rs) | +Image | ++ | + |