Skip to content

Commit 6b4ceee

Browse files
committed
Refactor server creation, updating and deletion via API
Fixes accidental duplicate servers
1 parent 2415302 commit 6b4ceee

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

Diff for: lib/manager.js

+32-9
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,32 @@ var Manager = function (config, logs) {
1414

1515
Manager.prototype = new events.EventEmitter()
1616

17-
Manager.prototype.addServer = function (options) {
17+
Manager.prototype.addServer = function (options, cb) {
18+
if (manager.getServer(Server.generateId(options.title))) {
19+
return cb(new Error('Server already exists'))
20+
}
21+
1822
var server = this._addServer(options)
19-
this.save()
20-
return server
23+
this.save(function (err) {
24+
cb(err, server)
25+
})
2126
}
2227

23-
Manager.prototype.removeServer = function (id) {
28+
Manager.prototype.updateServer = function (id, options, cb) {
29+
var server = manager.getServer(id)
30+
var anotherServer = manager.getServer(Server.generateId(options.title))
31+
32+
if (anotherServer !== null && anotherServer !== server) {
33+
return cb(new Error('Server already exists'))
34+
}
35+
36+
server.update(req.body)
37+
this.save(function (err) {
38+
cb(err, server)
39+
})
40+
}
41+
42+
Manager.prototype.removeServer = function (id, cb) {
2443
var server = this.serversHash[id]
2544

2645
if (!server) {
@@ -31,13 +50,14 @@ Manager.prototype.removeServer = function (id) {
3150
if (index > -1) {
3251
this.serversArr.splice(index, 1)
3352
}
34-
this.save()
3553

3654
if (server.pid) {
3755
server.stop()
3856
}
3957

40-
return server
58+
this.save(function (err) {
59+
cb(err, server)
60+
})
4161
}
4262

4363
Manager.prototype._addServer = function (data) {
@@ -90,7 +110,7 @@ Manager.prototype.load = function () {
90110
})
91111
}
92112

93-
Manager.prototype.save = function () {
113+
Manager.prototype.save = function (cb) {
94114
var data = []
95115
var self = this
96116

@@ -128,9 +148,12 @@ Manager.prototype.save = function () {
128148
fs.writeFile(filePath, JSON.stringify(data), function (err) {
129149
if (err) {
130150
console.error('Manager save error: ' + err)
131-
} else {
132-
self.emit('servers')
151+
cb(err)
152+
return
133153
}
154+
155+
self.emit('servers')
156+
cb()
134157
})
135158
}
136159

Diff for: lib/server.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ var Server = function (config, logs, options) {
2323
this.update(options)
2424
}
2525

26+
Server.generateId = function (title) {
27+
return slugify(title)
28+
.replace(/[\(|\)]/g, '')
29+
.replace(/\./g, '-')
30+
}
31+
2632
Server.prototype = new events.EventEmitter()
2733

2834
Server.prototype.createServerTitle = function (title) {
@@ -37,10 +43,6 @@ Server.prototype.createServerTitle = function (title) {
3743
return title
3844
}
3945

40-
Server.prototype.generateId = function () {
41-
return slugify(this.title).replace(/\./g, '-')
42-
}
43-
4446
Server.prototype.update = function (options) {
4547
this.additionalConfigurationOptions = options.additionalConfigurationOptions
4648
this.admin_password = options.admin_password
@@ -62,7 +64,7 @@ Server.prototype.update = function (options) {
6264
this.von = options.von
6365
this.verify_signatures = options.verify_signatures
6466

65-
this.id = this.generateId()
67+
this.id = Server.generateId(this.title)
6668
this.port = parseInt(this.port, 10) // If port is a string then gamedig fails
6769
}
6870

Diff for: routes/servers.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ module.exports = function (manager, mods) {
1313
return
1414
}
1515

16-
var server = manager.addServer(req.body)
17-
res.json(server)
16+
manager.addServer(req.body, function (err, server) {
17+
if (err) {
18+
return res.status(500).send(err)
19+
}
20+
21+
res.status(201).send(server)
22+
})
1823
})
1924

2025
router.get('/:server', function (req, res) {
@@ -28,15 +33,23 @@ module.exports = function (manager, mods) {
2833
return
2934
}
3035

31-
var server = manager.getServer(req.params.server)
32-
server.update(req.body)
33-
manager.save()
34-
res.json(server)
36+
manager.update(req.params.server, req.body, function (err, server) {
37+
if (err) {
38+
return res.status(500).send(err)
39+
}
40+
41+
res.status(200).send(server)
42+
})
3543
})
3644

3745
router.delete('/:server', function (req, res) {
38-
var server = manager.removeServer(req.params.server)
39-
res.json(server)
46+
manager.removeServer(req.params.server, function (err, server) {
47+
if (err) {
48+
return res.status(500).send(err)
49+
}
50+
51+
res.status(204).send()
52+
})
4053
})
4154

4255
router.post('/:server/start', function (req, res) {

Diff for: test/lib/server.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ var Server = require('../../lib/server.js')
44

55
describe('Server', function () {
66
describe('generateId()', function () {
7-
it('should include title', function () {
8-
var server = new Server(null, null, { title: 'title.with.lot.of.dots' })
9-
server.generateId().should.eql('title-with-lot-of-dots')
7+
it('should generate id for title with dots', function () {
8+
Server.generateId('title.with.lot.of.dots').should.eql('title-with-lot-of-dots')
9+
})
10+
11+
it('should generate id title with brackets', function () {
12+
Server.generateId('title [with] [lots of] [brackets]').should.eql('title-with-lots-of-brackets')
13+
})
14+
15+
it('should generate id title with parentheses', function () {
16+
Server.generateId('title (with) (lots of) (parentheses)').should.eql('title-with-lots-of-parentheses')
1017
})
1118
})
1219

0 commit comments

Comments
 (0)