Changeset 65b6638
- Timestamp:
- 02/28/24 18:44:19 (15 months ago)
- Branches:
- main
- Children:
- 75f5086
- Parents:
- d24f17c
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/App.js
rd24f17c r65b6638 11 11 import ReservationConfirmation from "./components/ReservationConfirmation"; 12 12 import ReservationEdit from "./components/ReservationEdit"; 13 import axios from "axios"; 13 14 14 15 const App = () => { … … 50 51 const isToday = selectedDate.toDateString() === today.toDateString(); 51 52 52 const startHour = isToday ? today.getHours() : 9; 53 const startMinute = isToday ? Math.ceil(today.getMinutes() / 15) * 15 : 0; 53 // Determine the start hour and minute 54 let startHour = 9; 55 let startMinute = 0; 56 if (isToday) { 57 const currentHour = today.getHours(); 58 const currentMinute = today.getMinutes(); 59 // If current time is later than 09:00, start from the current hour and minute 60 if (currentHour > 9 || (currentHour === 9 && currentMinute >= 0)) { 61 startHour = currentHour; 62 startMinute = Math.ceil(currentMinute / 15) * 15; 63 } 64 } 54 65 55 let currentTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), startHour, startMinute); 66 // Create the start time and end time 67 const startTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), startHour, startMinute); 56 68 const endTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), 23, 30); 57 69 70 // Generate time slots from start time to end time in 15-minute intervals 58 71 const slots = []; 72 let currentTime = new Date(startTime); 59 73 while (currentTime <= endTime) { 60 74 const option = currentTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false }); … … 63 77 } 64 78 79 // Update the timeSlots state 65 80 setTimeSlots(slots); 66 81 } 67 82 }, [date]); 83 68 84 69 85 const handleDateChange = (e) => { … … 83 99 }; 84 100 85 const handleSubmit = (e) => {101 const handleSubmit = async (e) => { 86 102 e.preventDefault(); 87 console.log(date); 88 console.log(selectedTime); 89 console.log(numPeople); 90 console.log(searchValue); 103 const [year, month, day] = date.split("-"); 104 105 let formattedDateTime; 106 const [selectedHours, selectedMinutes] = selectedTime.split(":"); 107 // Check if selectedHours and selectedMinutes are valid numbers 108 if (!isNaN(selectedHours) && !isNaN(selectedMinutes)) { 109 const dateTime = new Date(Date.UTC(year, month - 1, day, selectedHours, selectedMinutes)); 110 formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' '); 111 } else { 112 // Default values if selectedTime is not valid 113 const now = new Date(); 114 let defaultTime; 115 if (now.getHours() >= 9 && now.getHours() <= 23) { 116 defaultTime = now; 117 } else { 118 defaultTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 9, 0); // Set to 09:00 if current time is before 09:00 119 } 120 const dateTime = new Date(Date.UTC(year, month - 1, day, defaultTime.getHours(), defaultTime.getMinutes())); 121 formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' '); 122 } 123 124 const data = { 125 dateTime: formattedDateTime, 126 partySize: numPeople, 127 search: searchValue 128 }; 129 130 console.log("pecatam data pod mene") 131 console.log(data) 132 133 try { 134 const response = await axios.post('http://localhost:8080/api/search', data); 135 const filteredRestaurants = response.data; 136 console.log(filteredRestaurants); 137 // Handle response accordingly 138 } catch (error) { 139 console.error('Error:', error); 140 } 91 141 }; 92 142 93 const today = new Date().toISOString().split('T')[0]; 143 144 145 // Rest of your component code... 146 147 const today = new Date(); 148 const year = today.getFullYear(); 149 const month = String(today.getMonth() + 1).padStart(2, '0'); 150 const day = String(today.getDate()).padStart(2, '0'); 151 const formattedDate = `${year}-${month}-${day}`; 152 94 153 95 154 return ( … … 100 159 <div className="col-auto"> 101 160 <input className="form-control me-2" type="date" value={date} onChange={handleDateChange} 102 min={ today}/>161 min={formattedDate}/> 103 162 </div> 104 163 <div className="col-auto"> -
src/main/java/com/example/rezevirajmasa/demo/repository/RestaurantRepository.java
rd24f17c r65b6638 8 8 9 9 public interface RestaurantRepository extends JpaRepository<Restaurant, Long> { 10 11 10 12 } -
src/main/java/com/example/rezevirajmasa/demo/service/RestaurantService.java
rd24f17c r65b6638 2 2 3 3 import com.example.rezevirajmasa.demo.model.Restaurant; 4 import com.example.rezevirajmasa.demo.model.TableEntity; 4 5 5 6 import java.math.BigDecimal; 6 7 import java.time.LocalDate; 8 import java.time.LocalDateTime; 7 9 import java.util.List; 8 10 … … 18 20 List<Restaurant> listRestaurantBy(String search); 19 21 List<Restaurant> getRestaurantsWithAvailableTimeSlotsForToday(); 22 public List<Restaurant> findRestaurantsByDateTimeAndPartySize(LocalDateTime dateTime, int partySize, String search); 20 23 } -
src/main/java/com/example/rezevirajmasa/demo/service/impl/RestaurantServiceImpl.java
rd24f17c r65b6638 143 143 return restaurantsWithAvailableTimeSlots; 144 144 } 145 146 @Override 147 public List<Restaurant> findRestaurantsByDateTimeAndPartySize(LocalDateTime dateTime, int partySize, String search) { 148 List<Restaurant> allRestaurants = restaurantRepository.findAll(); 149 return allRestaurants.stream() 150 .filter(restaurant -> hasAvailableTable(restaurant, dateTime, partySize)) 151 .filter(restaurant -> isMatch(restaurant, search)) 152 .collect(Collectors.toList()); 153 } 154 155 private boolean hasAvailableTable(Restaurant restaurant, LocalDateTime dateTime, int partySize) { 156 for (TableEntity table : restaurant.getTablesList()) { 157 if (table.isAvailable(dateTime) && table.getCapacity() >= partySize) { 158 return true; 159 } 160 } 161 return false; 162 } 163 164 private boolean isMatch(Restaurant restaurant, String name) { 165 return name == null || name.isEmpty() || restaurant.getName().contains(name); 166 } 145 167 } -
src/main/java/com/example/rezevirajmasa/demo/web/rest/testController.java
rd24f17c r65b6638 6 6 import com.example.rezevirajmasa.demo.service.RestaurantService; 7 7 import com.example.rezevirajmasa.demo.service.TableService; 8 import jdk.jfr.consumer.RecordingStream; 8 9 import org.apache.coyote.Response; 10 import org.springframework.format.annotation.DateTimeFormat; 9 11 import org.springframework.http.HttpStatus; 10 12 import org.springframework.http.ResponseEntity; … … 12 14 13 15 import java.time.LocalDateTime; 16 import java.time.format.DateTimeFormatter; 14 17 import java.util.List; 18 import java.util.Map; 15 19 16 20 @CrossOrigin(origins = "http://localhost:3000/") … … 34 38 } 35 39 40 //restaurants 41 36 42 @RequestMapping("/api/restaurants") 37 43 public ResponseEntity<List<Restaurant>> getAllRestaurants() { … … 42 48 public ResponseEntity<Restaurant> getRestaurantById(@PathVariable Long restaurantId) { 43 49 return new ResponseEntity<Restaurant>(restaurantService.findById(restaurantId), HttpStatus.OK); 50 } 51 52 @PostMapping("/api/search") 53 public ResponseEntity<List<Restaurant>> searchRestaurants(@RequestBody Map<String, Object> requestData) { 54 String dateTime = (String) requestData.get("dateTime"); 55 Integer partySize = (Integer) requestData.get("partySize"); 56 String search = (String) requestData.get("search"); 57 58 // Now proceed with parsing dateTime and performing the search based on the received parameters 59 60 LocalDateTime parsedDateTime = null; 61 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); 62 if (dateTime != null) { 63 parsedDateTime = LocalDateTime.parse(dateTime, formatter); 64 } 65 66 List<Restaurant> filteredRestaurants = restaurantService.findRestaurantsByDateTimeAndPartySize(parsedDateTime, partySize, search); 67 68 return new ResponseEntity<List<Restaurant>>(filteredRestaurants, HttpStatus.OK); 44 69 } 45 70
Note:
See TracChangeset
for help on using the changeset viewer.