Make getRecipeTiddlers return the bagname as well

This commit is contained in:
Jeremy Ruston 2024-01-20 20:22:46 +00:00
parent e9f83ca735
commit 59aed49e98
4 changed files with 37 additions and 27 deletions

View File

@ -24,12 +24,12 @@ exports.handler = function(request,response,state) {
recipe_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]); recipe_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]);
if(recipe_name === recipe_name_2) { if(recipe_name === recipe_name_2) {
// Get the tiddlers in the recipe // Get the tiddlers in the recipe
var titles = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name); var recipeTiddlers = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name);
// 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(recipeTiddlers,function(recipeTiddlerInfo) {
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name); var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined, revision: "0", bag: "bag-gamma"})); tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined, revision: "0", bag: recipeTiddlerInfo.bag_name}));
}); });
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

@ -26,7 +26,7 @@ exports.handler = function(request,response,state) {
"Content-Type": "text/html" "Content-Type": "text/html"
}); });
// Get the tiddlers in the recipe // Get the tiddlers in the recipe
var titles = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name); var recipeTiddlers = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name);
// Render the template // Render the template
var template = $tw.sqlTiddlerStore.adminWiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{ var template = $tw.sqlTiddlerStore.adminWiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{
variables: { variables: {
@ -49,19 +49,19 @@ exports.handler = function(request,response,state) {
throw new Error("Cannot find tiddler store in template"); throw new Error("Cannot find tiddler store in template");
} }
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(recipeTiddlers,function(recipeTiddlerInfo) {
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name); var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
if((tiddlerInfo.tiddler.text || "").length > 10 * 1024 * 1024) { if((tiddlerInfo.tiddler.text || "").length > 10 * 1024 * 1024) {
response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{ response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "0", revision: "0",
bag: "bag-gamma", bag: recipeTiddlerInfo.bag_name,
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({},tiddlerInfo.tiddler,{ response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "0", revision: "0",
bag: "bag-gamma" bag: recipeTiddlerInfo.bag_name
}))); })));
} }
response.write(",") response.write(",")

View File

@ -387,22 +387,20 @@ Get the titles of the tiddlers in a recipe. Returns an empty array for recipes t
*/ */
SqlTiddlerStore.prototype.getRecipeTiddlers = function(recipename) { SqlTiddlerStore.prototype.getRecipeTiddlers = function(recipename) {
const rows = this.runStatementGetAll(` const rows = this.runStatementGetAll(`
SELECT DISTINCT title SELECT title, bag_name
FROM tiddlers FROM (
WHERE bag_id IN ( SELECT t.title, b.bag_name, MAX(rb.position) AS position
SELECT bag_id FROM bags AS b
FROM recipe_bags INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id
WHERE recipe_id = ( INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id
SELECT recipe_id INNER JOIN tiddlers AS t ON b.bag_id = t.bag_id
FROM recipes WHERE r.recipe_name = $recipe_name
WHERE recipe_name = $recipe_name GROUP BY title
)
) )
ORDER BY title ASC
`,{ `,{
recipe_name: recipename recipe_name: recipename
}); });
return rows.map(value => value.title); return rows;
}; };
/* /*

View File

@ -42,8 +42,14 @@ describe("SQL tiddler store", function() {
sqlTiddlerStore.saveBagTiddler({title: "Hello There",text: "I'm in beta",tags: "four five six"},"bag-beta"); sqlTiddlerStore.saveBagTiddler({title: "Hello There",text: "I'm in beta",tags: "four five six"},"bag-beta");
sqlTiddlerStore.saveBagTiddler({title: "Hello There",text: "I'm in gamma",tags: "seven eight nine"},"bag-gamma"); sqlTiddlerStore.saveBagTiddler({title: "Hello There",text: "I'm in gamma",tags: "seven eight nine"},"bag-gamma");
// 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([
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ "Another Tiddler", "Hello There"]); { title: 'Another Tiddler', bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-beta' }
]);
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([
{ title: 'Another Tiddler', bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-gamma' }
]);
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-rho").tiddler).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").tiddler).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" });
@ -52,12 +58,18 @@ describe("SQL tiddler store", function() {
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-upsilon").tiddler).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([
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ "Another Tiddler", "Hello There"]); { title: 'Another Tiddler', bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-alpha' }
]);
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([
{ title: 'Another Tiddler', bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-gamma' }
]);
expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-beta")).toEqual(null); expect(sqlTiddlerStore.getRecipeTiddler("Hello There","recipe-beta")).toEqual(null);
sqlTiddlerStore.deleteTiddler("Another Tiddler","bag-alpha"); sqlTiddlerStore.deleteTiddler("Another Tiddler","bag-alpha");
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ "Hello There"]); expect(sqlTiddlerStore.getRecipeTiddlers("recipe-rho")).toEqual([ { title: 'Hello There', bag_name: 'bag-alpha' } ]);
expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ "Hello There"]); expect(sqlTiddlerStore.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: 'Hello There', bag_name: 'bag-gamma' } ]);
// 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").tiddler).toEqual({title: "More", text: "None"}); expect(sqlTiddlerStore.getRecipeTiddler("More","recipe-rho").tiddler).toEqual({title: "More", text: "None"});