Skip to content

Commit

Permalink
update for search and category
Browse files Browse the repository at this point in the history
  • Loading branch information
ocitocit committed Oct 17, 2023
1 parent 4ba7f39 commit d57aed4
Showing 1 changed file with 70 additions and 9 deletions.
79 changes: 70 additions & 9 deletions actions/getListings.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,85 @@
import prisma from '@/libs/prismadb';

export interface IListingParams{
userId?:string
export interface IListingParams {
userId?: string;
guestCount?: number;
roomCount?: number;
bathroomCount?: number;
startDate?: string;
endDate?: string;
locationValue?: string;
category?: string;
}

export default async function getListings(
params:IListingParams
params: IListingParams
) {

const {userId}= params
const {
userId,
roomCount,
guestCount,
bathroomCount,
startDate,
endDate,
locationValue,
category
} = params;

let query:any={}
let query: any = {};

if (userId){
query.userId=userId
if (userId) {
query.userId = userId;
}

if (category) {
query.category = category;
}

if (roomCount) {
query.roomCount = {
gte: +roomCount
};
}

if (guestCount) {
query.guestCount = {
gte: +guestCount
};
}

if (bathroomCount) {
query.bathroomCount = {
gte: +bathroomCount
};
}

if (locationValue) {
query.locationValue = locationValue;
}

if (startDate && endDate) {
query.NOT = {
reservations: {
some: {
OR: [
{
endDate: { gte: startDate },
startDate: { lte: startDate }
},
{
startDate: { lte: endDate },
endDate: { gte: endDate }
}
]
}
}
}
}

try {
const listings = await prisma.listing.findMany({
where:query,
where: query,
orderBy: {
createdAt: 'desc'
}
Expand All @@ -32,5 +93,5 @@ export default async function getListings(
return safeListings;
} catch (error: any) {
throw new Error(error);
}
};
}

0 comments on commit d57aed4

Please sign in to comment.