This file contains hidden or 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
| const UsersService = { | |
| getById(db, id) { | |
| return db | |
| .from("users") | |
| .select("*") | |
| .where("id", id) | |
| .first(); | |
| }, | |
| deleteUser(db, id) { | |
| return db("users") |
This file contains hidden or 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
| router | |
| .route("/:id") | |
| .get(function(req, res) { | |
| const db = req.app.get("db"); | |
| UsersService.getById(db, req.params.id).then(data => { | |
| res.send(data); | |
| }); | |
| }) | |
| .patch(function(req, res) { | |
| const db = req.app.get("db"); |
This file contains hidden or 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
| const UsersService = { | |
| getById(db, id) { | |
| return db | |
| .from("users") | |
| .select("*") | |
| .where("id", id) | |
| .first(); | |
| }, | |
| deleteUser(db, id) { | |
| return db("users") |
This file contains hidden or 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
| router | |
| .route("/") | |
| .get(...) | |
| .post(function(req, res) { | |
| const db = req.app.get("db"); | |
| UsersService.insertUser(db, req.body).then(data => { | |
| res.send(data); | |
| }); | |
| }); |
This file contains hidden or 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
| const UsersService = { | |
| insertUser(db, newUser) { | |
| return db | |
| .insert(newUser) | |
| .into("users") | |
| .returning("*") | |
| .then(rows => { | |
| return rows[0]; | |
| }); | |
| } |
This file contains hidden or 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
| const UsersService = { | |
| getAllUsers(db) { | |
| return db | |
| .select("*") | |
| .from("users") | |
| .then(rows => rows); | |
| } | |
| }; | |
| module.exports = UsersService; |
This file contains hidden or 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
| router | |
| .route("/") | |
| .get(function(req, res) { | |
| const db = req.app.get("db"); | |
| UsersService.getAllUsers(db).then(data => { | |
| res.send(data); | |
| }); | |
| }); |
This file contains hidden or 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
| router.get("/seed", function(req, res, next) { | |
| const db = req.app.get('db'); | |
| db.schema.hasTable("users").then(function(exists) { | |
| if (!exists) { | |
| db.schema | |
| .createTable("users", function(table) { | |
| table.increments("id").primary(); | |
| table.string("name"); | |
| table.string("email"); | |
| }) |
This file contains hidden or 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
| var knex = require('knex'); | |
| const db = require("knex")({ | |
| client: "pg", | |
| connection: { | |
| host: "localhost", | |
| user: "postgres", | |
| password: "", | |
| database: "knex-test" | |
| } |
This file contains hidden or 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
| function analyzeNumber(num) { | |
| if (typeof num !== 'number') { | |
| console.log('not a number'); | |
| } | |
| else if (num % 2 === 0) { | |
| console.log('even number'); | |
| } | |
| else { | |
| console.log('odd number'); | |
| } |
NewerOlder