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}
0 コメント