Skip to content

Commit

Permalink
Merge branch 'development' into webscraper-woolworths
Browse files Browse the repository at this point in the history
  • Loading branch information
Krygsmancode committed Sep 24, 2023
2 parents 91d0786 + 2195442 commit e818750
Show file tree
Hide file tree
Showing 87 changed files with 3,795 additions and 1,678 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $RECYCLE.BIN/
*.log
log.txt
mealmaestro-logs.txt
logs/


# ios
Expand Down
1 change: 0 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-logging'
implementation 'org.seleniumhq.selenium:selenium-java:4.12.1'
implementation 'org.jsoup:jsoup:1.16.1'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import fellowship.mealmaestro.config.exceptions.UserNotFoundException;

@RestControllerAdvice
public class GlobalExceptionHandler {

// @ExceptionHandler(RuntimeException.class)
// public ResponseEntity<String> handleUserNotFoundException(RuntimeException e)
// {
// return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
// }
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<String> handleUserNotFoundException(UserNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Driver neo4jDriver() {
username = "No DB Username Found";
password = "No DB Password Found";
}

return GraphDatabase.driver(uri, AuthTokens.basic(username, password));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,31 @@ public class SecurityConfig {

private final AuthenticationProvider authenticationProvider;

public SecurityConfig(JwtAuthenticationFilter jwtAuthFilter, AuthenticationProvider authenticationProvider){
public SecurityConfig(JwtAuthenticationFilter jwtAuthFilter, AuthenticationProvider authenticationProvider) {
this.jwtAuthFilter = jwtAuthFilter;
this.authenticationProvider = authenticationProvider;
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(authReq -> authReq
.requestMatchers("/register", "/authenticate")
.permitAll()
.anyRequest()
.authenticated()
)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(authReq -> authReq
.requestMatchers("/register", "/authenticate", "/hello")
.permitAll()
.anyRequest()
.authenticated())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

http.authenticationProvider(authenticationProvider);
return http.build();
}

@Bean
CorsConfigurationSource corsConfigurationSource(){
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration corsConfig = new CorsConfiguration();
corsConfig.setAllowedOrigins(Arrays.asList("*"));
corsConfig.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fellowship.mealmaestro.config.exceptions;

public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fellowship.mealmaestro.controllers;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import fellowship.mealmaestro.models.neo4j.MealModel;
import jakarta.validation.Valid;

@RestController
public class LikeDislikeController {

public LikeDislikeController() {

}

@PostMapping("/liked")
public ResponseEntity<Void> liked(@Valid @RequestBody MealModel request, @RequestHeader("Authorization") String token) {
if (token == null || token.isEmpty()) {
return ResponseEntity.badRequest().build();
}

String authToken = token.substring(7);
//service here

return ResponseEntity.ok().build();
}

@PostMapping("/disliked")
public ResponseEntity<Void> disliked(@Valid @RequestBody MealModel request, @RequestHeader("Authorization") String token) {
if (token == null || token.isEmpty()) {
return ResponseEntity.badRequest().build();
}

String authToken = token.substring(7);
//service here

return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@
import fellowship.mealmaestro.models.RegenerateMealRequest;
import fellowship.mealmaestro.models.neo4j.DateModel;
import fellowship.mealmaestro.models.neo4j.MealModel;
import fellowship.mealmaestro.services.LogService;
import fellowship.mealmaestro.services.MealDatabaseService;
import fellowship.mealmaestro.services.MealManagementService;
import fellowship.mealmaestro.services.RecommendationService;
import jakarta.validation.Valid;

@RestController
public class MealManagementController {

private final MealManagementService mealManagementService;
private final MealDatabaseService mealDatabaseService;
private final RecommendationService recommendationService;
private final LogService logService;

public MealManagementController(MealManagementService mealManagementService,
MealDatabaseService mealDatabaseService) {
MealDatabaseService mealDatabaseService, RecommendationService recommendationService,
LogService logService) {
this.mealManagementService = mealManagementService;
this.mealDatabaseService = mealDatabaseService;
this.recommendationService = recommendationService;
this.logService = logService;
}

@PostMapping("/getMealPlanForDay")
Expand Down Expand Up @@ -123,13 +131,26 @@ public ResponseEntity<MealModel> regenerate(@RequestBody RegenerateMealRequest r
@RequestHeader("Authorization") String token)
throws JsonMappingException, JsonProcessingException {

token = token.substring(7);
logService.logMeal(token, request.getMeal(), "regenerate");
MealModel recoMeal = null;
try {
recoMeal = recommendationService.getRecommendedMeal(request.getMeal().getType(), token);
} catch (Exception e) {
System.out.println(e);
}

// Try find an appropriate meal in the database
Optional<MealModel> replacementMeal = mealDatabaseService.findMealTypeForUser(request.getMeal().getType(),
token);
Optional<MealModel> replacementMeal = null;
MealModel returnedMeal = null;

if (recoMeal != null) {
replacementMeal = Optional.ofNullable(recoMeal);
} else {
token = token.substring(7);

// Try find an appropriate meal in the database
replacementMeal = mealDatabaseService.findMealTypeForUser(request.getMeal().getType(), token);
}

if (replacementMeal.isPresent()) {
returnedMeal = mealDatabaseService.replaceMeal(request, replacementMeal.get(), token);
} else {
Expand All @@ -141,16 +162,4 @@ public ResponseEntity<MealModel> regenerate(@RequestBody RegenerateMealRequest r
return ResponseEntity.ok(returnedMeal);
}

// @GetMapping("/getPopularMeals")
// public String popularMeals() throws JsonMappingException,
// JsonProcessingException{
// return mealManagementService.generatePopularMeals();
// }

// @GetMapping("/getSearchedMeals")
// public String searchedMeals(@RequestParam String query) throws
// JsonMappingException, JsonProcessingException {
// // Call the mealManagementService to search meals based on the query
// return mealManagementService.generateSearchedMeals(query);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ public ResponseEntity<FoodModelM> addProduct(@RequestBody FoodModelM product,
}
return ResponseEntity.ok(barcodeService.addProduct(product));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package fellowship.mealmaestro.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import fellowship.mealmaestro.models.neo4j.MealModel;
import fellowship.mealmaestro.services.LogService;
import fellowship.mealmaestro.services.RecipeBookService;
import jakarta.validation.Valid;

import java.util.List;

@RestController
public class RecipeBookController {

@Autowired
private LogService logService;
private final RecipeBookService recipeBookService;

public RecipeBookController(RecipeBookService recipeBookService) {
Expand All @@ -25,6 +28,8 @@ public ResponseEntity<MealModel> addRecipe(@Valid @RequestBody MealModel request
return ResponseEntity.badRequest().build();
}

logService.logMeal(token, request, "save");

String authToken = token.substring(7);
return ResponseEntity.ok(recipeBookService.addRecipe(request, authToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import fellowship.mealmaestro.config.exceptions.UserNotFoundException;
import fellowship.mealmaestro.models.UpdateUserRequestModel;
import fellowship.mealmaestro.models.auth.AuthenticationRequestModel;
import fellowship.mealmaestro.models.auth.AuthenticationResponseModel;
Expand All @@ -32,7 +33,7 @@ public UserController(AuthenticationService authenticationService, UserService u

@PostMapping("/findByEmail")
public UserModel findByEmail(@RequestBody UserModel user) {
return userService.findByEmail(user.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
return userService.findByEmail(user.getEmail()).orElseThrow(() -> new UserNotFoundException("User not found"));
}

@PostMapping("/register")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MealModel {
private String name;

@NotBlank(message = "An image is required")
private String image;
private String image = "https://images.unsplash.com/photo-1498837167922-ddd27525d352?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1170&q=80";

@NotBlank(message = "A Description is required")
private String description;
Expand All @@ -37,7 +37,7 @@ public MealModel(String name, String instructions, String description, String im
this.name = name;
this.instructions = instructions;
this.description = description;
this.image = image;
this.image = "https://images.unsplash.com/photo-1498837167922-ddd27525d352?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1170&q=80";
this.ingredients = ingredients;
this.cookingTime = cookingTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ public String toString() {

return csv;
}

public List<String> getNameList() throws Exception{
List<String> list = new ArrayList<>();
for (FoodModel foodModel : this.foods) {
list.add(foodModel.getName());
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.security.core.userdetails.UserDetails;

import fellowship.mealmaestro.models.auth.AuthorityRoleModel;
import fellowship.mealmaestro.models.neo4j.relationships.HasLogEntry;
import fellowship.mealmaestro.models.neo4j.relationships.HasMeal;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -54,6 +55,36 @@ public class UserModel implements UserDetails {
@Relationship(type = "HAS_MEAL")
private List<HasMeal> meals;

@Relationship(type = "HAS_LOG_ENTRY")
private List<HasLogEntry> entries;

@Relationship(type = "HAS_VIEW")
private ViewModel view;

public Long getVersion() {
return this.version;
}

public void setVersion(Long version) {
this.version = version;
}

public List<HasLogEntry> getEntries() {
return this.entries;
}

public void setEntries(List<HasLogEntry> entries) {
this.entries = entries;
}

public ViewModel getView() {
return this.view;
}

public void setView(ViewModel view) {
this.view = view;
}

public UserModel() {
this.authorityRole = AuthorityRoleModel.USER;
}
Expand Down Expand Up @@ -167,4 +198,12 @@ public List<HasMeal> getMeals() {
public void setMeals(List<HasMeal> meals) {
this.meals = meals;
}

public List<HasLogEntry> getLogEntries() {
return entries;
}

public void setLogEntries(List<HasLogEntry> entries) {
this.entries = entries;
}
}
Loading

0 comments on commit e818750

Please sign in to comment.