Changeset 9293100


Ignore:
Timestamp:
03/03/24 13:59:59 (15 months ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
e35d0e9
Parents:
c63036a
Message:

RetaurantServiceImpl problemi
isAvailable od tableEntity...

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • my-react-app/src/App.js

    rc63036a r9293100  
    1313import axios from "axios";
    1414import { CuisineContext } from './components/CuisineContext';
     15import RestaurantInfo from "./components/RestaurantInfo";
    1516
    1617const App = () => {
     
    4243    const [date, setDate] = useState(todayDate);
    4344    const [selectedTime, setSelectedTime] = useState('');
     45    const [formatedDateTime, setFormatedDateTime] = useState('')
    4446    const [numPeople, setNumPeople] = useState(2);
    4547    const [searchValue, setSearchValue] = useState('');
    4648    const [timeSlots, setTimeSlots] = useState([]);
    4749
     50    let [filteredRestaurants, setFilteredRestaurants] = useState([]);
    4851    const cuisineTypes = useContext(CuisineContext);
     52    const [showCuisineSearch, setShowCuisineSearch] = useState(true);
    4953
    5054    useEffect(() => {
     
    112116                const dateTime = new Date(Date.UTC(year, month - 1, day, selectedHours, selectedMinutes));
    113117                formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' ');
     118                setFormatedDateTime(formattedDateTime);
    114119            }
    115120        } else {
     
    139144            const response = await axios.post('http://localhost:8080/api/search', data);
    140145            const filteredRestaurants = response.data;
     146            setFilteredRestaurants(filteredRestaurants);
    141147            console.log(filteredRestaurants);
     148            setShowCuisineSearch(false);
    142149            // Handle response accordingly
    143150        } catch (error) {
     
    150157        try {
    151158            const response = await axios.post(`http://localhost:8080/api/search/shortcut/${cuisineName}`, cuisineName);
    152             // Handle the response as needed
    153             console.log(response.data); // Log the response data, for example
     159            console.log(response.data);
     160            setFilteredRestaurants(response.data)
    154161        } catch (error) {
    155162            console.error('Error searching by cuisine:', error);
    156             // Handle errors, such as displaying an error message to the user
    157         }
    158     };
     163        }
     164        setShowCuisineSearch(false);
     165    };
     166
     167    const renderTimeSlots = (tablesList, restaurant) => {
     168        const [year, month, day] = date.split("-");
     169        const [hours, minutes] = selectedTime.split(":");
     170        const dateTime = new Date(year, month - 1, day, hours, minutes); // month is zero-based
     171
     172        let timestamp = dateTime.getTime();
     173        if (isNaN(timestamp)) {
     174            timestamp = Date.now();
     175        }
     176
     177        let renderedTimeSlots = {}; // Object to store rendered slots for each table capacity
     178
     179        return tablesList.flatMap(table => {
     180            // Render capacity header when encountering a new capacity
     181            if (!renderedTimeSlots[table.capacity]) {
     182                renderedTimeSlots[table.capacity] = 0;
     183                return (
     184                    <div key={table.capacity}>
     185                        <h3>Table for: {table.capacity}</h3>
     186                        {table.timeSlots.map((timeSlot, index) => {
     187                            const timeSlotTime = new Date(timeSlot).getTime();
     188                            const tableCapacity = table.capacity;
     189                            // Check if the time slot is after the current time, numPeople is less than or equal to tableCapacity, and limit to 5 slots
     190                            if (timeSlotTime > timestamp && numPeople <= tableCapacity && renderedTimeSlots[tableCapacity] < 5) {
     191                                renderedTimeSlots[tableCapacity]++;
     192                                const timeSlotDateTime = new Date(timeSlot);
     193                                const formattedDateTime = timeSlotDateTime.toLocaleString(); // Format for both date and time
     194
     195                                return (
     196                                    <button key={index} className="btn btn-primary me-2 mb-2" onClick={() => handleTimeSlotClick(table, timeSlot, restaurant)}>
     197                                        {formattedDateTime} {/* Display both date and time */}
     198                                    </button>
     199                                );
     200                            } else {
     201                                return null; // Render nothing if the condition is not met
     202                            }
     203                        })}
     204                    </div>
     205                );
     206            } else {
     207                // If capacity has been rendered, return null to avoid duplicate rendering
     208                return null;
     209            }
     210        });
     211    }
     212
    159213
    160214
     
    204258                    <button className="btn btn-outline-success" type="submit">Search</button>
    205259                </div>
    206                 <form>
     260
     261                <div>
     262                    {filteredRestaurants.map((restaurant) => (
     263                        <div key={restaurant.id} className="card mb-3">
     264                            <div className="card-body">
     265                                <RestaurantInfo key={restaurant.id} restaurant={restaurant}/>
     266                                <p>Available time slots</p>
     267                                <div className="d-flex flex-wrap">
     268                                    {renderTimeSlots(restaurant.tablesList.flatMap((table) => table), restaurant)}
     269                                </div>
     270                            </div>
     271                        </div>
     272                    ))}
     273                </div>
     274
     275
     276                {showCuisineSearch && (
    207277                    <div className="mb-3">
    208278                        <h2 className="display-2">Search by cuisine type</h2>
    209                         <ul className="list-group">
    210                             {cuisineTypes.map((cuisine, index) => (
    211                                 <li key={index} className="list-group-item">
    212                                     <button type="button" className="btn btn-outline-primary"
    213                                             onClick={() => handleSearchByCuisine(cuisine)}>
    214                                         {cuisine}
    215                                     </button>
    216                                 </li>
    217                             ))}
    218                         </ul>
    219                     </div>
    220                 </form>
    221 
    222 
     279                    <ul className="list-group">
     280                        {cuisineTypes.map((cuisine, index) => (
     281                            <li key={index} className="list-group-item">
     282                                <button type="button" className="btn btn-outline-primary"
     283                                        onClick={() => handleSearchByCuisine(cuisine)}>
     284                                    {cuisine}
     285                                </button>
     286                            </li>
     287                        ))}
     288                    </ul>
     289                </div>)}
    223290            </form>
    224291        </div>
  • src/main/java/com/example/rezevirajmasa/demo/repository/RestaurantRepository.java

    rc63036a r9293100  
    55import org.springframework.data.jpa.repository.JpaRepository;
    66import org.springframework.data.jpa.repository.Query;
     7import org.springframework.data.repository.query.Param;
    78
    89import java.util.List;
    910
    1011public interface RestaurantRepository extends JpaRepository<Restaurant, Long> {
    11     List<Restaurant> findAllByNameLike(String search);
     12    List<Restaurant> findAllByNameLikeIgnoreCase(String search);
    1213    List<Restaurant> findAllByCuisineTypeContaining(String search);
    1314    @Query("SELECT DISTINCT r.cuisineType FROM Restaurant r")
  • src/main/java/com/example/rezevirajmasa/demo/service/impl/RestaurantServiceImpl.java

    rc63036a r9293100  
    175175                    .collect(Collectors.toList());
    176176        } else {
    177             List<Restaurant> restaurantList = restaurantRepository.findAllByNameLike(search);
     177            List<Restaurant> restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(search);
    178178            if (restaurantList.isEmpty()) {
    179179                restaurantList = restaurantRepository.findAllByCuisineTypeContaining(search);
Note: See TracChangeset for help on using the changeset viewer.