実現したいこと
登録されている店舗の情報を編集するページを作成しているのですが、編集内容を記入する際に登録されている情報を事前にフォームに書かれているようにしたいので、情報を取得できるようにしたいです。
発生している問題・分からないこと
紐づけているテーブルのデータの呼び出しがうまくいきません。
該当のソースコード
AdminRestaurantController.java
1@Controller 2@RequestMapping("/admin/restaurants") 3public class AdminRestaurantController { 4 private final RestaurantRepository restaurantRepository; 5 private final RestaurantService restaurantService; 6 7 @Autowired 8 private CategoryRepository categoryRepository; 9 10 @Autowired 11 private RegularHolidayRepository regularHolidayRepository; 12 13 @Autowired 14 private final PictureRepository pictureRepository; 15 private PictureRegisterForm pictureRegisterForm; 16 private CategoryRegisterForm categoryRegisterForm; 17 private RegularHolidayRegisterForm regularHolidayRegisterForm; 18 19 public AdminRestaurantController(RestaurantRepository restaurantRepository, PictureRepository pictureRepository, RestaurantService restaurantService) { 20 this.restaurantRepository = restaurantRepository; 21 this.pictureRepository = pictureRepository; 22 this.restaurantService = restaurantService; 23 } 24 25 @GetMapping 26 public String index(Model model, @PageableDefault(page = 0, size = 10, sort = "id", direction = Direction.ASC) Pageable pageable, @RequestParam(name = "keyword", required = false) String keyword, @RequestParam(name = "word", required = false) String word) { 27 Page<Restaurant> restaurantPage; 28 29 if (keyword != null && !keyword.isEmpty()) { 30 restaurantPage = restaurantRepository.findByNameLike("%" + keyword + "%", pageable); 31 } else { 32 restaurantPage = restaurantRepository.findAll(pageable); 33 } 34 35 if (word != null && !word.isEmpty()) { 36 restaurantPage = restaurantRepository.findByNameLike("%" + word + "%", pageable); 37 } else { 38 restaurantPage = restaurantRepository.findAll(pageable); 39 } 40 41 model.addAttribute("restaurantPage", restaurantPage); 42 model.addAttribute("keyword", keyword); 43 model.addAttribute("word", word); 44 45 return "admin/restaurants/index"; 46 } 47 48 @GetMapping("/{id}") 49 public String show(@PathVariable(name = "id") Integer id, Model model, Pageable pageable) { 50 Restaurant restaurant = restaurantRepository.findById(id).orElse(null); 51 List<Category> categories = restaurantRepository.findByCategoriesId(id); 52 List<RegularHoliday> regularHolidays = restaurantRepository.findByRegularHolidaysId(id); 53 List<Picture> pictures = pictureRepository.findByRestaurantId(id); 54 55 model.addAttribute("restaurant", restaurant); 56 model.addAttribute("categories", categories); 57 model.addAttribute("regularHolidays", regularHolidays); 58 model.addAttribute("pictures", pictures); 59 60 return "admin/restaurants/show"; 61 } 62 63 @GetMapping("/register") 64 public String register(Model model) { 65 model.addAttribute("restaurantRegisterForm", new RestaurantRegisterForm()); 66 model.addAttribute("imageFilter", new ImageFilter()); 67 model.addAttribute("pictureRegisterForm", new PictureRegisterForm()); 68 69 //model.addAttribute(BindingResult.MODEL_KEY_PREFIX + "restaurantRegisterForm", new BeanPropertyBindingResult(new RestaurantRegisterForm(), "restaurantRegisterForm")); 70 71 List<Category> categories = categoryRepository.findAll(); 72 model.addAttribute("categories", categories); 73 74 List<RegularHoliday> regularHolidays = regularHolidayRepository.findAll(); 75 model.addAttribute("regularHolidays", regularHolidays); 76 77 return "admin/restaurants/register"; 78 } 79 80 @PostMapping("/create") 81 public String create(@ModelAttribute @Validated RestaurantRegisterForm restaurantRegisterForm, BindingResult bindingResult, RedirectAttributes redirectAttributes) { 82 if (bindingResult.hasErrors()) { 83 return "admin/restaurants/register"; 84 } 85 PictureRegisterForm pictureRegisterForm = new PictureRegisterForm(); 86 RegularHolidayRegisterForm regularHolidayRegisterForm = new RegularHolidayRegisterForm(); 87 CategoryRegisterForm categoryRegisterForm = new CategoryRegisterForm(); 88 restaurantService.create(restaurantRegisterForm, pictureRegisterForm, categoryRegisterForm, regularHolidayRegisterForm); 89 redirectAttributes.addFlashAttribute("successMessage", "新店舗を登録しました。"); 90 91 return "redirect:/admin/restaurants"; 92 } 93 94 @GetMapping("/{id}/edit") 95 public String edit(@PathVariable(name = "id") Integer id, Model model) { 96 Restaurant restaurant = restaurantRepository.getReferenceById(id); 97 Picture picture = pictureRepository.getReferenceById(id); 98 Category category = categoryRepository.getReferenceById(id); 99 RegularHoliday regularHoliday = regularHolidayRepository.getReferenceById(id); 100 String image = restaurant.getImage(); 101 List<MultipartFile> photoFiles; 102 103 List<Integer> categoryIds = restaurant.getCategories().stream() 104 .map(Category::getId) 105 .collect(Collectors.toList()); 106 List<Integer> regularHolidayIds = restaurant.getRegularHolidays().stream() 107 .map(RegularHoliday::getId) 108 .collect(Collectors.toList()); 109 110 RestaurantEditForm restaurantEditForm = new RestaurantEditForm( 111 restaurant.getId(), 112 restaurant.getName(), 113 restaurant.getCategories().getId(), 114 null, 115 null, 116 restaurant.getDescription(), 117 restaurant.getLowestPrice(), 118 restaurant.getHighestPrice(), 119 restaurant.getPostalCode(), 120 restaurant.getAddress(), 121 restaurant.getAccess(), 122 restaurant.getOpeningTime(), 123 restaurant.getClosingTime(), 124 restaurant.getPhoneNumber(), 125 restaurant.getSeatingCapacity(), 126 restaurant.getRegularHolidays().getId() 127 ); 128 129 restaurantEditForm.setCategoryIds(categoryIds); 130 restaurantEditForm.setRegularHolidayIds(regularHolidayIds); 131 132 model.addAttribute("image", image); 133 model.addAttribute("photoFiles", photoFiles); 134 model.addAttribute("restaurantEditForm", restaurantEditForm); 135 model.addAttribute("categories", categoryRepository.findAll()); 136 model.addAttribute("regularHolidays", regularHolidayRepository.findAll()); 137 138 return "admin/restaurants/edit"; 139 } 140}
RestaurantEditForm.java
1@Data 2@AllArgsConstructor 3public class RestaurantEditForm { 4 @NotBlank(message = "店舗名を入力してください。") 5 private String name; 6 7 @NotNull(message = "カテゴリを選択してください。") 8 private List<Integer> categoryIds; 9 10 private MultipartFile ImageFile; 11 12 private List<MultipartFile> photoFiles; 13 14 @NotBlank(message = "説明を入力してください。") 15 private String description; 16 17 @NotNull(message = "最低価格を入力してください。") 18 @Min(value = 1, message = "最低価格は1円以上に設定してください。") 19 private Integer lowestPrice; 20 21 @NotNull(message = "最高価格を入力してください。") 22 @Min(value = 1, message = "最高価格は1円以上に設定してください。") 23 private Integer highestPrice; 24 25 @NotBlank(message = "郵便番号を入力してください。(〒は必要ありません)") 26 private String postalCode; 27 28 @NotBlank(message = "住所を入力してください。") 29 private String address; 30 31 @NotBlank(message = "アクセス方法を入力してください。") 32 private String access; 33 34 @NotNull(message = "開店時間を入力してください。(例:12:00)") 35 @DateTimeFormat(pattern = "HH:mm") 36 private LocalTime openingTime; 37 38 @NotNull(message = "閉店時間を入力してください。(例:23:00)") 39 @DateTimeFormat(pattern = "HH:mm") 40 private LocalTime closingTime; 41 42 @NotBlank(message = "電話番号を入力してください。(例:012-345-678)") 43 private String phoneNumber; 44 45 @NotNull(message = "座席数を入力してください。") 46 @Min(value = 1, message = "座席数は1席以上に設定してください。") 47 private Integer seatingCapacity; 48 49 @NotNull(message = "定休日を選択してください。") 50 private List<Integer> regularHolidayIds; 51 52 public List<Integer> getCategoryIds() { 53 return categoryIds; 54 } 55 56 public void setCategoryIds(List<Integer> categoryIds) { 57 this.categoryIds = categoryIds; 58 } 59 60 public List<Integer> getRegularHolidayIds() { 61 return regularHolidayIds; 62 } 63 64 public void setRegularHolidayIds(List<Integer> regularHolidayIds) { 65 this.regularHolidayIds = regularHolidayIds; 66 } 67 68}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
categoryIdやregularHolidayIdの部分は、AdminRestaurantController.javaのeditのところで、restaurant.getCategoruId(),にしたり、regularHolidayIdも同じように書いたのですがうまくいかずでした。
補足
特になし
回答1件
あなたの回答
tips
プレビュー