Node.js 相近实战(三) 之图书管理系统(图书信息录入)
Node.js 切近实战(三) 之图书管理系统(图书信息录入)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
var mongoose = require( 'mongoose' );
var Schema = mongoose.Schema;
var bookSchema = new Schema({
_id: Schema.Types.ObjectId,
Title: { type: String, index: true , required: true },
ISBN: { type: String, required: true , unique: true },
Author: { type: String },
Price: { type: Number, min: 0.1, max: 99999 },
Press: { type: String, required: true },
PressDate: { type: Date, default : Date.now },
Description: { type: String, required: true },
Image: { type: Schema.ObjectId }
}, { strict: true ,
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
}); bookSchema.virtual( 'bookFullName' ).get( function () {
return this .title + '(' + ( this .author? this .author: '' ) + ')' ;
}); bookSchema.methods.saveBookInfo = function (callback) {
this ._id = new mongoose.Types.ObjectId;
this .save(callback);
} bookSchema.set( 'collection' , 'books' );
var bookModel = mongoose.model( "books" , bookSchema);
exports.bookModel = bookModel; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env node var debug = require( 'debug' )( 'ChinaBook' );
var mongoose = require( 'mongoose' );
initMongoose(); var app = require( '../app' );
app.set( 'port' , process.env.PORT || 3000);
var server = app.listen(app.get( 'port' ), function () {
debug( 'Express server listening on port ' + server.address().port);
}); function initMongoose(){
mongoose.connect( 'localhost:27017/ChinaBook' , function (err, db){
if (err) {
console.log( 'Unable to connect to the mongoDB server. Error:' , err);
} else {
console.log( 'Connection established to' , 'localhost:27017/ChinaBook' );
}
});
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
var bookSchemas = require( '../model/bookinfo.js' );
var bookMsgRes = require( '../framework/message/book_msg.js' );
var validator = require( 'validator' );
var mongoose = require( 'mongoose' );
var bookModel = bookSchemas.bookModel;
exports.bookSave = function (req, res) {
if (validator.isNull(req.body.Title)) {
res.json(bookMsgRes.buildJsonErrorRes( 'BookTitleEmpty' ));
return ;
}
if (!validator.isISBN(req.body.ISBN)) {
res.json(bookMsgRes.buildJsonErrorRes( 'ISBNInCorrect' ));
return ;
}
if (!validator.isFloat(req.body.Price, { min: 0.01, max: 999999 })) {
res.json(bookMsgRes.buildJsonErrorRes( 'PriceInCorrect' ));
return ;
}
if (validator.isNull(req.body.Press)) {
res.json(bookMsgRes.buildJsonErrorRes( 'PressEmpty' ));
return ;
}
if (validator.isNull(req.body.Description)) {
res.json(bookMsgRes.buildJsonErrorRes( 'DescriptionEmpty' ));
return ;
}
var bookEntity = new bookModel(req.body);
bookEntity.saveBookInfo( function (error, doc) {
if (error) {
res.json({ isSuc: false , msg: error.message });
} else {
res.json({ isSuc: true });
}
});
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var msg = {
TitleEmpty: 'Book Title Can\'t be empty!' ,
ISBNInCorrect: 'ISBN is incorrect!' ,
PriceInCorrect: 'The price is incorrect,it must bwtween 0.1 and 99999!' ,
PressEmpty: 'Press can\'t be empty!' ,
DescriptionEmpty: 'Description can\'t be empty!'
} exports.buildJsonErrorRes = function (key) { return { isSuc: false , msg: msg[key] };
} exports.buildJsonSucRes = function (key) { return { isSuc: false , msg: msg[key] };
} |
1
2
|
var router = express.Router();
router.post( '/book' , bookRoutes.bookSave);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#book_typeIn(ng-controller='bookTypeInCtrl') label(style='font-size:17px') Book Info:
hr.panel-line
form(name='bookForm')
.row.row-middle-height
.col-md-12
.col-md-1
label Title:
.col-md-5
input.form-control(name='title' type='text' maxlength='50' placeholder='Book name' ng-model='Book.Title' required)
.col-md-6
p.error-message(ng-show='submitted && bookForm.title.$error.required') Title can't be empty.
.row.row-margin.row-middle-height
.col-md-12
.col-md-1
label ISBN:
.col-md-5
input.form-control(name='ISBN' type='text' ng-model='Book.ISBN' maxlength ='30' required)
.col-md-6
p.error-message(ng-show='submitted && bookForm.ISBN.$error.required') ISBN can't be empty.
.row.row-margin.row-middle-height
.col-md-12
.col-md-1
label Author:
.col-md-5
input.form-control(type='text' maxlength ='30' ng-model='Book.Author')
.row.row-margin.row-middle-height
.col-md-12
.col-md-1
label Price:
.col-md-5
input.form-control.tooltip-show(name='price' type='text' maxlength='10' ng-model='Book.Price' data-toggle='tooltip' data-placement='top' ng-pattern='/^[0-9]+(.[0-9]{2})?$/' title='Two decimal places')
.col-md-6
p.error-message(ng-show='submitted && bookForm.price.$error.pattern') Price is incorrect.
.row.row-margin.row-middle-height
.col-md-12
.col-md-1
label Press:
.col-md-5
input.form-control(name='press' type='text' maxlength='50' ng-model='Book.Press' required)
.col-md-6
p.error-message(ng-show='submitted && bookForm.press.$error.required') Press can't be empty.
.row.row-margin.row-middle-height
.col-md-12
.col-md-1
label PressDate:
.col-md-5
kendo-date-picker(name='pressDate' ng-model='Book.PressDate' k-format='yyyy-MM-dd' onkeydown='return false;' required)
.col-md-6
p.error-message(ng-show='submitted && bookForm.pressDate.$error.required') PressDate can't be empty.
.row.row-margin.row-middle-height
.col-md-12
.col-md-1
label Description:
.col-md-5
input.form-control(name='description' type='text' maxlength='200' ng-model='Book.Description' required)
.col-md-6
p.error-message(ng-show='submitted && bookForm.description.$error.required') Description can't be empty.
.row-margin
button.k-button.k-primary.btn-width-70(type='button' ng-click='bookInfoSave()') Save
button.k-button.k-primary.btn-width-70(type='button' ng-click='bookInfoReset()' style='margin-left:10px') Reset
block scripts
script(type='text/javascript' src='/javascripts/local/book/bookTypeIn.js')
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
var appModule = angular.module( 'bookTypeInModule' , [ "kendo.directives" ]);
appModule.config( function ($locationProvider) {
$locationProvider.html5Mode( true );
}); appModule.controller( 'bookTypeInCtrl' , function ($scope, $http, $location) {
angular.element( '.tooltip-show' ).tooltip( 'show' );
Messenger.options = {
extraClasses: 'messenger-fixed messenger-on-top messenger-on-center' ,
theme: 'flat'
}
var now = new Date();
$scope.Book = {};
$scope.bookInfoSave = function () {
$scope.submitted = true ;
if (!$scope.bookForm.$valid) {
return ;
}
$http({
method: "post" ,
url: "/book" ,
headers: { 'Content-Type' : 'application/json' },
data: $scope.Book
}).success( function (data) {
if (data.isSuc) {
$scope.bookInfoReset();
$scope.showMsg( 'success' , 'Saved successfully!' );
}
else {
$scope.showMsg( 'error' , data.msg);
}
});
}
$scope.bookInfoReset = function () {
$scope.Book = {};
$scope.submitted = false ;
}
$scope.showMsg = function (type, msg) {
Messenger().post({
message: msg,
type: type,
hideAfter: 2,
showCloseButton: true
});
}
$scope.onError = function (error) {
$scope.UploadError = error;
}
}); angular.element( '#book_typeIn' ).data( '$injector' , '' );
angular.bootstrap(angular.element( '#book_typeIn' ), [ 'bookTypeInModule' ]);
|