generated from textbook/starter-kit
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Consistent use of arrow functions - Simpler naming - Extract query creation
- Loading branch information
Showing
7 changed files
with
115 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,55 @@ | ||
import db, { singleLine } from "../db"; | ||
import db, { ErrorCodes, insertQuery, singleLine, updateQuery } from "../db"; | ||
|
||
import { DuplicateResource } from "./resourcesService"; | ||
|
||
const resourceQuery = singleLine` | ||
SELECT r.*, t.name as topic_name | ||
FROM resources as r | ||
LEFT JOIN topics as t | ||
ON r.topic = t.id | ||
`; | ||
|
||
export const add = async ({ description, source, title, topic, url }) => { | ||
try { | ||
const { | ||
rows: [created], | ||
} = await db.query( | ||
"INSERT INTO resources (description, source, title, topic, url) VALUES ($1, $2, $3, $4, $5) RETURNING *;", | ||
insertQuery("resources", [ | ||
"description", | ||
"source", | ||
"title", | ||
"topic", | ||
"url", | ||
]), | ||
[description, source, title, topic, url] | ||
); | ||
return created; | ||
} catch (err) { | ||
if (err.code === "23505" /** unique_violation */) { | ||
if (err.code === ErrorCodes.UNIQUE_VIOLATION) { | ||
throw new DuplicateResource(); | ||
} | ||
throw err; | ||
} | ||
}; | ||
|
||
export const findAll = async () => { | ||
const { rows } = await db.query(resourceQuery); | ||
return rows; | ||
}; | ||
|
||
export const findOne = async (id) => { | ||
const { | ||
rows: [resource], | ||
} = await db.query(`${resourceQuery} WHERE r.id = $1;`, [id]); | ||
return resource; | ||
}; | ||
|
||
export const getAll = async ({ draft }) => { | ||
const { rows } = await db.query( | ||
draft ? `${resourceQuery};` : `${resourceQuery} WHERE r.draft = false;` | ||
); | ||
return rows; | ||
}; | ||
|
||
export const update = async (id, { draft, publication, publisher }) => { | ||
const { | ||
rows: [updated], | ||
} = await db.query( | ||
"UPDATE resources SET draft = $2, publication = $3, publisher = $4 WHERE id = $1 RETURNING *;", | ||
updateQuery("resources", ["draft", "publication", "publisher"]), | ||
[id, draft, publication, publisher] | ||
); | ||
return updated; | ||
}; | ||
|
||
const resourceQuery = singleLine` | ||
SELECT r.*, t.name as topic_name | ||
FROM resources as r | ||
LEFT JOIN topics as t | ||
ON r.topic = t.id | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import db from "../db"; | ||
|
||
export async function findOne(id) { | ||
export const findAll = async () => { | ||
const { rows } = await db.query("SELECT * FROM topics;"); | ||
return rows; | ||
}; | ||
|
||
export const findOne = async (id) => { | ||
const { | ||
rows: [topic], | ||
} = await db.query("SELECT * FROM topics WHERE id = $1;", [id]); | ||
return topic; | ||
} | ||
|
||
export async function getAll() { | ||
const { rows } = await db.query("SELECT * FROM topics;"); | ||
return rows; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters