Skip to content

Commit

Permalink
zoobot/feature/data-sources
Browse files Browse the repository at this point in the history
  • Loading branch information
zoobot committed Feb 14, 2023
1 parent 2a620dd commit e9ead86
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
31 changes: 24 additions & 7 deletions server/routes/sources/sources-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import convertObjectKeysToSnakeCase from '../shared-routes-utils.js';
export async function createSource(data) {
// eslint-disable-next-line no-unused-vars
const { crosswalk, destinations, ...source } = data;
console.log('createSource source', source);

const query = `
INSERT INTO sources(\${this:name})
Expand All @@ -16,6 +17,7 @@ export async function createSource(data) {
}

export async function createCrosswalk(data) {
console.log('createCrosswalk data', data);
const query = `
INSERT INTO crosswalk(\${this:name})
VALUES(\${this:csv})
Expand Down Expand Up @@ -66,18 +68,33 @@ export async function getSourceById(id) {
return source;
}

export async function updateSourceById(updatedSourceData, id) {
const updatedSourceDataInSnakeCase =
convertObjectKeysToSnakeCase(updatedSourceData);
export async function updateSourceById(data) {
const dataInSnakeCase = convertObjectKeysToSnakeCase(data);

const condition = pgPromise.as.format(`WHERE id = ${id} RETURNING *`);
const condition = pgPromise.as.format(` WHERE id = '${data.id}' RETURNING *`);
const query =
pgPromise.helpers.update(
updatedSourceDataInSnakeCase,
Object.keys(updatedSourceDataInSnakeCase),
dataInSnakeCase,
Object.keys(dataInSnakeCase),
'sources',
) + condition;
const updatedSource = await db.one(query, updatedSourceDataInSnakeCase);
const updatedSource = await db.one(query, dataInSnakeCase);

return updatedSource;
}

export async function updateCrosswalkById(data, id) {
console.log('updateCrosswalkById data', data);
const dataInSnakeCase = convertObjectKeysToSnakeCase(data);

const condition = pgPromise.as.format(` WHERE id = '${id}' RETURNING *`);
const query =
pgPromise.helpers.update(
dataInSnakeCase,
Object.keys(dataInSnakeCase),
'crosswalk',
) + condition;
const updatedSource = await db.one(query, dataInSnakeCase);

return updatedSource;
}
64 changes: 22 additions & 42 deletions server/routes/sources/sources-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import {
createSource,
createCrosswalk,
getAllSources,
updateCrosswalkById,
} from './sources-queries.js';

const sourcesRouter = express.Router();

sourcesRouter.get('/', async (req, res) => {
const { id, country, source } = req.query;
console.log('req.query', req.query);

if (source === 'All') {
const sources = await getAllSources();
res.status(200).json(sources ?? {});
const source = await getAllSources();
res.status(200).json(source ?? {});
}

if (id) {
Expand Down Expand Up @@ -59,28 +61,10 @@ sourcesRouter.get('/', async (req, res) => {
sourcesRouter.post('/', async (req, res) => {
// eslint-disable-next-line no-unused-vars
const { crosswalk, source } = req.body;
const { id } = source;
const foundSource = await getSourceById(id);

const response = {};
if (foundSource) {
const updatedSource = await updateSourceById(source);
if (!updatedSource) throw new AppError(400, 'Error creating source');
response.source = await updatedSource;
console.log('sourcesRouter.post source', source);
console.log('sourcesRouter.post crosswalk', crosswalk);

if (crosswalk) {
const responseCrosswalk = await updateSourceById({
id,
...crosswalk,
});

if (!responseCrosswalk)
throw new AppError(400, 'Error creating Crosswalk');
response.crosswalk = await responseCrosswalk;
}
}

const responseSource = await createSource(source);
const response = await createSource(source);
if (!response) throw new AppError(400, 'Error creating source');
if (crosswalk) {
const responseCrosswalk = await createCrosswalk({
Expand All @@ -90,33 +74,29 @@ sourcesRouter.post('/', async (req, res) => {
if (!responseCrosswalk) throw new AppError(400, 'Error creating Crosswalk');
}

res.status(200).json(responseSource ?? { message: 'No data to update' });
res.status(200).json(response ?? { message: 'No data to update' });
});

sourcesRouter.put('/', async (req, res) => {
// eslint-disable-next-line no-unused-vars
const { crosswalk, source } = req.body;
const { id } = source;
const foundSource = await getSourceById(id);
//validate source and crosswalk
console.log('sourcesRouter.put source', source);
console.log('sourcesRouter.put crosswalk', crosswalk);

const response = {};
if (foundSource) {
const responseSource = await updateSourceById(source);
if (!responseSource) throw new AppError(400, 'Error creating source');
response.source = await responseSource;
const response = await updateSourceById(source);
console.log('sourcesRouter.put response', response);
if (!response) throw new AppError(400, 'Error creating source');

if (crosswalk) {
const responseCrosswalk = await updateSourceById({
id,
...crosswalk,
});
const responseCrosswalk = await updateCrosswalkById(crosswalk, source.id);

if (!responseCrosswalk)
throw new AppError(400, 'Error creating Crosswalk');
response.crosswalk = await responseCrosswalk;
}
res.status(200).json(responseSource ?? { message: 'No data to update' });
}
if (!responseCrosswalk) throw new AppError(400, 'Error creating Crosswalk');

res.status(200).json(
{ response, ...{ crosswalk: responseCrosswalk } } ?? {
message: 'No data to update',
},
);
});

export default sourcesRouter;

0 comments on commit e9ead86

Please sign in to comment.