mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
311d736805
- /src/main/java/com/example/demo/controller/SiteController.java - /src/main/resources/templates/home.html - /src/main/resources/templates/explore.html - /src/main/resources/templates/view-recipe.html - /src/main/resources/templates/public-profile.html - /src/main/resources/templates/my-profile.html
112 lines
3.8 KiB
Java
112 lines
3.8 KiB
Java
package com.example.demo.controller;
|
|
|
|
import java.security.Principal;
|
|
import java.util.List;
|
|
|
|
import com.example.demo.dto.RecipeDto;
|
|
import com.example.demo.service.RecipeService;
|
|
import com.example.demo.service.UserService;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
@Controller
|
|
public class SiteController {
|
|
|
|
private final RecipeService recipeService;
|
|
private final UserService userService;
|
|
|
|
public SiteController(RecipeService recipeService, UserService userService) {
|
|
this.recipeService = recipeService;
|
|
this.userService = userService;
|
|
}
|
|
|
|
@GetMapping("/")
|
|
public String viewHomePage() {
|
|
return "redirect:/explore";
|
|
}
|
|
|
|
@GetMapping("/favorites")
|
|
public String viewFavorites(Model model, Principal principal) {
|
|
model.addAttribute("guest", principal == null);
|
|
model.addAttribute("newestRecipes", recipeService.getNewestRecipes(5));
|
|
|
|
if (principal == null) {
|
|
model.addAttribute("recipes", List.of());
|
|
} else {
|
|
model.addAttribute("recipes", userService.getFavoriteRecipesByUsername(principal.getName()));
|
|
}
|
|
|
|
return "home";
|
|
}
|
|
|
|
@GetMapping("/login")
|
|
public String viewLoginPage(@RequestParam(required = false) String redirect, Model model) {
|
|
model.addAttribute("redirect", redirect);
|
|
return "login";
|
|
}
|
|
|
|
@GetMapping("/register")
|
|
public String viewRegisterPage(Model model) {
|
|
return "create-account";
|
|
}
|
|
|
|
@GetMapping("/create")
|
|
public String viewCreatePage(Model model) {
|
|
return "create-recipe";
|
|
}
|
|
|
|
@GetMapping("/recipes/{id}")
|
|
public String viewRecipe(@PathVariable Integer id, Model model) {
|
|
RecipeDto recipe = recipeService.getRecipeById(id);
|
|
model.addAttribute("recipe", recipe);
|
|
return "view-recipe";
|
|
}
|
|
|
|
@GetMapping("/recipes/{id}/edit")
|
|
public String viewEditRecipePage(@PathVariable Integer id, Model model) {
|
|
RecipeDto recipe = recipeService.getRecipeById(id);
|
|
model.addAttribute("recipe", recipe);
|
|
return "update-recipe";
|
|
}
|
|
|
|
@GetMapping("/update-recipe/{id}")
|
|
public String showUpdateRecipePage(@PathVariable Integer id, Model model) {
|
|
RecipeDto recipe = recipeService.getRecipeById(id);
|
|
model.addAttribute("recipe", recipe);
|
|
return "update-recipe";
|
|
}
|
|
|
|
@GetMapping("/explore")
|
|
public String explore(
|
|
@RequestParam(required = false) String q,
|
|
@RequestParam(required = false) List<String> tags,
|
|
@RequestParam(required = false) List<Integer> prices,
|
|
@RequestParam(required = false) List<Integer> cookTime,
|
|
@RequestParam(required = false) List<Integer> prepTime,
|
|
Model model,
|
|
Principal principal
|
|
) {
|
|
List<RecipeDto> recipes = recipeService.getRecipes(q, tags, prices, cookTime, prepTime);
|
|
model.addAttribute("recipes", recipes);
|
|
model.addAttribute("q", q);
|
|
model.addAttribute("tags", tags);
|
|
model.addAttribute("guest", principal == null);
|
|
return "explore";
|
|
}
|
|
|
|
@PostMapping("/api/recipes/{id}/image")
|
|
@ResponseBody
|
|
public ResponseEntity<?> updateRecipeImage(@PathVariable Integer id,
|
|
@RequestParam("image") MultipartFile image) {
|
|
recipeService.updateRecipeImage(id, image);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
} |