Skip to content

Commit

Permalink
Adds restaurant searching
Browse files Browse the repository at this point in the history
  • Loading branch information
gstark committed Sep 22, 2021
1 parent b3e5601 commit 6db5ed8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
21 changes: 17 additions & 4 deletions ClientApp/src/pages/Restaurants.tsx
@@ -1,15 +1,21 @@
import React from 'react'
import React, { useState } from 'react'
import tacoTuesday from '../images/taco-tuesday.svg'
import map from '../images/map.png'
import { RestaurantType } from '../types'
import { useQuery } from 'react-query'
import { SingleRestaurantFromList } from '../components/SingleRestaurantFromList'

export function Restaurants() {
const [filterText, setFilterText] = useState('')

const { data: restaurants = [] } = useQuery<RestaurantType[]>(
'restaurants',
['restaurants', filterText],
async function () {
const response = await fetch('/api/restaurants')
const response = await fetch(
filterText.length === 0
? '/api/restaurants'
: `/api/restaurants?filter=${filterText}`
)

return response.json()
}
Expand All @@ -21,7 +27,14 @@ export function Restaurants() {
<img src={tacoTuesday} alt="Taco Tuesday" />
</h1>
<form className="search">
<input type="text" placeholder="Search..." />
<input
type="text"
placeholder="Search..."
value={filterText}
onChange={function (event) {
setFilterText(event.target.value)
}}
/>
</form>

<section className="map">
Expand Down
11 changes: 9 additions & 2 deletions Controllers/RestaurantsController.cs
Expand Up @@ -31,11 +31,18 @@ public RestaurantsController(DatabaseContext context)
// Returns a list of all your Restaurants
//
[HttpGet]
public async Task<ActionResult<IEnumerable<Restaurant>>> GetRestaurants()
public async Task<ActionResult<IEnumerable<Restaurant>>> GetRestaurants(string filter)
{
// Uses the database context in `_context` to request all of the Restaurants, sort
// them by row id and return them as a JSON array.
return await _context.Restaurants.OrderBy(row => row.Id).ToListAsync();
if (filter == null)
{
return await _context.Restaurants.ToListAsync();
}
else
{
return await _context.Restaurants.Where(restaurant => restaurant.Name.ToLower().Contains(filter.ToLower())).ToListAsync();
}
}

// GET: api/Restaurants/5
Expand Down

0 comments on commit 6db5ed8

Please sign in to comment.