diff --git a/webapp/controllers/index.js b/webapp/controllers/index.js index 468fdde..b038e6b 100644 --- a/webapp/controllers/index.js +++ b/webapp/controllers/index.js @@ -18,5 +18,6 @@ router.get("/", function(req, res) { }) router.use(require("./mods")) +router.use("/workers", require("./workerapi")) module.exports = router diff --git a/webapp/controllers/workerapi.js b/webapp/controllers/workerapi.js new file mode 100644 index 0000000..0d8f749 --- /dev/null +++ b/webapp/controllers/workerapi.js @@ -0,0 +1,36 @@ +"use strict" + +const express = require("express") +const router = express.Router() + +router.get("/get-work", function(req, res) { + var worker_token = req.query.token + var db = req.app.get("db") + db.Worker.findOne({ + where: { + token: worker_token + } + }).then(function(worker) { + db.Work.findAll({ + limit: 1, + where: { + workerId: null + } + }).then(function(jobs) { + if (jobs && jobs.length == 1) { + var job = jobs[0] + job.workerId = worker.id + job.save() + res.send({ + author: job.author, + basename: job.basename, + type: job.work_type + }) + } else { + res.send({}) + } + }) + }) +}) + +module.exports = router diff --git a/webapp/models/database.js b/webapp/models/database.js index c5ed06d..74120dc 100644 --- a/webapp/models/database.js +++ b/webapp/models/database.js @@ -38,6 +38,18 @@ var Mod = sequelize.define("mod", { User.hasMany(Mod) Mod.belongsTo(User) +var Worker = sequelize.define("worker", { + token: Sequelize.STRING(128) +}) + +var Work = sequelize.define("work", { + author: Sequelize.STRING(100), + basename: Sequelize.STRING(100), + work_type: Sequelize.ENUM("fetch", "scan", "forum") +}) + +Work.belongsTo(Worker) + const CMod = require("./../../common/mod") function convertRowToMod(row) { var mod = new CMod(row.user.username) @@ -64,6 +76,8 @@ function convertRowToMod(row) { async.parallel([ function(callback) { User.sync().then(callback) }, function(callback) { Mod.sync().then(callback) }, + function(callback) { Worker.sync().then(callback) }, + function(callback) { Work.sync().then(callback) }, function() { User.findOrCreate({ where: { @@ -98,7 +112,26 @@ async.parallel([ approved: true } }).then(function(mod) { + Worker.findOrCreate({ + where: { + token: "foobar" + }, + defaults: {} + }).then(function(worker) { + worker = worker[0] + Work.findOrCreate({ + where: { + author: "rubenwardy", + basename: "awards", + }, + defaults: { + work_type: "fetch" + } + }).then(function(mod) { + + }) + }) }) }) }]) @@ -106,5 +139,7 @@ async.parallel([ module.exports = { User: User, Mod: Mod, + Work: Work, + Worker: Worker, convertRowToMod: convertRowToMod }