From 4234ba6199aa83320ab0d3592cb3461363927acb Mon Sep 17 00:00:00 2001 From: zoobot Date: Tue, 14 Feb 2023 09:35:50 -0800 Subject: [PATCH] change id_name to id_source_name --- server/routes/sources/sources-queries.js | 33 ++++++++++----------- server/routes/sources/sources-router.js | 37 +++++++++++------------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/server/routes/sources/sources-queries.js b/server/routes/sources/sources-queries.js index d9634ce..662d746 100644 --- a/server/routes/sources/sources-queries.js +++ b/server/routes/sources/sources-queries.js @@ -4,12 +4,11 @@ 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}) VALUES(\${this:csv}) - RETURNING country, city, id, created + RETURNING country, city, id_source_name as idSourceName, created `; const response = await db.one(query, source); @@ -17,11 +16,10 @@ 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}) - RETURNING id + RETURNING id_source_name as idSourceName, created `; const response = await db.one(query, data); @@ -30,7 +28,7 @@ export async function createCrosswalk(data) { export async function findSourceCountry(country) { const query = `SELECT - id, iso_alpha_3 as country, state, city, + id_source_name as idSourceName, iso_alpha_3 as country, state, city, email, contact, who as org, phone, info, download, broken_reason as note, broken FROM sources @@ -42,7 +40,7 @@ export async function findSourceCountry(country) { export async function findSourceCity(city) { const query = `SELECT - id, iso_alpha_3 as country, state, city, + id_source_name as idSourceName, iso_alpha_3 as country, state, city, email, contact, who as org, phone, info, download, broken_reason as note, broken FROM sources @@ -53,7 +51,7 @@ export async function findSourceCity(city) { } export async function getAllSources() { - const query = `SELECT id, iso_alpha_3 as country, state, city, + const query = `SELECT id_source_name as idSourceName, iso_alpha_3 as country, state, city, email, contact, who, phone, info, download, broken_reason as note, broken FROM sources;`; @@ -61,17 +59,19 @@ export async function getAllSources() { return source; } -export async function getSourceById(id) { - const query = `SELECT id - FROM sources where id = '${id}';`; +export async function getSourceByIdSourceName(idSourceName) { + const query = `SELECT id_source_name as idSourceName + FROM sources where id_source_name = '${idSourceName}';`; const source = await db.any(query); return source; } -export async function updateSourceById(data) { +export async function updateSourceByIdSourceName(data) { const dataInSnakeCase = convertObjectKeysToSnakeCase(data); - const condition = pgPromise.as.format(` WHERE id = '${data.id}' RETURNING *`); + const condition = pgPromise.as.format( + ` WHERE id_source_name = '${data.idSourceName}' RETURNING *`, + ); const query = pgPromise.helpers.update( dataInSnakeCase, @@ -79,15 +79,15 @@ export async function updateSourceById(data) { 'sources', ) + condition; const updatedSource = await db.one(query, dataInSnakeCase); - return updatedSource; } -export async function updateCrosswalkById(data, id) { - console.log('updateCrosswalkById data', data); +export async function updateCrosswalkByIdSourceName(data, idSourceName) { const dataInSnakeCase = convertObjectKeysToSnakeCase(data); - const condition = pgPromise.as.format(` WHERE id = '${id}' RETURNING *`); + const condition = pgPromise.as.format( + ` WHERE id_source_name = '${idSourceName}' RETURNING *`, + ); const query = pgPromise.helpers.update( dataInSnakeCase, @@ -95,6 +95,5 @@ export async function updateCrosswalkById(data, id) { 'crosswalk', ) + condition; const updatedSource = await db.one(query, dataInSnakeCase); - return updatedSource; } diff --git a/server/routes/sources/sources-router.js b/server/routes/sources/sources-router.js index e9963e0..707f10d 100644 --- a/server/routes/sources/sources-router.js +++ b/server/routes/sources/sources-router.js @@ -2,27 +2,26 @@ import express from 'express'; import AppError from '../../errors/AppError.js'; import { findSourceCountry, - getSourceById, - updateSourceById, + getSourceByIdSourceName, + updateSourceByIdSourceName, createSource, createCrosswalk, getAllSources, - updateCrosswalkById, + updateCrosswalkByIdSourceName, } 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); + const { idSourceName, country, source } = req.query; if (source === 'All') { const source = await getAllSources(); res.status(200).json(source ?? {}); } - if (id) { - const source = await getSourceById(id); + if (idSourceName) { + const source = await getSourceByIdSourceName(idSourceName); res.status(200).json(source ?? {}); } @@ -35,18 +34,18 @@ sourcesRouter.get('/', async (req, res) => { sourcesRouter.get('/', async (req, res) => { // eslint-disable-next-line no-unused-vars const { crosswalk, source } = req.body; - const { id } = source; - const foundSource = await getSourceById(id); + const { idSourceName } = source; + const foundSource = await getSourceByIdSourceName(idSourceName); const response = {}; if (foundSource) { - const responseSource = await updateSourceById(source); + const responseSource = await updateSourceByIdSourceName(source); if (!responseSource) throw new AppError(400, 'Error creating source'); response.source = await responseSource; if (crosswalk) { - const responseCrosswalk = await updateSourceById({ - id, + const responseCrosswalk = await updateSourceByIdSourceName({ + idSourceName, ...crosswalk, }); @@ -61,14 +60,12 @@ sourcesRouter.get('/', async (req, res) => { sourcesRouter.post('/', async (req, res) => { // eslint-disable-next-line no-unused-vars const { crosswalk, source } = req.body; - console.log('sourcesRouter.post source', source); - console.log('sourcesRouter.post crosswalk', crosswalk); const response = await createSource(source); if (!response) throw new AppError(400, 'Error creating source'); if (crosswalk) { const responseCrosswalk = await createCrosswalk({ - id: source.id, + idSourceName: source.idSourceName, ...crosswalk, }); if (!responseCrosswalk) throw new AppError(400, 'Error creating Crosswalk'); @@ -81,14 +78,14 @@ sourcesRouter.put('/', async (req, res) => { // eslint-disable-next-line no-unused-vars const { crosswalk, source } = req.body; //validate source and crosswalk - console.log('sourcesRouter.put source', source); - console.log('sourcesRouter.put crosswalk', crosswalk); - const response = await updateSourceById(source); - console.log('sourcesRouter.put response', response); + const response = await updateSourceByIdSourceName(source); if (!response) throw new AppError(400, 'Error creating source'); - const responseCrosswalk = await updateCrosswalkById(crosswalk, source.id); + const responseCrosswalk = await updateCrosswalkByIdSourceName( + crosswalk, + source.idSourceName, + ); if (!responseCrosswalk) throw new AppError(400, 'Error creating Crosswalk');