Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions api/src/models/KeyPhrase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Document, Schema, model } from 'mongoose';
import { composeMongoose } from 'graphql-compose-mongoose';

import { unique } from './validation.js';

export interface KeyPhrase extends Document {
value: string;
description: string;
}

const keyPhraseSchema = new Schema<KeyPhrase>(
{
value: {
type: String,
required: true,
unique: true,
validate: [unique('KeyPhrase', 'value')],
set: (v: string) => v.toLowerCase(),
},
description: {
type: String,
required: true,
},
},
{ timestamps: true }
);

export const KeyPhrase = model<KeyPhrase>('KeyPhrase', keyPhraseSchema);
export const KeyPhraseTC = composeMongoose(KeyPhrase);
58 changes: 58 additions & 0 deletions api/src/schema/KeyPhrase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Recipe } from '../models/Recipe.js';
import { KeyPhrase, KeyPhraseTC } from '../models/KeyPhrase.js';
import { createOneResolver, updateByIdResolver } from './utils.js';

KeyPhraseTC.addResolver({
name: 'updateById',
description: 'Update a key phrase by its ID',
type: KeyPhraseTC.mongooseResolvers.updateById().getType(),
args: KeyPhraseTC.mongooseResolvers.updateById().getArgs(),
resolve: updateByIdResolver(KeyPhrase, KeyPhraseTC),
});

KeyPhraseTC.addResolver({
name: 'createOne',
description: 'Create a new key phrase',
type: KeyPhraseTC.mongooseResolvers.createOne().getType(),
args: KeyPhraseTC.mongooseResolvers.createOne().getArgs(),
resolve: createOneResolver(KeyPhrase, KeyPhraseTC),
});

export const KeyPhraseQuery = {
keyPhraseById: KeyPhraseTC.mongooseResolvers
.findById()
.setDescription('Retrieve a key phrase by its ID'),
keyPhraseByIds: KeyPhraseTC.mongooseResolvers
.findByIds()
.setDescription('Retrieve multiple key phrases by their IDs'),
keyPhraseOne: KeyPhraseTC.mongooseResolvers
.findOne()
.setDescription('Retrieve a single key phrase'),
keyPhraseMany: KeyPhraseTC.mongooseResolvers
.findMany()
.setDescription('Retrieve multiple key phrases'),
};

export const KeyPhraseMutation = {
keyPhraseCreateOne: KeyPhraseTC.getResolver('createOne'),
keyPhraseUpdateById: KeyPhraseTC.getResolver('updateById'),
keyPhraseRemoveById: KeyPhraseTC.mongooseResolvers
.removeById()
.setDescription('Remove a key phrase by its ID'),
};

// Returns true if the phrase value appears in any recipe instruction text
export const KeyPhraseQueryExtra = {
keyPhraseUsedInRecipes: {
type: 'Boolean!',
args: { value: 'String!' },
resolve: async (_: unknown, { value }: { value: string }) => {
const escapedValue = value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(`\\b${escapedValue}\\b`, 'i');
const found = await Recipe.findOne({
'instructionSubsections.instructions': { $regex: regex },
}).lean();
return found !== null;
},
},
};
4 changes: 4 additions & 0 deletions api/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { isAdmin, isImageOwnerOrAdmin } from '../middleware/authorisation.js';
import { UnitConversionMutation, UnitConversionQuery } from './UnitConversion.js';
import { ConversionRuleMutation, ConversionRuleQuery } from './UnitConversion.js';
import { isDocumentOwnerOrAdmin, isVerified } from '../middleware/authorisation.js';
import { KeyPhraseMutation, KeyPhraseQuery, KeyPhraseQueryExtra } from './KeyPhrase.js';
import { PrepMethodMutation, PrepMethodQuery, PrepMethodQueryAdmin } from './PrepMethod.js';

const isAdminMutations = composeResolvers(
Expand All @@ -26,6 +27,7 @@ const isAdminMutations = composeResolvers(
...TagMutation,
...UnitConversionMutation,
...ConversionRuleMutation,
...KeyPhraseMutation,
},
},
{ 'Mutation.*': [isAdmin()] }
Expand Down Expand Up @@ -122,6 +124,8 @@ schemaComposer.Query.addFields({
...ImageQuery,
...UnitConversionQuery,
...ConversionRuleQuery,
...KeyPhraseQuery,
...KeyPhraseQueryExtra,
...isAdminQueries.Query,
});
schemaComposer.Mutation.addFields({
Expand Down
4 changes: 3 additions & 1 deletion api/src/scripts/seedDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'dotenv-flow/config';
import mongoose from '../utils/database.js';
import { populateUnits } from '../utils/populate.js';
import { populateIngredients, populateUsers } from '../utils/populate.js';
import { populateImages, populateRecipes, populateTags } from '../utils/populate.js';
import { populatePrepMethods, populateSizes, populateUnits } from '../utils/populate.js';
import { populateKeyPhrases, populatePrepMethods, populateSizes } from '../utils/populate.js';

async function populateData() {
await populateUsers();
await populateTags();
await populateKeyPhrases();
await populateUnits();
await populateSizes();
await populateIngredients();
Expand Down
32 changes: 32 additions & 0 deletions api/src/utils/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Size } from '../models/Size.js';
import { Image } from '../models/Image.js';
import { IMAGE_DIR } from '../constants.js';
import { Recipe } from '../models/Recipe.js';
import { KeyPhrase } from '../models/KeyPhrase.js';
import { Ingredient } from '../models/Ingredient.js';
import { PrepMethod } from '../models/PrepMethod.js';

Expand All @@ -28,6 +29,37 @@ export async function populateTags() {
}
}

export async function populateKeyPhrases() {
try {
// Remove all existing key phrases
await KeyPhrase.collection.drop();

const dummyKeyPhrases = [
{ value: 'sear', description: 'To cook at high heat until a crust forms.' },
{ value: 'blanch', description: 'Briefly boil then plunge into ice water.' },
{
value: 'fold in',
description:
'Gently combine a lighter mixture into a heavier one without deflating.',
},
{
value: 'season to taste',
description:
'Add salt, pepper, or other seasonings according to personal preference.',
},
{
value: 'bring to a boil',
description: 'Heat liquid until it reaches 100°C (212°F) and bubbles vigorously.',
},
];
const createdKeyPhrases = await KeyPhrase.create(dummyKeyPhrases);

console.log('Dummy key phrases added:', createdKeyPhrases);
} catch (error) {
console.error('Error populating key phrases:', error);
}
}

export async function populatePrepMethods() {
try {
// Remove all existing prep methods
Expand Down
Loading