Skip to content

Instantly share code, notes, and snippets.

View AvanthikaMeenakshi's full-sized avatar
💭
Coding

Avanthika AvanthikaMeenakshi

💭
Coding
View GitHub Profile
const UsersService = {
getById(db, id) {
return db
.from("users")
.select("*")
.where("id", id)
.first();
},
deleteUser(db, id) {
return db("users")
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");
const UsersService = {
getById(db, id) {
return db
.from("users")
.select("*")
.where("id", id)
.first();
},
deleteUser(db, id) {
return db("users")
router
.route("/")
.get(...)
.post(function(req, res) {
const db = req.app.get("db");
UsersService.insertUser(db, req.body).then(data => {
res.send(data);
});
});
const UsersService = {
insertUser(db, newUser) {
return db
.insert(newUser)
.into("users")
.returning("*")
.then(rows => {
return rows[0];
});
}
const UsersService = {
getAllUsers(db) {
return db
.select("*")
.from("users")
.then(rows => rows);
}
};
module.exports = UsersService;
router
.route("/")
.get(function(req, res) {
const db = req.app.get("db");
UsersService.getAllUsers(db).then(data => {
res.send(data);
});
});
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");
})
var knex = require('knex');
const db = require("knex")({
client: "pg",
connection: {
host: "localhost",
user: "postgres",
password: "",
database: "knex-test"
}
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');
}