Browse Source
feat: config dao, service, controller and routes feat: manage starting date raid for retreiving bis list feat: fusion of ring and trinket statsmaster
13 changed files with 216 additions and 49 deletions
@ -0,0 +1,27 @@
|
||||
import configService from '../services/config.service.js'; |
||||
import typesError from '../error/types.error.js'; |
||||
|
||||
const getConfig = async (request, reply) => { |
||||
try { |
||||
const config = await configService.getConfig() |
||||
return reply.code(200).send({ config: config.toJson() }) |
||||
} catch (e) { |
||||
return reply.code(500).send({ message: e.message }) |
||||
} |
||||
} |
||||
|
||||
const updateConfig = async (request, reply) => { |
||||
const newConfig = request.body |
||||
try { |
||||
await configService.updateConfig(newConfig) |
||||
return reply.code(200).send({ success: true }) |
||||
} catch (e) { |
||||
return reply.code(500).send({ message: typesError.TECHNICAL_UNKNOWN }) |
||||
} |
||||
} |
||||
|
||||
|
||||
export default { |
||||
getConfig, |
||||
updateConfig |
||||
} |
||||
@ -0,0 +1,76 @@
|
||||
import sqlite3 from 'sqlite3' |
||||
import bcrypt from 'bcryptjs' |
||||
import ErrorType from '../error/types.error.js' |
||||
import serverConfig from '../configuration/server.config.js' |
||||
|
||||
sqlite3.verbose() |
||||
|
||||
const tableName = 'Config' |
||||
|
||||
class Config { |
||||
constructor(options = {}) { |
||||
this.id = options.id |
||||
this.startingDateRaid = options.startingDateRaid |
||||
} |
||||
|
||||
static find() { |
||||
return new Promise((resolve, reject) => { |
||||
const db = openDB() |
||||
db.serialize(() => { |
||||
db.all(`SELECT * FROM ${tableName}`, (err, configs) => { |
||||
if (err) reject(new Error(ErrorType.TECHNICAL_UNKNOWN)) |
||||
else { |
||||
if (configs.length) { |
||||
const config = createConfigFromDB(configs[0]) |
||||
resolve(config); |
||||
} else reject(new Error(ErrorType.TECHNICAL_UNKNOWN)) |
||||
} |
||||
}) |
||||
closeDB(db) |
||||
}) |
||||
}) |
||||
} |
||||
|
||||
async update() { |
||||
return new Promise((resolve, reject) => { |
||||
const db = openDB(); |
||||
db.serialize(() => { |
||||
db.run(`UPDATE ${tableName} SET startingDateRaid = ${this.startingDateRaid} WHERE id = ${this.id}`, err => { |
||||
if (err) reject(new Error(ErrorType.TECHNICAL_UNKNOWN)) |
||||
else resolve() |
||||
}) |
||||
}) |
||||
closeDB(db) |
||||
}) |
||||
} |
||||
|
||||
toJson() { |
||||
return { |
||||
id: this.id, |
||||
startingDateRaid: this.startingDateRaid |
||||
} |
||||
}
|
||||
} |
||||
|
||||
export default Config |
||||
|
||||
function openDB() { |
||||
const env = process.env.NODE_ENV || 'development' |
||||
return new sqlite3.Database(serverConfig.database[env].path, err => { |
||||
if (err) throw new Error(err) |
||||
}) |
||||
} |
||||
|
||||
function closeDB(db) { |
||||
db.close(err => { |
||||
if (err) throw new Error(err) |
||||
}) |
||||
} |
||||
|
||||
function createConfigFromDB(dbUser) { |
||||
return new Config({ |
||||
id: dbUser.id, |
||||
startingDateRaid: dbUser.startingDateRaid |
||||
}) |
||||
} |
||||
|
||||
@ -0,0 +1,22 @@
|
||||
'use strict'; |
||||
|
||||
/** @type {import('sequelize-cli').Migration} */ |
||||
module.exports = { |
||||
async up (queryInterface, Sequelize) { |
||||
await queryInterface.createTable('Config', { |
||||
id: { |
||||
allowNull: false, |
||||
autoIncrement: true, |
||||
primaryKey: true, |
||||
type: Sequelize.INTEGER |
||||
}, |
||||
startingDateRaid: { |
||||
type: Sequelize.INTEGER |
||||
} |
||||
}); |
||||
}, |
||||
|
||||
async down (queryInterface, Sequelize) { |
||||
await queryInterface.dropTable('Config'); |
||||
} |
||||
}; |
||||
@ -0,0 +1,15 @@
|
||||
'use strict'; |
||||
|
||||
/** @type {import('sequelize-cli').Migration} */ |
||||
module.exports = { |
||||
async up (queryInterface, Sequelize) { |
||||
await queryInterface.bulkInsert('Config', [{ |
||||
id: 1, |
||||
startingDateRaid: 1717020000000, |
||||
}], {}) |
||||
}, |
||||
|
||||
async down (queryInterface, Sequelize) { |
||||
await queryInterface.bulkDelete('Config', null, {}) |
||||
} |
||||
}; |
||||
@ -0,0 +1,15 @@
|
||||
'use strict'; |
||||
|
||||
/** @type {import('sequelize-cli').Migration} */ |
||||
module.exports = { |
||||
async up (queryInterface, Sequelize) { |
||||
await queryInterface.bulkInsert('Config', [{ |
||||
id: 1, |
||||
startingDateRaid: 1717020000, |
||||
}], {}) |
||||
}, |
||||
|
||||
async down (queryInterface, Sequelize) { |
||||
await queryInterface.bulkDelete('Config', null, {}) |
||||
} |
||||
}; |
||||
@ -0,0 +1,18 @@
|
||||
import Config from '../dao/config.dao.js' |
||||
|
||||
async function getConfig() { |
||||
return Config.find() |
||||
} |
||||
|
||||
async function updateConfig(options) { |
||||
const config = new Config({ |
||||
id: options.id, |
||||
startingDateRaid: options.startingDateRaid |
||||
}) |
||||
await config.update() |
||||
} |
||||
|
||||
export default { |
||||
getConfig, |
||||
updateConfig |
||||
} |
||||
Loading…
Reference in new issue