Skip to content

Commit

Permalink
fix matching
Browse files Browse the repository at this point in the history
  • Loading branch information
stephalexis committed Sep 23, 2018
1 parent a49c8d8 commit f14c393
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
19 changes: 13 additions & 6 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,32 @@ const pool = new pg.Pool(dbConfig);

async function findMatch(typeId) {
const client = await pool.connect()

const res = await client.query(`
SELECT
item_inventory.id as inv_id, shopping_list_items.id as list_id
FROM item_inventory
INNER JOIN shopping_list_items
ON shopping_list_items.item_type = item_inventory.item_type
LEFT JOIN item_status ON item_status.shopping_list_item_id = shopping_list_items.id
WHERE item_status.status IS NULL AND item_inventory.item_type = $1
WHERE item_inventory.item_type = $1
ORDER BY shopping_list_items.item_priority
LIMIT 1;
`, [typeId]
)
console.log(res.rows);
if (res.rows.length > 0){
res2 = await client.query(`
INSERT INTO item_status (status, item_inventory_id, shopping_list_item_id)
VALUES ('matched', $1, $2);
let res2 = await client.query(`
INSERT INTO item_match (item_inventory_id, shopping_list_item_id)
VALUES ($1, $2);
`, [res.rows[0].inv_id, res.rows[0].list_id]
)
let res3 = await client.query(`
UPDATE item_inventory SET item_status = 'matched' WHERE item_inventory.id = $1
`,[res.rows[0].inv_id]
)
let res4 = await client.query(`
UPDATE shopping_list_items SET list_status = 'matched' WHERE shopping_list_items.id = $1
`,[res.rows[0].list_id]
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/APIService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ export const fetchItemTypes = fetch('http://localhost:3000/item_types').then(

export const fetchShoppingListItems = clientId =>
fetch(
`http://localhost:3000/shopping_list_items?client_id=eq.${clientId}`
`http://localhost:2000/shopping_list_items?client_id=eq.${clientId}`
).then(res => res.json());

export const submitShoppingListItem = (clientId, itemCategory) => {
const data = {
client_id: clientId,
item_type: Number(itemCategory)
};
return fetch('http://localhost:3000/shopping_list_items', {
return fetch('http://localhost:2000/shopping_list_items', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
Expand Down
29 changes: 14 additions & 15 deletions postgrest-api/migrations/1_entities.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CREATE TABLE shopping_list_items (
id SERIAL PRIMARY KEY,
item_labels JSONB,
item_priority INTEGER,
list_status TEXT DEFAULT 'awaiting',
date_requested DATE DEFAULT CURRENT_DATE,
client_id INTEGER,
item_type INTEGER,
Expand All @@ -50,36 +51,34 @@ CREATE TABLE IF NOT EXISTS drop_locations(
address TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS item_status(
id SERIAL PRIMARY KEY,
msg TEXT,
shared boolean,
status TEXT DEFAULT 'in-stock',
updated_at DATE DEFAULT CURRENT_DATE,
item_inventory_id INTEGER,
shopping_list_item_id INTEGER,
FOREIGN KEY (shopping_list_item_id) REFERENCES shopping_list_items(id)
);

CREATE TABLE IF NOT EXISTS item_inventory (
id SERIAL PRIMARY KEY,
item_type INTEGER,
item_labels JSONB,
item_status INTEGER,
item_status TEXT DEFAULT 'in-stock',
image JSONB,
donor_email TEXT,
location_id INTEGER,
added_by TEXT NOT NULL,
added_by TEXT,
FOREIGN KEY (item_type) REFERENCES item_types(id),
FOREIGN KEY (item_status) REFERENCES item_status(id),
FOREIGN KEY (location_id) REFERENCES drop_locations(id)
);

CREATE TABLE IF NOT EXISTS item_match(
id SERIAL PRIMARY KEY,
msg TEXT,
shared boolean,
updated_at DATE DEFAULT CURRENT_DATE,
item_inventory_id INTEGER,
shopping_list_item_id INTEGER,
FOREIGN KEY (shopping_list_item_id) REFERENCES shopping_list_items(id),
FOREIGN KEY (item_inventory_id) REFERENCES item_inventory(id)
);

CREATE TABLE IF NOT EXISTS referrals_form_inputs (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL,
optional BOOLEAN DEFAULT false
);

ALTER TABLE item_status ADD CONSTRAINT fkey FOREIGN KEY (item_inventory_id) REFERENCES item_inventory(id);
20 changes: 10 additions & 10 deletions postgrest-api/migrations/2_data_clients.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ VALUES ('Smith', 'Lora', '[email protected]', '905-323-1324', '{}', 'Mellany M
('Regal', 'Ross', '[email protected]', '225-333-1537', '{}', 'Tina Broth', '{}', 2);

INSERT INTO shopping_list_items (item_priority, client_id, item_type)
VALUES (10, 1, 1),(4, 2, 1),(9, 2, 3);
VALUES (10, 1, 1), (4, 2, 1), (9, 2, 3);

INSERT INTO item_inventory (id, item_type, image, donor_email, added_by)
INSERT INTO item_inventory (item_type, image, donor_email, added_by)
VALUES
(1, 1, '{}', '[email protected]', 'Samantha H'),
(2, 1, '{}', '[email protected]', 'Matheiw Matherr'),
(3, 1, '{}', '[email protected]', 'Talia T'),
(4, 2, '{}', '[email protected]', 'Jonnie K'),
(5, 3, '{}', '[email protected]', 'Samantha H');

INSERT INTO item_status (status, item_inventory_id, shopping_list_item_id)
VALUES ('matched', 5, 3);
(1, '{}', '[email protected]', 'Samantha H'),
(1, '{}', '[email protected]', 'Matheiw Matherr'),
(1, '{}', '[email protected]', 'Talia T'),
(2, '{}', '[email protected]', 'Jonnie K'),
(3, '{}', '[email protected]', 'Samantha H');

INSERT INTO item_match (item_inventory_id, shopping_list_item_id)
VALUES (5, 3);

INSERT INTO referrals_form_inputs
VALUES (1, 'First name', 'shortAnswer', 'FALSE');
Expand Down

0 comments on commit f14c393

Please sign in to comment.