Skip to content

Commit

Permalink
upgrading search + adding title of brand in front of model in search …
Browse files Browse the repository at this point in the history
…items + fixing model editor
  • Loading branch information
filipc30 committed Feb 7, 2024
1 parent 1cca6d3 commit f666d24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
49 changes: 29 additions & 20 deletions app/routes/_base+/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,36 @@ export async function loader({ request }: DataFunctionArgs) {
}

const like = `%${searchTerm ?? ''}%`
const likeParts = searchTerm.split(' ').map(part => `%${part}%`)

// TODO: in future, extend search params to look for individual string parts - e.g. "500 speciale" and "speciale 500" should find the same item.. (TBD)
const rawSearchResults = await prisma.$queryRaw`
SELECT
m.id,
m.url,
m.title,
'CarModel' as type,
b.title as carBrandTitle
FROM CarModel m
JOIN CarBrand b ON m.carBrandId = b.id
WHERE m.title LIKE ${like} OR m.url LIKE ${like}
UNION
SELECT id, url, title, 'CarBrand' as type, NULL as carBrandTitle
FROM CarBrand
WHERE title LIKE ${like}
UNION
SELECT id, url, name as title, 'Dealer' as type, NULL as carBrandTitle
FROM Dealer
WHERE name LIKE ${like}
LIMIT 20
`
SELECT
m.id,
m.url,
m.title,
'CarModel' as type,
b.title as carBrandTitle
FROM CarModel m
JOIN CarBrand b ON m.carBrandId = b.id
WHERE (m.visibility = true)
AND (
m.title LIKE ${like}
OR m.url LIKE ${like}
OR m.carBrandId LIKE ${like}
OR m.carBrandId LIKE ${likeParts[0]} AND m.title LIKE ${likeParts[1]}
OR m.title LIKE ${likeParts[0]} AND m.carBrandId LIKE ${likeParts[1]}
)
UNION
SELECT id, url, title, 'CarBrand' as type, NULL as carBrandTitle
FROM CarBrand
WHERE (visibility = true AND title LIKE ${like})
UNION
SELECT id, url, name as title, 'Dealer' as type, NULL as carBrandTitle
FROM Dealer
WHERE name LIKE ${like}
LIMIT 20
`;

const result = SearchResultsSchema.safeParse(rawSearchResults)
if (!result.success) {
Expand Down Expand Up @@ -157,7 +166,7 @@ export default function Index() {
className="flex flex-col items-center justify-center rounded-lg bg-muted px-5 py-8 transition duration-200 hover:bg-highlight hover:text-highlight-foreground"
>
<span className="overflow-hidden text-ellipsis whitespace-nowrap text-center text-body-md">
{result.title}
{result.carBrandTitle} {result.title}
</span>
</Link>
</li>
Expand Down
3 changes: 2 additions & 1 deletion app/routes/resources+/__model-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function action({ request, params }: DataFunctionArgs) {
? carModelFacility[0].map(facilityId => ({ id: facilityId }))
: [],
},
carBrandId: carBrandId,
carBrandId,
},
update: {
url,
Expand All @@ -129,6 +129,7 @@ export async function action({ request, params }: DataFunctionArgs) {
? carModelFacility[0].map(facilityId => ({ id: facilityId }))
: [],
},
carBrandId,
},
})

Expand Down

0 comments on commit f666d24

Please sign in to comment.