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

View File

@ -28,8 +28,8 @@ exports.handler = function(request,response,state) {
// Get a skinny version of each tiddler // Get a skinny version of each tiddler
var tiddlers = []; var tiddlers = [];
$tw.utils.each(titles,function(title) { $tw.utils.each(titles,function(title) {
var tiddler = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name); var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
tiddlers.push(Object.assign({},tiddler,{text: undefined, revision: "0", bag: "bag-gamma"})); tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined, revision: "0", bag: "bag-gamma"}));
}); });
var text = JSON.stringify(tiddlers); var text = JSON.stringify(tiddlers);
state.sendResponse(200,{"Content-Type": "application/json"},text,"utf8"); 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)); response.write(template.substring(0,markerPos + marker.length));
$tw.utils.each(titles,function(title) { $tw.utils.each(titles,function(title) {
var tiddler = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name); var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
if((tiddler.text || "").length > 10 * 1024 * 1024) { if((tiddlerInfo.tiddler.text || "").length > 10 * 1024 * 1024) {
response.write(JSON.stringify(Object.assign({},tiddler,{ response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "0", revision: "0",
bag: "bag-gamma", bag: "bag-gamma",
text: undefined, text: undefined,
_canonical_uri: `/wiki/${recipe_name}/recipes/${recipe_name}/tiddlers/${title}` _canonical_uri: `/wiki/${recipe_name}/recipes/${recipe_name}/tiddlers/${title}`
}))); })));
} else { } else {
response.write(JSON.stringify(Object.assign({},tiddler,{ response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "0", revision: "0",
bag: "bag-gamma" 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) { SqlTiddlerStore.prototype.getRecipeTiddler = function(title,recipename) {
const rows = this.runStatementGetAll(` const rowTiddlerId = this.runStatementGet(`
SELECT field_name, field_value SELECT t.tiddler_id, b.bag_name
FROM fields FROM bags AS b
WHERE tiddler_id = ( INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id
SELECT t.tiddler_id INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id
FROM bags AS b INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id
INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id WHERE r.recipe_name = $recipe_name
INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id AND t.title = $title
INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id ORDER BY rb.position DESC
WHERE r.recipe_name = $recipe_name
AND t.title = $title
ORDER BY rb.position DESC
)
`,{ `,{
title: title, title: title,
recipe_name: recipename recipe_name: recipename
}); });
if(rows.length === 0) { if(!rowTiddlerId) {
return null; 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 // Verify what we've got
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ "Another Tiddler", "Hello There"]); expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ "Another Tiddler", "Hello There"]);
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).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("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("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")).toEqual({ title: "Hello There", text: "I'm in gamma", tags: "seven eight nine" }); 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")).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" }); 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")).toEqual({title: "Hello There",text: "I'm in beta",tags: "four five six"}); 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 // Delete a tiddlers to ensure the underlying tiddler in the recipe shows through
sqlTiddlerStore.deleteTiddler("Hello There","bag-beta"); sqlTiddlerStore.deleteTiddler("Hello There","bag-beta");
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ "Another Tiddler", "Hello There"]); 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"]); expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ "Hello There"]);
// Save a recipe tiddler // Save a recipe tiddler
sqlTiddlerStore.saveRecipeTiddler({title: "More", text: "None"},"recipe-rho"); 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"});
}); });
}); });