Skip to content

Commit

Permalink
Started documentation for end points
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleBridges committed Oct 2, 2020
1 parent 2cc81c0 commit 763d215
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 94 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Back-end
# Farm Fresh API

---

[Documentation](https://web.postman.co/collections/11725136-f9e17211-9122-41f4-b758-902d6d1387b7?version=latest&workspace=faa2959a-ee89-44cb-8769-476dad3a658d)
16 changes: 8 additions & 8 deletions auth/authenticateMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const jwt = require('jsonwebtoken')
const jwt_secrets = require('../config/secrets.js')
const jwt = require("jsonwebtoken");
const jwt_secrets = require("../config/secrets.js");

module.exports = (req, res, next) => {
let token = req.headers.authorization
let token = req.headers.authorization;
if (token) {
jwt.verify(token, jwt_secrets.jwtSecret, (err, decodedToken) => {
req.decodedJwt = decodedToken
next()
})
req.decodedJwt = decodedToken;
next();
});
} else {
res.status(400).json({ errorMessage: "You must be logged in" })
res.status(400).json({ errorMessage: "You must be logged in" });
}
}
};
Binary file modified data/farm_fresh.db3
Binary file not shown.
75 changes: 41 additions & 34 deletions users/users-router.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
const router = require('express').Router();
const router = require("express").Router();

const Users = require('../users/usersModel');
const Users = require("../users/usersModel");

router.get('/', async (req, res) => {
const users = await Users.find()
res.status(200).json(users)
})
router.get("/", async (req, res) => {
const users = await Users.find();
res.status(200).json(users);
});

router.get('/:id', async (req, res) => {
const id = req.params.id
let user = await Users.findById(id)
const profile = await Users.findProfile(id)
const location = await Users.findLocation(id)
res.status(200).json({ ...user, profile: profile, location: location })
})
router.get("/:id", async (req, res) => {
const id = req.params.id;
console.log(id);
let user = await Users.findById(id);
const profile = await Users.findProfile(id);
const location = await Users.findLocation(id);
res.status(200).json({
...user,
profile: profile ? profile : {},
location: location ? location : {},
});
});

router.delete('/:id', async (req, res) => {
const id = req.params.id
const user = await Users.findById(id)
await Users.del(id)
res.status(200).json(({ message: `User with the ID of ${user.id} has been removed` }))
})
router.delete("/:id", async (req, res) => {
const id = req.params.id;
const user = await Users.findById(id);
await Users.del(id);
res
.status(200)
.json({ message: `User with the ID of ${user.id} has been removed` });
});

router.get('/:id/cart', async (req, res) => {
const id = req.params.id
const user = await Users.findById(id)
const cart = await Users.findCartById(id)
res.status(200).json({ userId: user.id, cart: cart })
})
router.get("/:id/cart", async (req, res) => {
const id = req.params.id;
const user = await Users.findById(id);
const cart = await Users.findCartById(id);
res.status(200).json({ userId: user.id, cart: cart });
});

router.get('/:id/inventory', async (req, res) => {
const id = req.params.id
const user = await Users.findById(id)
const profile = await Users.findProfile(id)
router.get("/:id/inventory", async (req, res) => {
const id = req.params.id;
const user = await Users.findById(id);
const profile = await Users.findProfile(id);

if (profile.is_grower) {
const inventory = await Users.findInventoryById(id)
res.status(200).json({ userId: user.id, inventory: inventory })
const inventory = await Users.findInventoryById(id);
res.status(200).json({ userId: user.id, inventory: inventory });
} else {
res.status(400).json({ errorMessage: 'You must be a registered grower' })
res.status(400).json({ errorMessage: "You must be a registered grower" });
}
})
module.exports = router
});
module.exports = router;
103 changes: 52 additions & 51 deletions users/usersModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const db = require('../data/dbConfig');
const db = require("../data/dbConfig");

module.exports = {
add,
Expand All @@ -9,82 +9,83 @@ module.exports = {
findProfile,
findLocation,
findCartById,
findInventoryById
findInventoryById,
};

function find() {
return db('user').select('id', 'username', 'password');
return db("user").select("id", "username", "password");
}

function findBy(filter) {
return db('user').where(filter);
return db("user").where(filter);
}

async function add(user) {
const id = await db('user').insert(user);
return id[0]
const id = await db("user").insert(user);
return id[0];
}

async function del(id) {
const userCount = await db('user').where('id', id).del();
return userCount
const userCount = await db("user").where("id", id).del();
return userCount;
}

function findById(id) {
return db('user')
.where({ id })
.first();
return db("user").where({ id }).first();
}

async function findProfile(id) {
const profiles = await db('user_profile')

const profile = profiles
.filter(u => u.user_id == id)[0]

return { ...profile, is_grower: profile.is_grower }

const profiles = await db("user_profile");
try {
const profile = profiles.filter((u) => u.user_id == id)[0];

return { ...profile, is_grower: profile.is_grower };
} catch {
return {};
}
}

async function findLocation(id) {
const location = await db('location')

return location
.filter(u => u.user_id == id)[0]
const location = await db("location");

return location.filter((u) => u.user_id == id)[0];
}

async function findCartById(id) {
// const cartItems = await db('cart_item')
const userCart = await db('user_cart')
const cartItems = await db('cart_item')
const inventoryItems = await db('inventory_item')
const userCartItems = userCart.map(uc => {
let items = []
inventoryItems.map(ii => {
cartItems.map(ci => {
if (uc.user_id == id && uc.cart_item_id == ci.id && ci.inventory_item_id == ii.id) {
items.push({ ...ii, quantity: ci.quantity })
const userCart = await db("user_cart");
const cartItems = await db("cart_item");
const inventoryItems = await db("inventory_item");
const userCartItems = userCart.map((uc) => {
let items = [];
inventoryItems.map((ii) => {
cartItems.map((ci) => {
if (
uc.user_id == id &&
uc.cart_item_id == ci.id &&
ci.inventory_item_id == ii.id
) {
items.push({ ...ii, quantity: ci.quantity });
}
})
})
return items
})
return userCartItems.flat()
});
});
return items;
});

return userCartItems.flat();
}

async function findInventoryById(id) {
const userInventory = await db('user_inventory')
const inventoryItems = await db('inventory_item')
const userInventoryItems = userInventory.map(ui => {
let items = []
inventoryItems.map(ii => {
const userInventory = await db("user_inventory");
const inventoryItems = await db("inventory_item");
const userInventoryItems = userInventory.map((ui) => {
let items = [];
inventoryItems.map((ii) => {
if (ui.user_id == id && ui.inventory_item_id == ii.id) {
items.push(ii)
items.push(ii);
}
})
return items
})
return userInventoryItems.flat()
}
});
return items;
});

return userInventoryItems.flat();
}

1 comment on commit 763d215

@vercel
Copy link

@vercel vercel bot commented on 763d215 Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.