Updated whisky query to support % syntax, and summary update totals

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2021-08-16 15:28:36 -07:00
parent baf891e5ed
commit 056b388520
4 changed files with 107 additions and 21 deletions

View File

@ -1,17 +1,47 @@
# Whisky Business
Refresh the OLCC DB: ```plantuml
@startuml
object location {
address
phone
id
}
```bash object whisky {
node whisky.js label
price
average[52] : statewide average count per week
id
location[] --> location : count
}
object client
object server
object user {
location
radius
whisky[]
}
user --> whisky : monitor
@enduml
``` ```
# Whisky Business
Launch GraphQL server: Launch GraphQL server:
```bash ```bash
npm start npm start
``` ```
Refresh the OLCC DB:
```bash
node whisky.js
```
Launch website: Launch website:
```bash ```bash

View File

@ -53,7 +53,9 @@ const WhiskyType = new graphql.GraphQLObjectType({
lastSeen: { type: graphql.GraphQLString }, lastSeen: { type: graphql.GraphQLString },
size: { type: graphql.GraphQLInt }, size: { type: graphql.GraphQLInt },
price: { type: graphql.GraphQLFloat }, price: { type: graphql.GraphQLFloat },
inventories: { type: graphql.GraphQLList(InventoryType) } inventories: { type: graphql.GraphQLList(InventoryType) },
quantity: { type: graphql.GraphQLInt },
updated: { type: graphql.GraphQLString }
}) })
}); });
@ -91,7 +93,9 @@ const buildWhiskies = (rows) => {
price: row.price, price: row.price,
size: row.size, size: row.size,
code: row.code, code: row.code,
inventories: [] inventories: [],
quantity: 0,
updated: 0
} }
whiskies[row.code] = whisky; whiskies[row.code] = whisky;
} else { } else {
@ -110,6 +114,8 @@ const buildWhiskies = (rows) => {
quantity: row.quantity, quantity: row.quantity,
updated: row.updated updated: row.updated
}); });
whisky.updated = (new Date(row.updated) > new Date(whisky.updated)) ? row.updated : whisky.updated;
whisky.quantity += row.quantity;
} }
}); });
const results = []; const results = [];
@ -176,14 +182,17 @@ var queryType = new graphql.GraphQLObjectType({
fields: { fields: {
Whiskies: { Whiskies: {
type: graphql.GraphQLList(WhiskyType), type: graphql.GraphQLList(WhiskyType),
resolve: (root, args, context, info) => { args: {
code: { type: graphql.GraphQLString }
},
resolve: (root, {code}, context, info) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
database.all( database.all(
"SELECT w.*,l.code AS 'location',l.address,l.city,l.phone,l.latitude,l.longitude,i.quantity,i.updated " + "SELECT w.*,i.quantity,i.updated " +
"FROM Whiskies AS w " + "FROM Whiskies AS w " +
"LEFT JOIN Inventories AS i ON w.code=i.whisky "+ "LEFT JOIN Inventories AS i ON w.code=i.whisky " +
"LEFT JOIN Locations AS l ON l.code=i.location " + (code ? "WHERE w.code LIKE (?) " : "") +
";", function(err, rows) { ";", [code], function(err, rows) {
if (err) { console.error(err); return reject(null); } if (err) { console.error(err); return reject(null); }
resolve(buildWhiskies(rows)); resolve(buildWhiskies(rows));
}); });
@ -198,9 +207,9 @@ var queryType = new graphql.GraphQLObjectType({
resolve: (root, {code}, context, info) => { resolve: (root, {code}, context, info) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
database.all( database.all(
"SELECT w.*,l.code AS 'location',l.address,l.city,l.phone,l.latitude,l.longitude,i.quantity,i.updated "+ "SELECT w.*,l.code AS 'location',l.address,l.city,l.phone,l.latitude,l.longitude,i.quantity,i.updated " +
"FROM Whiskies AS w " + "FROM Whiskies AS w " +
"LEFT JOIN Inventories AS i ON w.code=i.whisky "+ "LEFT JOIN Inventories AS i ON w.code=i.whisky " +
"LEFT JOIN Locations AS l ON l.code=i.location " + "LEFT JOIN Locations AS l ON l.code=i.location " +
"WHERE w.code = (?);", [code], function(err, rows) { "WHERE w.code = (?);", [code], function(err, rows) {
if (err) { console.error(err); return reject(null); } if (err) { console.error(err); return reject(null); }

View File

@ -1,6 +1,6 @@
import { StatusBar } from 'expo-status-bar'; import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';
import { request, GraphQLClient, gql } from 'graphql-request'; import { request, GraphQLClient, gql } from 'graphql-request';
import moment from 'moment'; import moment from 'moment';
import { buildExecutionContext } from 'graphql/execution/execute'; import { buildExecutionContext } from 'graphql/execution/execute';
@ -84,6 +84,7 @@ const whiskyStyles = StyleSheet.create({
export default function App() { export default function App() {
const [whiskies, setWhiskies] = useState<any>(null), const [whiskies, setWhiskies] = useState<any>(null),
[search, setSearch] = useState<string>(""),
[activeWhisky, setActiveWhisky] = useState<string>(""); [activeWhisky, setActiveWhisky] = useState<string>("");
const onPress = (code : string) => { const onPress = (code : string) => {
@ -104,7 +105,7 @@ export default function App() {
); );
}) : []; }) : [];
let query = gql` { const query = gql` {
Whiskies { Whiskies {
code code
description description
@ -134,9 +135,55 @@ export default function App() {
console.error(error); console.error(error);
}); });
const submitSearch = () => {
const query = gql` {
Whiskies {
code
description
price
size
lastSeen
inventories {
location {
code
address
city
phone
longitude
latitude
}
quantity
updated
}
}
}`;
console.log(search);
client.request(query)
.then(data => {
setWhiskies(data.Whiskies);
})
.catch (error => {
console.error(error);
});
};
const keyPress = (event : KeyboardEvent) => {
if (event.code == "\n") {
submitSearch();
}
};
return ( return (
<View style={[styles.container]}> <View style={[styles.container]}>
<TextInput style={[{borderWidth: 1 }]}
onBlur={submitSearch}
onKeyPress={(event : any) => keyPress(event)}
onChange={(value) => setSearch(value.target.value)}></TextInput>
<View style={[styles.container]}>
{ items } { items }
</View>
</View> </View>
); );
} }

View File

@ -349,7 +349,7 @@ fs.stat("cache")
}) })
.then((response) => { .then((response) => {
if (response.status != 200) { if (response.status != 200) {
throw "Error fetching resource from oregonliquorsearch"; throw new Error("Error fetching resource from oregonliquorsearch");
} }
}); });
}).then(async () => { }).then(async () => {
@ -382,8 +382,7 @@ fs.stat("cache")
const rows = dom.window.document.querySelectorAll('table.list tr'); const rows = dom.window.document.querySelectorAll('table.list tr');
const olccWhiskies = []; const olccWhiskies = [];
const processRow = (row) => {
rows.forEach(row => {
const th = row.querySelectorAll('table.list th a'); const th = row.querySelectorAll('table.list th a');
if (th.length) { if (th.length) {
th.forEach(header => headers.push(header.textContent.trim())); th.forEach(header => headers.push(header.textContent.trim()));
@ -407,7 +406,7 @@ fs.stat("cache")
case 'Proof': case 'Proof':
if (parseFloat(value) == value) { if (parseFloat(value) == value) {
value = parseFloat(value); value = parseFloat(value);
} }
break; break;
case 'Case Price': case 'Case Price':
@ -433,8 +432,9 @@ fs.stat("cache")
whisky[headers[index]] = value; whisky[headers[index]] = value;
}); });
olccWhiskies.push(whisky); olccWhiskies.push(whisky);
}); };
rows.forEach(processRow);
return olccWhiskies; return olccWhiskies;
}).then(async (olccWhiskies) => { }).then(async (olccWhiskies) => {
await Promise.all(olccWhiskies.map(async (olccWhisky) => { await Promise.all(olccWhiskies.map(async (olccWhisky) => {