Skip to content

Commit

Permalink
Merge pull request #123 from COS301-SE-2023/PantryRefactor
Browse files Browse the repository at this point in the history
Pantry refactor
  • Loading branch information
SkulderLock authored Sep 24, 2023
2 parents 4531401 + afda8ea commit 2195442
Show file tree
Hide file tree
Showing 81 changed files with 2,115 additions and 395 deletions.
3 changes: 2 additions & 1 deletion android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ android {

apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {
implementation project(':capacitor-mlkit-barcode-scanning')
implementation project(':capacitor-app')
implementation project(':capacitor-haptics')
implementation project(':capacitor-http')
implementation project(':capacitor-keyboard')
implementation project(':capacitor-status-bar')

implementation "androidx.webkit:webkit:1.4.0"
}


Expand Down
8 changes: 8 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data android:name="com.google.mlkit.vision.DEPENDENCIES" android:value="barcode-ui" />

<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:name=".MainActivity"
Expand Down Expand Up @@ -38,4 +40,10 @@
<!-- Permissions -->

<uses-permission android:name="android.permission.INTERNET" />

<!-- To get access to the camera -->
<uses-permission android:name="android.permission.CAMERA" />

<!-- To get access to the flashlight -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
</manifest>
3 changes: 3 additions & 0 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')

include ':capacitor-mlkit-barcode-scanning'
project(':capacitor-mlkit-barcode-scanning').projectDir = new File('../node_modules/@capacitor-mlkit/barcode-scanning/android')

include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android')

Expand Down
2 changes: 2 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
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.jsoup:jsoup:1.16.1'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package fellowship.mealmaestro;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.fasterxml.jackson.core.JsonProcessingException;


@SpringBootApplication
@EnableScheduling
public class MealmaestroApplication {

public static void main(String[] args) throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
Expand Down Expand Up @@ -45,4 +47,13 @@ public PasswordEncoder passwordEncoder() {
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}

@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(6);
scheduler.initialize();
return scheduler;
}

}
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
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
Expand All @@ -15,8 +14,11 @@
@RestController
public class BrowseController {

@Autowired
private BrowseService browseService;
private final BrowseService browseService;

public BrowseController(BrowseService browseService) {
this.browseService = browseService;
}

@GetMapping("/getPopularMeals")
public ResponseEntity<List<MealModel>> getPopularMeals(@RequestHeader("Authorization") String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -19,16 +18,28 @@
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 {
@Autowired
private MealManagementService mealManagementService;
@Autowired
private MealDatabaseService mealDatabaseService;

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

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

@PostMapping("/getMealPlanForDay")
public ResponseEntity<List<MealModel>> dailyMeals(@Valid @RequestBody DateModel request,
Expand Down Expand Up @@ -120,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 @@ -138,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 @@ -3,7 +3,6 @@
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -17,8 +16,11 @@
@RestController
public class PantryController {

@Autowired
private PantryService pantryService;
private final PantryService pantryService;

public PantryController(PantryService pantryService) {
this.pantryService = pantryService;
}

@PostMapping("/addToPantry")
public ResponseEntity<FoodModel> addToPantry(@Valid @RequestBody FoodModel request,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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.mongo.findBarcodeRequest;
import fellowship.mealmaestro.models.mongo.FoodModelM;
import fellowship.mealmaestro.services.BarcodeService;

@RestController
public class ProductController {

private final BarcodeService barcodeService;

public ProductController(BarcodeService barcodeService) {
this.barcodeService = barcodeService;
}

@PostMapping("/findProduct")
public ResponseEntity<FoodModelM> findProduct(@RequestBody findBarcodeRequest request,
@RequestHeader("Authorization") String token) {
if (token == null || token.isEmpty()) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(barcodeService.findProduct(request));
}

@PostMapping("/addProduct")
public ResponseEntity<FoodModelM> addProduct(@RequestBody FoodModelM product,
@RequestHeader("Authorization") String token) {
if (token == null || token.isEmpty()) {
return ResponseEntity.badRequest().build();
}
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
Loading

0 comments on commit 2195442

Please sign in to comment.