Skip to content

Instantly share code, notes, and snippets.

@kamalpreet-rad
Last active December 7, 2023 00:43
Show Gist options
  • Save kamalpreet-rad/79d47de43a2dc953f9b0e4c3ec53bbdb to your computer and use it in GitHub Desktop.
Save kamalpreet-rad/79d47de43a2dc953f9b0e4c3ec53bbdb to your computer and use it in GitHub Desktop.
Final Project
/**
* @author : Kamalpreet
* @group : Bulkify
* @created date : 12-01-2023
**/
public inherited sharing class RecipeController {
@AuraEnabled
public static void addIngredient(String ingredientName, Integer measurementAmount, String measurementType, ID recipeId){
Ingredient__c ing = new Ingredient__c();
ing.Name = ingredientName;
ing.Measurement__c = measurementAmount;
ing.Measurement_Type__c = measurementType;
ing.Recipe__c = recipeId;
insert ing;
}
@AuraEnabled
public static List < Ingredient__c > generateGroceryList(Id recipeId){
return
[
select Id, Name,Measurement__c,Measurement_Type__c,Recipe__c FROM Ingredient__c
WHERE Recipe__c = :recipeId
];
}
@AuraEnabled
public static List < Ingredient__c > scaleRecipeForServings (ID recipeId, Decimal desiredServings) {
//Scale the recipe and return the list of scaled ingredients
List<Ingredient__c> scalingOfIngredients = [Select Id, Name,Measurement__c, Measurement_Type__c,Recipe__c ,Recipe__r.servings__c
FROM Ingredient__c
WHERE Recipe__c= :recipeId AND Recipe__r.Servings__c!=NULL
];
for( Ingredient__c ing : scalingOfIngredients)
{
Decimal servingsCalculator = desiredServings/ ing.Recipe__r.Servings__c;
ing.Measurement__c = ing.Measurement__c*servingsCalculator;
}
return scalingOfIngredients;
}
@AuraEnabled(Cacheable=true)
public static Recipe__c[] getAllRecipes() {
return [
SELECT
Id,
Name,
Draft__c,
Active_Time__c,
Active_Time_Units__c,
Complexity__c,
Needs_Review__c,
Possible_Duplicate__c,
Season__c
FROM Recipe__c
ORDER BY Name
LIMIT 50
];
}
@AuraEnabled(Cacheable=true)
public static Recipe__c[] searchRecipes(String searchTerm) {
// Return all recipes when no search term
searchTerm = searchTerm.trim();
if (searchTerm == '') {
System.debug('returning: '+getAllRecipes());
return getAllRecipes();
}
// Prepare query paramters
searchTerm = '%' + searchTerm + '%';
// Execute search query
return [
SELECT
Id,
Name,
Draft__c,
Active_Time__c,
Active_Time_Units__c,
Complexity__c,
Needs_Review__c,
Possible_Duplicate__c,
Season__c
FROM Recipe__c
WHERE Name LIKE :searchTerm
ORDER BY Name
LIMIT 50
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment