-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.coffee
55 lines (43 loc) · 1.55 KB
/
db.coffee
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
mongo = require 'mongoskin'
db = mongo.db '127.0.0.1:27017/sakuratya?auto_reconnect=true&poolSize=10',safe:true
global.CONFIG = JSON.parse(require('fs').readFileSync 'config.json')
User = require './classes/user'
bcrypt = require 'bcrypt'
db.bind 'posts',
insertNew:(post,fn)->
doc =
title:post.title
content:post.content
tags:post.tags
view_count:mongo.Long.fromInt 0
fav_count:mongo.Long.fromInt 0
comm_count:mongo.Long.fromInt 0
doc.test = post.test if post.test?
this.insert doc,fn
db.bind 'users',
create:(user,fn)->
doc =
email:user.email
disp_name:user.display_name
info:{}
doc.test = true if user.test?
doc.group = 'user'
doc.group = user.group if user.group?
bcrypt.hash user.password,10,(err,hash)=>
if err
return process.nextTick ->fn err
doc.passwd = hash
this.insert doc,fn
login:(email,pass,fn)->
this.find({email}).toArray (err,user)->
if err
return process.nextTick ->fn(err)
if user.length is 0
return process.nextTick ->fn(null,'not_found')
bcrypt.compare pass,user[0].passwd,(err,res)->
if err
return process.nextTick ->fn(err)
if !res
return process.nextTick ->fn null,'mismatch'
process.nextTick ->fn null,new User user[0]
module.exports = db