Implemented username reserve feature

This commit is contained in:
2021-03-03 04:24:33 +11:00
parent 3d4bae1281
commit e3ef30c10c
7 changed files with 101 additions and 6 deletions
+2 -5
View File
@@ -1,10 +1,7 @@
const express = require('express');
const router = express.Router();
//the routes
//TODO: import the routes here
//basic route management
//TODO: define the routes here
//reserve the name using a pseudonym
router.post('/reserve', require('./reserve'));
module.exports = router;
+34
View File
@@ -0,0 +1,34 @@
const crypto = require("crypto");
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const { pseudonyms } = require('../database/models');
const route = async (req, res) => {
//generate a UUID to act as a pseudonym (starting with a period)
const pseudonym = `.${uuid()}`;
//find or create the record (using the username)
const [instance, created] = await pseudonyms.findOrCreate({
where: {
username: req.fields.username
}
});
//save the pseudonym (overwriting existing pseudonyms)
const result = await pseudonyms.update({
pseudonym: pseudonym
},{
where: {
username: req.fields.username
}
});
//OK
return res.status(200).send({ pseudonym: pseudonym });
};
//lazy
const uuid = (bytes = 16) => crypto.randomBytes(bytes).toString("hex");
module.exports = route;
+1 -1
View File
@@ -1,3 +1,3 @@
module.exports = {
//TODO: models
pseudonyms: require('./pseudonyms')
};
+28
View File
@@ -0,0 +1,28 @@
const Sequelize = require('sequelize');
const sequelize = require('..');
module.exports = sequelize.define('pseudonyms', {
id: {
type: Sequelize.INTEGER(11),
allowNull: false,
autoIncrement: true,
primaryKey: true,
unique: true
},
username: {
type: 'varchar(320)',
unique: true
},
pseudonym: {
type: 'varchar(320)',
unique: true
},
deletion: {
type: 'DATETIME',
allowNull: true,
defaultValue: null
}
});
+2
View File
@@ -5,10 +5,12 @@ require('dotenv').config();
const express = require('express');
const app = express();
const server = require('http').Server(app);
const formidable = require('express-formidable');
const bodyParser = require('body-parser');
const cors = require('cors');
//config
app.use(formidable());
app.use(bodyParser.json());
app.use(cors());