-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' of https://github.com/COS301-SE-2023/Meal-…
…Maestro into development
- Loading branch information
Showing
37 changed files
with
889 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,5 @@ out/ | |
### VS Code ### | ||
.vscode/ | ||
angular.json | ||
package-lock.json | ||
package.json |
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/fellowship/mealmaestro/controllers/RecipeBookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fellowship.mealmaestro.controllers; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import fellowship.mealmaestro.models.MealModel; | ||
import fellowship.mealmaestro.services.RecipeBookService; | ||
import jakarta.validation.Valid; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class RecipeBookController { | ||
|
||
private final RecipeBookService recipeBookService; | ||
|
||
public RecipeBookController(RecipeBookService recipeBookService) { | ||
this.recipeBookService = recipeBookService; | ||
} | ||
|
||
@PostMapping("/addRecipe") | ||
public ResponseEntity<MealModel> addRecipe(@Valid @RequestBody MealModel request, @RequestHeader("Authorization") String token) { | ||
if (token == null || token.isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
String authToken = token.substring(7); | ||
return ResponseEntity.ok(recipeBookService.addRecipe(request, authToken)); | ||
} | ||
|
||
@PostMapping("/removeRecipe") | ||
public ResponseEntity<Void> removeRecipe(@Valid @RequestBody MealModel request, @RequestHeader("Authorization") String token) { | ||
if (token == null || token.isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
String authToken = token.substring(7); | ||
recipeBookService.removeRecipe(request, authToken); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("/getAllRecipes") | ||
public ResponseEntity<List<MealModel>> getAllRecipes(@RequestHeader("Authorization") String token) { | ||
if (token == null || token.isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
String authToken = token.substring(7); | ||
return ResponseEntity.ok(recipeBookService.getAllRecipes(authToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/fellowship/mealmaestro/models/RecipeModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package fellowship.mealmaestro.models; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public class RecipeModel { | ||
@NotBlank(message = "A title is required") | ||
private String title; | ||
|
||
@NotBlank(message = "An image is required") | ||
private String image; | ||
|
||
public RecipeModel(String title, String image) { | ||
this.title = title; | ||
this.image = image; | ||
} | ||
|
||
public String getTitle() { | ||
return this.title; | ||
} | ||
|
||
public String getImage() { | ||
return this.image; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public void setImage(String image) { | ||
this.image = image; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
backend/src/main/java/fellowship/mealmaestro/repositories/RecipeBookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package fellowship.mealmaestro.repositories; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import org.neo4j.driver.Driver; | ||
import org.neo4j.driver.Session; | ||
import org.neo4j.driver.TransactionCallback; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.neo4j.driver.Values; | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
import fellowship.mealmaestro.models.MealModel; | ||
|
||
@Repository | ||
public class RecipeBookRepository { | ||
|
||
@Autowired | ||
private final Driver driver; | ||
|
||
public RecipeBookRepository(Driver driver){ | ||
this.driver = driver; | ||
} | ||
|
||
//#region Create | ||
public MealModel addRecipe(MealModel recipe, String email){ | ||
try (Session session = driver.session()){ | ||
return session.executeWrite(addRecipeTransaction(recipe, email)); | ||
} | ||
} | ||
|
||
public static TransactionCallback<MealModel> addRecipeTransaction(MealModel recipe, String email) { | ||
return transaction -> { | ||
transaction.run("MATCH (user:User {email: $email}), (recipe:Meal {name: $name, description: $desc, image: $image, ingredients: $ing, " + | ||
"instructions: $ins, cookingTime: $ck})" + | ||
"MERGE (user)-[:HAS_RECIPE_BOOK]->(recipeBook:`Recipe Book`) " + | ||
"MERGE (recipeBook)-[:CONTAINS]->(recipe)", | ||
Values.parameters("email", email, "name", recipe.getName(), "desc", recipe.getdescription(), "image", recipe.getimage(), | ||
"ing", recipe.getingredients(), "ins", recipe.getinstructions(), "ck", recipe.getcookingTime())); | ||
return (new MealModel(recipe.getName(), recipe.getinstructions(), recipe.getdescription(), recipe.getimage(), recipe.getingredients(), recipe.getcookingTime())); | ||
}; | ||
} | ||
//#endregion | ||
|
||
//#region Read | ||
public List<MealModel> getAllRecipes(String user){ | ||
try (Session session = driver.session()){ | ||
return session.executeRead(getAllRecipesTransaction(user)); | ||
} | ||
} | ||
|
||
public static TransactionCallback<List<MealModel>> getAllRecipesTransaction(String user) { | ||
return transaction -> { | ||
var result = transaction.run("MATCH (user:User {email: $email})-[:HAS_RECIPE_BOOK]->(book:`Recipe Book`)-[:CONTAINS]->(recipe:Meal) " + | ||
"RETURN recipe.name AS name, recipe.image AS image, recipe.description AS description, recipe.ingredients as ingredients, recipe.instructions as instructions, recipe.cookingTime as cookingTime", | ||
Values.parameters("email", user)); | ||
|
||
List<MealModel> recipes = new ArrayList<>(); | ||
while (result.hasNext()){ | ||
var record = result.next(); | ||
recipes.add(new MealModel(record.get("name").asString(), record.get("instructions").asString(), record.get("description").asString(), record.get("image").asString(), | ||
record.get("ingredients").asString(), record.get("cookingTime").asString())); | ||
} | ||
return recipes; | ||
}; | ||
} | ||
//#endregion | ||
|
||
//#region Delete | ||
public void removeRecipe(MealModel recipe, String email){ | ||
try (Session session = driver.session()){ | ||
session.executeWrite(removeRecipeTransaction(recipe, email)); | ||
} | ||
} | ||
|
||
public static TransactionCallback<Void> removeRecipeTransaction(MealModel recipe, String email) { | ||
return transaction -> { | ||
transaction.run("MATCH (user:User {email: $email})-[:HAS_RECIPE_BOOK]->(book:`Recipe Book`)-[r:CONTAINS]->(recipe:Meal {name: $name}) " + | ||
"DELETE r", | ||
Values.parameters("email", email, "name", recipe.getName())); | ||
return null; | ||
}; | ||
} | ||
//#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/fellowship/mealmaestro/services/RecipeBookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fellowship.mealmaestro.services; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
import fellowship.mealmaestro.models.MealModel; | ||
import fellowship.mealmaestro.repositories.RecipeBookRepository; | ||
import fellowship.mealmaestro.services.auth.JwtService; | ||
|
||
@Service | ||
public class RecipeBookService { | ||
|
||
@Autowired | ||
private JwtService jwtService; | ||
|
||
private final RecipeBookRepository recipeBookRepository; | ||
|
||
public RecipeBookService(RecipeBookRepository recipeBookRepository) { | ||
this.recipeBookRepository = recipeBookRepository; | ||
} | ||
|
||
public MealModel addRecipe(MealModel recipe, String token) { | ||
String email = jwtService.extractUserEmail(token); | ||
return recipeBookRepository.addRecipe(recipe, email); | ||
} | ||
|
||
public void removeRecipe(MealModel request, String token) { | ||
String email = jwtService.extractUserEmail(token); | ||
recipeBookRepository.removeRecipe(request, email); | ||
} | ||
|
||
public List<MealModel> getAllRecipes(String token) { | ||
String email = jwtService.extractUserEmail(token); | ||
|
||
return recipeBookRepository.getAllRecipes(email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.