mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
create account and recipe working. Explore kind of works
This commit is contained in:
@@ -36,7 +36,8 @@ public class RecipeController {
|
|||||||
|
|
||||||
// build create recipe REST API
|
// build create recipe REST API
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<RecipeDto> saveRecipe(@RequestBody RecipeDto recipeDto, Authentication authentication) {
|
public ResponseEntity<RecipeDto> saveRecipe(@Valid @RequestBody Recipe recipe, Authentication authentication) {
|
||||||
|
RecipeDto recipeDto = recipeService.convertToDto(recipe);
|
||||||
String currentUsername = authentication.getName();
|
String currentUsername = authentication.getName();
|
||||||
return new ResponseEntity<>(recipeService.saveRecipe(recipeDto, currentUsername), HttpStatus.CREATED);
|
return new ResponseEntity<>(recipeService.saveRecipe(recipeDto, currentUsername), HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Controller;
|
|||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class SiteController {
|
public class SiteController {
|
||||||
@@ -49,12 +50,25 @@ public class SiteController {
|
|||||||
return "view-recipe";
|
return "view-recipe";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @GetMapping("/explore")
|
||||||
|
// public String viewExplorePage(Model model) {
|
||||||
|
// //model.addAttribute("allemplist", employeeServiceImpl.getAllEmployee());
|
||||||
|
// List<RecipeDto> recipes = recipeService.getAllRecipes();
|
||||||
|
// model.addAttribute("recipes", recipes);
|
||||||
|
// return "explore";
|
||||||
|
// }
|
||||||
|
|
||||||
@GetMapping("/explore")
|
@GetMapping("/explore")
|
||||||
public String viewExplorePage(Model model) {
|
public String explore(
|
||||||
//model.addAttribute("allemplist", employeeServiceImpl.getAllEmployee());
|
@RequestParam(required = false) String q,
|
||||||
List<RecipeDto> recipes = recipeService.getAllRecipes();
|
@RequestParam(required = false) List<String> tags,
|
||||||
model.addAttribute("recipes", recipes);
|
Model model
|
||||||
|
) {
|
||||||
|
List<RecipeDto> recipes = recipeService.getRecipes(q, tags);
|
||||||
|
model.addAttribute("recipes", recipes);
|
||||||
|
model.addAttribute("q", q);
|
||||||
return "explore";
|
return "explore";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,8 +4,11 @@ import java.security.Principal;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -33,11 +36,11 @@ public class UserController {
|
|||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.userRepo = userRepo;
|
this.userRepo = userRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// build create user REST API
|
// build create user REST API
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<User> saveUser(@RequestBody User user) {
|
public ResponseEntity<User> saveUser(@RequestBody User user) {
|
||||||
|
|
||||||
return new ResponseEntity<User>(userService.saveUser(user), HttpStatus.CREATED);
|
return new ResponseEntity<User>(userService.saveUser(user), HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ public class RecipeServiceImpl implements RecipeService {
|
|||||||
ensureUserNotBanned(currentUser);
|
ensureUserNotBanned(currentUser);
|
||||||
enforceUploadLimit(currentUser);
|
enforceUploadLimit(currentUser);
|
||||||
|
|
||||||
|
|
||||||
Recipe recipe = new Recipe(dto.getTitle(), dto.getDescription(), dto.getPrepTimeMinutes(),
|
Recipe recipe = new Recipe(dto.getTitle(), dto.getDescription(), dto.getPrepTimeMinutes(),
|
||||||
dto.getCookTimeMinutes(), dto.getServings(), currentUser, dto.getStatus());
|
dto.getCookTimeMinutes(), dto.getServings(), currentUser, dto.getStatus());
|
||||||
|
|
||||||
@@ -146,6 +147,7 @@ public class RecipeServiceImpl implements RecipeService {
|
|||||||
riDto.getNotes());
|
riDto.getNotes());
|
||||||
|
|
||||||
recipe.getRecipeIngredients().add(ri);
|
recipe.getRecipeIngredients().add(ri);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,8 +374,8 @@ public class RecipeServiceImpl implements RecipeService {
|
|||||||
public List<RecipeDto> getRecipes(String name, List<String> tags) {
|
public List<RecipeDto> getRecipes(String name, List<String> tags) {
|
||||||
|
|
||||||
List<Recipe> recipes;
|
List<Recipe> recipes;
|
||||||
|
|
||||||
if (!name.isBlank()) {
|
if ((name != null) && (!name.isBlank())) {
|
||||||
recipes = recipeRepository.findByTitleContainingIgnoreCase(name);
|
recipes = recipeRepository.findByTitleContainingIgnoreCase(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,7 +383,7 @@ public class RecipeServiceImpl implements RecipeService {
|
|||||||
recipes = recipeRepository.findAll();
|
recipes = recipeRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tags.isEmpty() && !recipes.isEmpty()) {
|
if ((tags != null) && (!tags.isEmpty()) && !recipes.isEmpty()) {
|
||||||
recipes = recipes.stream()
|
recipes = recipes.stream()
|
||||||
.filter(recipe -> recipe.getTags().stream().anyMatch(tag -> tags.contains(tag.getName())))
|
.filter(recipe -> recipe.getTags().stream().anyMatch(tag -> tags.contains(tag.getName())))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.example.demo.dto.ImageDto;
|
import com.example.demo.dto.ImageDto;
|
||||||
@@ -26,11 +29,13 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
private UserRepo userRepository;
|
private UserRepo userRepository;
|
||||||
private RecipeRepo recipeRepository;
|
private RecipeRepo recipeRepository;
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository) {
|
public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository, PasswordEncoder passwordEncoder) {
|
||||||
super();
|
super();
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.recipeRepository = recipeRepository;
|
this.recipeRepository = recipeRepository;
|
||||||
|
this.passwordEncoder = passwordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserDto convertToDto(User user) {
|
public UserDto convertToDto(User user) {
|
||||||
@@ -43,6 +48,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
user.setRole("ROLE_USER");
|
user.setRole("ROLE_USER");
|
||||||
}
|
}
|
||||||
user.setBanned(false);
|
user.setBanned(false);
|
||||||
|
user.setHashedpassword(passwordEncoder.encode(user.getPassword()));
|
||||||
|
|
||||||
|
|
||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -191,15 +191,18 @@ body, html {
|
|||||||
.main-content {
|
.main-content {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
display: flex;
|
/*display: flex; this line was breaking the searched results. They returned recipes would not load at the top of the page*/
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
align-items: flex-start;
|
align-items: flex-start;
|
||||||
overflow: scroll;
|
overflow: auto;
|
||||||
scrollbar-color: var(--dusty-red) var(--pale-yellow);
|
scrollbar-color: var(--dusty-red) var(--pale-yellow);
|
||||||
height: 100%;
|
height: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* safari and old browsers*/
|
/* safari and old browsers*/
|
||||||
::-webkit-scrollbar-track {
|
::-webkit-scrollbar-track {
|
||||||
background: var(--pale-yellow);
|
background: var(--pale-yellow);
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
|
||||||
|
<meta name="_csrf" th:content="${_csrf.token}"/>
|
||||||
<title>Create Thyme Crunch Account</title>
|
<title>Create Thyme Crunch Account</title>
|
||||||
<link rel="stylesheet" th:href="@{css/create-account.css}">
|
<link rel="stylesheet" th:href="@{css/create-account.css}">
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Delius+Swash+Caps&family=Mali:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200;1,300;1,400;1,500;1,600;1,700" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Delius+Swash+Caps&family=Mali:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200;1,300;1,400;1,500;1,600;1,700" rel="stylesheet">
|
||||||
@@ -85,12 +88,17 @@
|
|||||||
hashedpassword: password,
|
hashedpassword: password,
|
||||||
role: "USER"
|
role: "USER"
|
||||||
};
|
};
|
||||||
|
const csrfToken = document.querySelector('meta[name="_csrf"]').getAttribute('content');
|
||||||
|
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
||||||
|
|
||||||
|
console.log("JSON to submit:", JSON.stringify(userData, null, 2));
|
||||||
|
|
||||||
fetch("http://localhost:8080/api/users", {
|
fetch("http://localhost:8080/api/users", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json"
|
[csrfHeader]: csrfToken,
|
||||||
},
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
body: JSON.stringify(userData)
|
body: JSON.stringify(userData)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -40,10 +40,10 @@
|
|||||||
<!-- Main Content -->
|
<!-- Main Content -->
|
||||||
<main class="main-content">
|
<main class="main-content">
|
||||||
<div class="search-bar">
|
<div class="search-bar">
|
||||||
<form action="/search" method="get">
|
<form action="/explore" method="get">
|
||||||
<label for="site-search">Search:</label>
|
<label for="site-search">Search:</label>
|
||||||
<div class="search-btn">
|
<div class="search-btn">
|
||||||
<input type="search" id="site-search" name="q" placeholder="Search for recipes...">
|
<input type="search" id="site-search" name="q" th:value="${q}" placeholder="Search for recipes...">
|
||||||
<button type="submit"><i class="fa fa-search"></i></button>
|
<button type="submit"><i class="fa fa-search"></i></button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@@ -68,8 +68,29 @@
|
|||||||
<div class="body-right">
|
<div class="body-right">
|
||||||
<div class="sidebar-right">
|
<div class="sidebar-right">
|
||||||
<h1> FILTER </h1>
|
<h1> FILTER </h1>
|
||||||
|
<form id="filter-form">
|
||||||
|
<h3>Price</h3>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="price" value="1"> $
|
||||||
|
</label><br>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="price" value="2"> $$
|
||||||
|
</label><br>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="price" value="3"> $$$
|
||||||
|
</label><br>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="price" value="4"> $$$$
|
||||||
|
</label>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<script>
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user