Update store.getRecipeTiddler to also return the bag from which the tiddler came

This commit is contained in:
Jeremy Ruston 2024-01-19 20:35:47 +00:00
parent 01d29ed11e
commit afa9ad3cde
5 changed files with 45 additions and 35 deletions

View File

@ -23,15 +23,15 @@ exports.handler = function(request,response,state) {
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
recipe_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
title = $tw.utils.decodeURIComponentSafe(state.params[2]),
tiddler = recipe_name === recipe_name_2 && $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
if(recipe_name === recipe_name_2 && tiddler) {
tiddlerInfo = recipe_name === recipe_name_2 && $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
if(recipe_name === recipe_name_2 && tiddlerInfo) {
// If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
var tiddlerFields = {},
knownFields = [
"bag", "created", "creator", "modified", "modifier", "permissions", "recipe", "revision", "tags", "text", "title", "type", "uri"
];
$tw.utils.each(tiddler,function(value,name) {
$tw.utils.each(tiddlerInfo.tiddler,function(value,name) {
if(knownFields.indexOf(name) !== -1) {
tiddlerFields[name] = value;
} else {
@ -40,7 +40,7 @@ exports.handler = function(request,response,state) {
}
});
tiddlerFields.revision = "0";
tiddlerFields.bag = "bag-gamma";
tiddlerFields.bag = tiddlerInfo.bag_name;
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
} else {

View File

@ -28,8 +28,8 @@ exports.handler = function(request,response,state) {
// Get a skinny version of each tiddler
var tiddlers = [];
$tw.utils.each(titles,function(title) {
var tiddler = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
tiddlers.push(Object.assign({},tiddler,{text: undefined, revision: "0", bag: "bag-gamma"}));
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined, revision: "0", bag: "bag-gamma"}));
});
var text = JSON.stringify(tiddlers);
state.sendResponse(200,{"Content-Type": "application/json"},text,"utf8");

View File

@ -50,16 +50,16 @@ exports.handler = function(request,response,state) {
}
response.write(template.substring(0,markerPos + marker.length));
$tw.utils.each(titles,function(title) {
var tiddler = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
if((tiddler.text || "").length > 10 * 1024 * 1024) {
response.write(JSON.stringify(Object.assign({},tiddler,{
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
if((tiddlerInfo.tiddler.text || "").length > 10 * 1024 * 1024) {
response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "0",
bag: "bag-gamma",
text: undefined,
_canonical_uri: `/wiki/${recipe_name}/recipes/${recipe_name}/tiddlers/${title}`
})));
} else {
response.write(JSON.stringify(Object.assign({},tiddler,{
response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "0",
bag: "bag-gamma"
})));

View File

@ -324,32 +324,42 @@ SqlTiddlerStore.prototype.getBagTiddler = function(title,bagname) {
}
};
/*
Returns {bag_name:, tiddler: {fields}}
*/
SqlTiddlerStore.prototype.getRecipeTiddler = function(title,recipename) {
const rows = this.runStatementGetAll(`
SELECT field_name, field_value
FROM fields
WHERE tiddler_id = (
SELECT t.tiddler_id
FROM bags AS b
INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id
INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id
INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id
WHERE r.recipe_name = $recipe_name
AND t.title = $title
ORDER BY rb.position DESC
)
const rowTiddlerId = this.runStatementGet(`
SELECT t.tiddler_id, b.bag_name
FROM bags AS b
INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id
INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id
INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id
WHERE r.recipe_name = $recipe_name
AND t.title = $title
ORDER BY rb.position DESC
`,{
title: title,
recipe_name: recipename
});
if(rows.length === 0) {
if(!rowTiddlerId) {
return null;
} else {
return rows.reduce((accumulator,value) => {
accumulator[value["field_name"]] = value.field_value;
return accumulator;
},{title: title});
}
// Get the fields
const rows = this.runStatementGetAll(`
SELECT field_name, field_value
FROM fields
WHERE tiddler_id = $tiddler_id
`,{
tiddler_id: rowTiddlerId.tiddler_id,
recipe_name: recipename
});
return {
bag_name: rowTiddlerId.bag_name,
tiddler: rows.reduce((accumulator,value) => {
accumulator[value["field_name"]] = value.field_value;
return accumulator;
},{title: title})
};
};
/*

View File

@ -44,12 +44,12 @@ describe("SQL tiddler store", function() {
// Verify what we've got
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ "Another Tiddler", "Hello There"]);
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ "Another Tiddler", "Hello There"]);
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-rho")).toEqual({ title: "Hello There", text: "I'm in beta", tags: "four five six" });
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-rho").tiddler).toEqual({ title: "Hello There", text: "I'm in beta", tags: "four five six" });
expect(sqlTiddlerStore.getRecipeTiddler("Missing Tiddler","recipe-rho")).toEqual(null);
expect(sqlTiddlerStore.getRecipeTiddler("Another Tiddler","recipe-rho")).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" });
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-sigma")).toEqual({ title: "Hello There", text: "I'm in gamma", tags: "seven eight nine" });
expect(sqlTiddlerStore.getRecipeTiddler("Another Tiddler","recipe-sigma")).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" });
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-upsilon")).toEqual({title: "Hello There",text: "I'm in beta",tags: "four five six"});
expect(sqlTiddlerStore.getRecipeTiddler("Another Tiddler","recipe-rho").tiddler).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" });
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-sigma").tiddler).toEqual({ title: "Hello There", text: "I'm in gamma", tags: "seven eight nine" });
expect(sqlTiddlerStore.getRecipeTiddler("Another Tiddler","recipe-sigma").tiddler).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" });
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-upsilon").tiddler).toEqual({title: "Hello There",text: "I'm in beta",tags: "four five six"});
// Delete a tiddlers to ensure the underlying tiddler in the recipe shows through
sqlTiddlerStore.deleteTiddler("Hello There","bag-beta");
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ "Another Tiddler", "Hello There"]);
@ -60,7 +60,7 @@ describe("SQL tiddler store", function() {
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ "Hello There"]);
// Save a recipe tiddler
sqlTiddlerStore.saveRecipeTiddler({title: "More", text: "None"},"recipe-rho");
expect(sqlTiddlerStore.getRecipeTiddler("More","recipe-rho")).toEqual({title: "More", text: "None"});
expect(sqlTiddlerStore.getRecipeTiddler("More","recipe-rho").tiddler).toEqual({title: "More", text: "None"});
});
});