create account and recipe working. Explore kind of works

This commit is contained in:
durn
2026-04-17 10:46:38 -06:00
parent b32b3d3a03
commit b26af70ef5
8 changed files with 78 additions and 18 deletions
@@ -36,7 +36,8 @@ public class RecipeController {
// build create recipe REST API
@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();
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class SiteController {
@@ -49,12 +50,25 @@ public class SiteController {
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")
public String viewExplorePage(Model model) {
//model.addAttribute("allemplist", employeeServiceImpl.getAllEmployee());
List<RecipeDto> recipes = recipeService.getAllRecipes();
model.addAttribute("recipes", recipes);
public String explore(
@RequestParam(required = false) String q,
@RequestParam(required = false) List<String> tags,
Model model
) {
List<RecipeDto> recipes = recipeService.getRecipes(q, tags);
model.addAttribute("recipes", recipes);
model.addAttribute("q", q);
return "explore";
}
}
@@ -4,8 +4,11 @@ import java.security.Principal;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -33,11 +36,11 @@ public class UserController {
this.userService = userService;
this.userRepo = userRepo;
}
// build create user REST API
@PostMapping
public ResponseEntity<User> saveUser(@RequestBody User user) {
return new ResponseEntity<User>(userService.saveUser(user), HttpStatus.CREATED);
}
@@ -129,6 +129,7 @@ public class RecipeServiceImpl implements RecipeService {
ensureUserNotBanned(currentUser);
enforceUploadLimit(currentUser);
Recipe recipe = new Recipe(dto.getTitle(), dto.getDescription(), dto.getPrepTimeMinutes(),
dto.getCookTimeMinutes(), dto.getServings(), currentUser, dto.getStatus());
@@ -146,6 +147,7 @@ public class RecipeServiceImpl implements RecipeService {
riDto.getNotes());
recipe.getRecipeIngredients().add(ri);
}
}
@@ -372,8 +374,8 @@ public class RecipeServiceImpl implements RecipeService {
public List<RecipeDto> getRecipes(String name, List<String> tags) {
List<Recipe> recipes;
if (!name.isBlank()) {
if ((name != null) && (!name.isBlank())) {
recipes = recipeRepository.findByTitleContainingIgnoreCase(name);
}
@@ -381,7 +383,7 @@ public class RecipeServiceImpl implements RecipeService {
recipes = recipeRepository.findAll();
}
if (!tags.isEmpty() && !recipes.isEmpty()) {
if ((tags != null) && (!tags.isEmpty()) && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> recipe.getTags().stream().anyMatch(tag -> tags.contains(tag.getName())))
.collect(Collectors.toList());
@@ -4,6 +4,9 @@ import java.util.ArrayList;
import java.util.List;
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 com.example.demo.dto.ImageDto;
@@ -26,11 +29,13 @@ public class UserServiceImpl implements UserService {
private UserRepo userRepository;
private RecipeRepo recipeRepository;
private PasswordEncoder passwordEncoder;
public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository) {
public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository, PasswordEncoder passwordEncoder) {
super();
this.userRepository = userRepository;
this.recipeRepository = recipeRepository;
this.passwordEncoder = passwordEncoder;
}
public UserDto convertToDto(User user) {
@@ -43,6 +48,9 @@ public class UserServiceImpl implements UserService {
user.setRole("ROLE_USER");
}
user.setBanned(false);
user.setHashedpassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}