-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
95 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,42 @@ | ||
"use server"; | ||
|
||
import prisma from "@/lib/db"; | ||
import { LISTINGS_PER_PAGE } from "./constants"; | ||
import { countListingsToSkip } from "./function"; | ||
import { Listing } from "./types"; | ||
import { Listing, PrismaListing } from "./types"; | ||
|
||
export async function countMatchedListings(): Promise<number> { | ||
// TODO: Add filter param values | ||
return await prisma.listing.count(); | ||
} | ||
const prismaListingMapper = (dbListing: PrismaListing) => { | ||
const dbListingPrice = dbListing.prices | ||
.filter((dbListingPrice) => dbListingPrice.isCurrent) | ||
.at(0)! | ||
.value.toNumber(); | ||
const dbListingAddress = dbListing.address!; | ||
const listing: Listing = { | ||
id: dbListing.id, | ||
imageUrls: dbListing.imageUrls, | ||
beds: dbListing.beds, | ||
baths: dbListing.baths, | ||
area: dbListing.area.toNumber(), | ||
createdDate: dbListing.createdDate, | ||
address: { | ||
addressLine: dbListingAddress.addressLine, | ||
city: dbListingAddress.city, | ||
state: dbListingAddress.state, | ||
longitude: dbListingAddress.longitude.toNumber(), | ||
latitude: dbListingAddress.latitude.toNumber(), | ||
}, | ||
price: { | ||
value: dbListingPrice, | ||
}, | ||
}; | ||
return listing; | ||
}; | ||
|
||
export async function fetchListingsByPage( | ||
currentPage: number | ||
): Promise<Listing[]> { | ||
const dbListings = await prisma.listing.findMany({ | ||
skip: countListingsToSkip(currentPage, LISTINGS_PER_PAGE), | ||
take: LISTINGS_PER_PAGE, | ||
export async function fetchMatchedListings(): Promise<Listing[]> { | ||
// TODO: Add filter param values | ||
const prismaListings = await prisma.listing.findMany({ | ||
include: { | ||
address: true, | ||
prices: true, | ||
}, | ||
}); | ||
const processedListings = dbListings.map((dbListing) => { | ||
const dbListingPrice = dbListing.prices | ||
.filter((dbListingPrice) => dbListingPrice.isCurrent) | ||
.at(0)! | ||
.value.toNumber(); | ||
const dbListingAddress = dbListing.address!; | ||
const listing: Listing = { | ||
id: dbListing.id, | ||
imageUrls: dbListing.imageUrls, | ||
beds: dbListing.beds, | ||
baths: dbListing.baths, | ||
area: dbListing.area.toNumber(), | ||
createdDate: dbListing.createdDate, | ||
address: { | ||
addressLine: dbListingAddress.addressLine, | ||
city: dbListingAddress.city, | ||
state: dbListingAddress.state, | ||
longitude: dbListingAddress.longitude.toNumber(), | ||
latitude: dbListingAddress.latitude.toNumber(), | ||
}, | ||
price: { | ||
value: dbListingPrice, | ||
}, | ||
}; | ||
return listing; | ||
}); | ||
|
||
return processedListings; | ||
return prismaListings.map(prismaListingMapper); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters