Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (99 sloc) 3.246 kb
36a6cf3 Restrict access
Anatoliy Chakkaev authored
1 load 'application'
2
3 before use 'userRequired'
4
65125d2 Initial release
Anatoliy Chakkaev authored
5 loadPage = ->
6 Page.findById req.params.id, (err, page) =>
7 if err or !page
8 redirect path_to.pages
9 else
10 @page = page
11 next()
12
13 before loadPage,
14 only: ['show', 'edit', 'update', 'destroy']
15
16 action 'new', ->
17 @page = new Page
18 @page.path = req.param('path') if req.param('path')?
19
20 render title: 'New page'
21
22 action 'create', ->
23 if Page.index[req.body.path]
24 flash 'error', 'Page for this path already exists'
25 render 'new', title: 'New page'
26 else
27 @page = new Page
28 @page["title"] = req.body["title"]
29 @page["content"] = req.body["content"]
30 @page["path"] = req.body["path"]
31 @page.save (errors) =>
32 if errors
33 flash 'error', 'Page can not be created'
34 render 'new', title: 'New page'
35 else
36 flash 'info', 'Page created'
37 Page.buildIndex =>
38 redirect @page.path
39
40 action 'index', ->
41 @pages = Page.getSorted()
42
43 if req.params.format == 'json'
44 send @pages
45 else
46 render
47 title: 'Pages index'
48
49 action 'show', ->
50 render title: 'Page show'
51
52 action 'edit', ->
53 render title: 'Page edit'
54
55 action 'update', ->
56 if Page.index[req.body.path] && Page.index[req.body.path].id != @page.id
57 flash 'error', 'Page can not be updated'
58 render 'edit', title: 'Edit page details'
59 else
60 @page["title"] = req.body["title"]
61 @page["content"] = req.body["content"]
62 @page["path"] = req.body["path"]
63 @page.save (err) =>
64 if !err
65 flash 'info', 'Page updated'
66 Page.buildIndex =>
67 redirect @page.path
68 else
69 flash 'error', 'Page can not be updated'
70 render 'edit', title: 'Edit page details'
71
72 action 'destroy', ->
73 @page.remove (error) ->
74 if error
75 flash 'error', 'Can not destroy page'
76 else
77 flash 'info', 'Page successfully removed'
78
79 Page.buildIndex () ->
80 send "'" + path_to.pages + "'"
81
82 action 'reorder', ->
83 wait = 0
84 req.body.order.forEach (path, index) ->
85 wait += 1
86 Page.index[path].pos = index
87 Page.index[path].save ->
88 if --wait == 0
89 Page.buildIndex ->
90 Page.getSorted().forEach (p) ->
91 console.log p.pos
92 send 'ok'
66a87a5 Added dump
Anatoliy Chakkaev authored
93
94 action 'backup', ->
95 require('fs').writeFileSync(app.root + '/db/dump.json', JSON.stringify(Page.getSorted().map (page) -> page.doc))
96 send 'ok'
97
98 action 'restore', ->
99 wait = 0
100 waitSaving = 0
101 saved = ->
102 if --waitSaving == 0
103 send '0k'
104 done = ->
105 if --wait == 0
106 pages = JSON.parse(require('fs').readFileSync(app.root + '/db/dump.json').toString('utf8'))
107 pages.forEach (page) ->
108 delete page._id
109 p = new Page(page)
110 waitSaving += 1
111 p.save(saved)
112
113 Page.find (err, pages) ->
114 pages.forEach (page) ->
115 wait += 1
116 page.remove(done)
Something went wrong with that request. Please try again.