Skip to content

Commit

Permalink
change id_name to id_source_name
Browse files Browse the repository at this point in the history
  • Loading branch information
zoobot committed Feb 14, 2023
1 parent e9ead86 commit 4234ba6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
33 changes: 16 additions & 17 deletions server/routes/sources/sources-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ 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);
return response;
}

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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -53,48 +51,49 @@ 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;`;
const source = await db.any(query);
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,
Object.keys(dataInSnakeCase),
'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,
Object.keys(dataInSnakeCase),
'crosswalk',
) + condition;
const updatedSource = await db.one(query, dataInSnakeCase);

return updatedSource;
}
37 changes: 17 additions & 20 deletions server/routes/sources/sources-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {});
}

Expand All @@ -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,
});

Expand All @@ -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');
Expand All @@ -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');

Expand Down

0 comments on commit 4234ba6

Please sign in to comment.