LaVOZs

The World’s Largest Online Community for Developers

'; html - Add opening hours to restaurant java spring one to many relationship - LavOzs.Com

BACKEND: here is my backend RestraurantService class CreateRestaurantclass which has a list of OpeningHours:

public void createRestaurant(@Valid RestaurantCreateRequest restaurantCreateRequest){
        String email = SecurityContextHolder.getContext().getAuthentication().getName();
        RestaurantOwner restaurantOwner = restaurantOwnerService.findByEmail(email);
        boolean restaurantAlreadyExistWithThisName = restaurantAlreadyExistWithThisName(restaurantCreateRequest.getRestaurantName(),restaurantOwner.getId());
        if (restaurantAlreadyExistWithThisName){
            throw new RestaurantNotCreatedException("This name already exists, cannot create, please choose another one");
        }

        List<OpeningHours> openingHours = new ArrayList<>();
        List<OpeningHoursCreateRequest> openingHoursRequest = restaurantCreateRequest.getOpeningHoursList();
        for (OpeningHoursCreateRequest openingHoursCreateRequest : openingHoursRequest) {
            openingHours.add(OpeningHours.builder()
                    .withDay(openingHoursCreateRequest.getDay())
                    .withStartingHours(openingHoursCreateRequest.getStartingHours())
                    .withClosingHours(openingHoursCreateRequest.getClosingHours())
                    .build());
        }
        UUID uuid = UUID.randomUUID();
        Restaurant restaurant = Restaurant.builder()
                .withUuid(uuid)
                .withRestaurantName(restaurantCreateRequest.getRestaurantName())
                .withRestaurantOwner(restaurantOwner)
                .withOpeningHours(openingHours)
                .build();
        restaurantRepository.save(restaurant);
    }

BACKEND CONTROLLER: here is my backend Controller createRestaurant method

@PostMapping("/restaurants")
public void createRestaurant( @RequestBody RestaurantCreateRequest restaurantCreateRequest) throws IOException {
        restaurantService.createRestaurant(restaurantCreateRequest);
    }

Here is json in put

{
    "restaurantName" : "test restfeo",
    "openingHoursList" : [
        {
        "day" : "Monday",
        "startingHours" : "12:00",
        "closingHours" : "22:00"    
        },
        {
        "day" : "Tuesday",
        "startingHours" : "12:00",
        "closingHours" : "22:00"

        }
        ,
        {
        "day" : "Wednessay",
        "startingHours" : "12:00",
        "closingHours" : "22:00"

        },
        {
        "day" : "Thursday",
        "startingHours" : "12:00",
        "closingHours" : "22:00"

        },
        {
        "day" : "Friday",
        "startingHours" : "12:00",
        "closingHours" : "22:00"

        },
        {
        "day" : "Saturday",
        "startingHours" : "12:00",
        "closingHours" : "22:00"

        },
        {
        "day" : "Sunday",
        "startingHours" : "12:00",
        "closingHours" : "22:00"

        },
        {
        "day" : "closed",
        "startingHours" : "closed",
        "closingHours" : "closed"

        }

        ]
}

** untill here everything work very fine, i can give a list of openinghours to controller which can add data to database **

BUT

its been more than one week i a am trying to send data from html to json and its not working Really need help

here is my clientside coding

** Here is client side : Restaurant client**

public Optional<String> createRestaurant(RestaurantCreateRequest restaurantCreateRequest) throws IOException {
        try {
            restTemplate.postForEntity(backendHost + "restaurants", restaurantCreateRequest, null);
        } catch (HttpClientErrorException e) {
            return Optional.of(ErrorMessageMapper.getErrorMessage(e));
        }
        return Optional.empty();
    }

** Here is restaurant controller **

 @PostMapping("/restaurants")
    public String addRestaurantToRestaurantOwner(Model model,
                                                 @Valid @ModelAttribute("restaurantCreateRequest") RestaurantCreateRequest restaurantCreateRequest,
                                                 BindingResult bindingResult, HttpSession httpSession) throws IOException {
        if (bindingResult.hasErrors()) {
            return "restaurants/create-restaurant";
        }
        Optional<String> error = restaurantClient.createRestaurant(restaurantCreateRequest);
        if(error.isPresent()){
            model.addAttribute("nameError", true);
            return "restaurants/create-restaurant";
        }

        return "redirect:/restaurantOwner/restaurantOwner-dashboard";
    }

** I also tried to store openinghours data to http session but i failed**

@PostMapping("/addOpeningHoursToList")
    public String add(Model model, @Valid @ModelAttribute("restaurantCreateRequest") RestaurantCreateRequest restaurantCreateRequest, HttpSession httpSession, BindingResult bindingResult) {

        httpSession.setAttribute("openingHours", new RestaurantCreateRequest().getOpeningHoursList());
        return "restaurant/create-restaurant";

    }

** findally Html **


<form th:action="@{/restaurants}" method="post">
                <div>
                    <label for="restaurantName">Restaurant name:</label>
                    <input type="text" id="restaurantName"  th:field="${restaurantCreateRequest.restaurantName}" required>
                    <span th:if="${nameError}">Restaurant already in use</span>
                </div>
            </form>
            <form id="list" action="@{/addOpeningHoursToList}">
                <div>
                    <h>Opening Hours</h>
                </div>
                <div>
                    <div>
                        <select type="text" id="day" name="day" th:text="${#httpSession.openingHours}">
                            <option>Monday</option>
                            <option>Tuesday</option>
                            <option>Wednesday</option>
                            <option>Thursday</option>
                            <option>Friday</option>
                            <option>Saturday</option>
                            <option>Sunday</option>
                        </select>
                    </div>
                    <div>
                        <input type="text" id="openingHours" name="openingHours" th:field="${httpSession.openingHours.openingHours}">
                    </div>
                    <div>
                        <input type="text" id="closingHours" name="closingHours" th:field="${httpSession.openingHours.closingHours}">
                    </div>
                    <div>
                        <input type="submit" value="Submit">
                    </div>
                </div>
            </form>

Thanks in advance for your help ::: i am unable to create a list of restaurant hours and pass it to the controller

Related
why spill failure happens for Custom Data Type in Hadoop
Property on model attribute set in one controller method is not available in another controller method
How to render partial view in Spring MVC
Cannot display HTML string
Eliminate circular JSON in Java Spring Many to Many Relationship
(Spring, Thymeleaf) How to request to controller 'POST' with list of model inside a model?
Problem with Spring many to many relationship
th:each indexing throwing errors