Skip to content

Commit

Permalink
feat: change post endpoint functionality to upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Nov 14, 2023
1 parent 52483c2 commit 33ff220
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 33 deletions.
53 changes: 53 additions & 0 deletions database/migrations/20231113105802-add-ref-id-unique-constraint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231113105802-add-ref-id-unique-constraint-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231113105802-add-ref-id-unique-constraint-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX ref_id_key;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX "ref_id_key" ON "config"("ref_id");
15 changes: 3 additions & 12 deletions server/handlers/config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ConfigService = require('../../services/ConfigService');
const HttpError = require('../../utils/HttpError');
const { configPostSchema, configPatchSchema } = require('./schemas');
const { configPostSchema } = require('./schemas');

const configGetHandler = async (req, res) => {
const configService = new ConfigService();
Expand All @@ -16,21 +16,12 @@ const configGetHandler = async (req, res) => {
const configPostHandler = async (req, res) => {
await configPostSchema.validateAsync(req.body, { abortEarly: false });
const configService = new ConfigService();
await configService.createConfigs(req.body);
const config = await configService.createConfigs(req.body);

res.status(200).json({ ok: true });
};

const configPatchHandler = async (req, res) => {
await configPatchSchema.validateAsync(req.body, { abortEarly: false });
const configService = new ConfigService();
await configService.patchConfigs(req.body);

res.status(200).json({ ok: true });
res.status(200).json({ config });
};

module.exports = {
configGetHandler,
configPostHandler,
configPatchHandler,
};
7 changes: 1 addition & 6 deletions server/handlers/config/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@ const configPostSchema = Joi.object({
ref_uuid: Joi.string().uuid(),
});

const configPatchSchema = Joi.object({
id: Joi.string().required(),
data: Joi.object().required(),
});

module.exports = { configPostSchema, configPatchSchema };
module.exports = { configPostSchema };
4 changes: 0 additions & 4 deletions server/models/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class Config {
async createConfigs(config) {
return this._configRepository.create(config);
}

async patchConfigs(config) {
return this._configRepository.update(config);
}
}

module.exports = Config;
11 changes: 11 additions & 0 deletions server/repositories/ConfigRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ class ConfigRepository extends BaseRepository {
constructor(session) {
super('config', session);
}

async create(object) {
const result = await this._session
.getDB()(this._tableName)
.insert(object)
.onConflict('ref_id')
.merge('ref_uuid', 'name', 'data')
.returning('*');

return result[0];
}
}

module.exports = ConfigRepository;
9 changes: 2 additions & 7 deletions server/routes/configRoutes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
const express = require('express');

const router = express.Router();
const {
configGetHandler,
configPostHandler,
configPatchHandler,
} = require('../handlers/config');
const { configGetHandler, configPostHandler } = require('../handlers/config');
const { handlerWrapper } = require('../utils/utils');

router
.route('/config')
.get(handlerWrapper(configGetHandler))
.post(handlerWrapper(configPostHandler))
.patch(handlerWrapper(configPatchHandler));
.post(handlerWrapper(configPostHandler));

module.exports = router;
4 changes: 0 additions & 4 deletions server/services/ConfigService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class ConfigService {
async createConfigs(config) {
return this._config.createConfigs(config);
}

async patchConfigs(config) {
return this._config.patchConfigs(config);
}
}

module.exports = ConfigService;

0 comments on commit 33ff220

Please sign in to comment.