mirror of
https://github.com/Ratstail91/sineql-demo.git
synced 2026-05-05 23:30:11 +10:00
queryBook and createBook are working
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
const queryAuthor = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
};
|
||||
|
||||
const createAuthor = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
};
|
||||
|
||||
const updateAuthor = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
};
|
||||
|
||||
const deleteAuthor = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
queryAuthor,
|
||||
createAuthor,
|
||||
updateAuthor,
|
||||
deleteAuthor,
|
||||
};
|
||||
@@ -0,0 +1,105 @@
|
||||
const { Book } = require('../database/models');
|
||||
|
||||
//utils
|
||||
const checkDateFormat = date => /(19\d{2}|20\d{2})[-\/.](0[1-9]|1[012])[-\/.](0[1-9]|[12][0-9]|3[01])/.test(date);
|
||||
|
||||
const queryBook = async (query, typeGraph) => {
|
||||
//sequelize stuff to find
|
||||
const attributes = [];
|
||||
|
||||
//specify fields to find
|
||||
Object.keys(query)
|
||||
.filter(key => key != 'typeName') //filter out this meta-field
|
||||
.forEach(key => {
|
||||
//check published date
|
||||
if (key == 'published' && !checkDateFormat(query[key].match)) {
|
||||
throw 'Wrong date format for published';
|
||||
}
|
||||
|
||||
//all book members should be scalar
|
||||
if (query[key].scalar) {
|
||||
//push this key into the attributes list
|
||||
attributes.push(key);
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
//search the database
|
||||
const result = await Book.findAll({
|
||||
attributes: attributes
|
||||
});
|
||||
|
||||
//finally
|
||||
return result;
|
||||
};
|
||||
|
||||
const createBook = async (query, typeGraph) => {
|
||||
//the array of objects to insert
|
||||
const inserts = [];
|
||||
|
||||
query.forEach((q, idx) => {
|
||||
//just in case
|
||||
if (!q.create) {
|
||||
throw 'Unexpected create == false';
|
||||
}
|
||||
|
||||
//specify fields to create
|
||||
Object.keys(q)
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out this meta-field
|
||||
.forEach(key => {
|
||||
//check published date
|
||||
if (key == 'published' && !checkDateFormat(q[key].create)) {
|
||||
throw 'Wrong date format for published';
|
||||
}
|
||||
|
||||
//all book members should be scalar
|
||||
if (q[key].scalar) {
|
||||
//make sure this exists
|
||||
inserts[idx] = inserts[idx] || {};
|
||||
|
||||
//push this key into the inserts list
|
||||
inserts[idx][key] = q[key].create;
|
||||
}
|
||||
})
|
||||
;
|
||||
});
|
||||
|
||||
//insert into the database
|
||||
const results = await Book.bulkCreate(inserts, { fields: ['title', 'published']});
|
||||
|
||||
//determine what fields to return
|
||||
const returns = [];
|
||||
|
||||
query.map((q, idx) => {
|
||||
//make sure it exists
|
||||
returns[idx] = returns[idx] || {};
|
||||
|
||||
//specify fields to return in each record
|
||||
const keys = Object.keys(q)
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out this meta-field
|
||||
.forEach(key => {
|
||||
if (q[key].scalar) {
|
||||
returns[idx][key] = results[idx][key];
|
||||
}
|
||||
})
|
||||
;
|
||||
});
|
||||
|
||||
//finally
|
||||
return returns;
|
||||
};
|
||||
|
||||
const updateBook = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
};
|
||||
|
||||
const deleteBook = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
queryBook,
|
||||
createBook,
|
||||
updateBook,
|
||||
deleteBook,
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
const { queryWeather, createWeather, updateWeather, deleteWeather } = require('./weather-handlers');
|
||||
const { queryBook, createBook, updateBook, deleteBook } = require('./book-handlers');
|
||||
const { queryAuthor, createAuthor, updateAuthor, deleteAuthor } = require('./author-handlers');
|
||||
|
||||
//collate
|
||||
module.exports = {
|
||||
queryHandlers: {
|
||||
Weather: queryWeather,
|
||||
Book: queryBook,
|
||||
Author: queryAuthor,
|
||||
},
|
||||
|
||||
createHandlers: {
|
||||
Weather: createWeather,
|
||||
Book: createBook,
|
||||
Author: createAuthor,
|
||||
},
|
||||
|
||||
updateHandlers: {
|
||||
Weather: updateWeather,
|
||||
Book: updateBook,
|
||||
Author: updateAuthor,
|
||||
},
|
||||
|
||||
deleteHandlers: {
|
||||
Weather: deleteWeather,
|
||||
Book: deleteBook,
|
||||
Author: deleteAuthor,
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
const queryWeather = async (query, typeGraph) => {
|
||||
let q = null;
|
||||
|
||||
//find location
|
||||
if (query.city && query.city.match) {
|
||||
q = query.city.match;
|
||||
} else if (query.latitude && query.latitude.match && query.longitude && query.longitude.match) {
|
||||
q = `${query.latitude.match},${query.longitude.match}`;
|
||||
} else {
|
||||
throw 'Unknown location';
|
||||
}
|
||||
|
||||
//get the data
|
||||
const response = await fetch(`http://api.weatherapi.com/v1/current.json?key=${process.env.WEATHER_KEY}&q=${q}`);
|
||||
const data = await response.json();
|
||||
|
||||
//return value
|
||||
const result = {
|
||||
//EMPTY
|
||||
};
|
||||
|
||||
//pick out each valid field
|
||||
if (query.last_updated) {
|
||||
result['last_updated'] = data.current.last_updated;
|
||||
}
|
||||
if (query.temp_c) {
|
||||
result['temp_c'] = data.current.temp_c;
|
||||
}
|
||||
if (query.temp_f) {
|
||||
result['temp_f'] = data.current.temp_f;
|
||||
}
|
||||
if (query.condition) {
|
||||
result['condition'] = data.current.condition.text;
|
||||
}
|
||||
if (query.wind_mph) {
|
||||
result['wind_mph'] = data.current.wind_mph;
|
||||
}
|
||||
if (query.wind_kph) {
|
||||
result['wind_kph'] = data.current.wind_kph;
|
||||
}
|
||||
if (query.wind_dir) {
|
||||
result['wind_dir'] = data.current.wind_dir;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const changeWeather = async (query, typeGraph) => {
|
||||
throw "You can't change the weather!";
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
queryWeather,
|
||||
createWeather: changeWeather,
|
||||
updateWeather: changeWeather,
|
||||
deleteWeather: changeWeather,
|
||||
};
|
||||
Reference in New Issue
Block a user