Skip to content

Commit

Permalink
Refactor getFavoriteListings to use Prisma model and add type annotat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
GithmiHashara committed Sep 14, 2024
1 parent 3b734d8 commit b04d88b
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions app/actions/getFavoriteListings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
// import prisma from '@/app/libs/prismadb'

// import getCurrentUser from './getCurrentUser'

// export default async function getFavoriteListings() {
// try {
// const currentUser = await getCurrentUser()

// if (!currentUser) {
// return []
// }

// const favorites = await prisma.listing.findMany({
// where: {
// id: {
// in: [...(currentUser.favoriteIds || [])]
// }
// }
// })

// const safeFavorites = favorites.map((favorite) => ({
// ...favorite,
// createdAt: favorite.createdAt.toISOString()
// }))

// return safeFavorites
// } catch (error: any) {
// throw new Error(error)
// }
// }


//2nd one

// import prisma from '@/app/libs/prismadb'
// import getCurrentUser from './getCurrentUser'
// import { Listing } from '@prisma/client' // Assuming your Prisma model is named Listing

// export default async function getFavoriteListings() {
// try {
// const currentUser = await getCurrentUser()

// if (!currentUser) {
// return []
// }

// const favorites: Listing[] = await prisma.listing.findMany({
// where: {
// id: {
// in: [...(currentUser.favoriteIds || [])]
// }
// }
// })

// const safeFavorites = favorites.map((favorite: Listing) => ({
// ...favorite,
// createdAt: favorite.createdAt.toISOString()
// }))

// return safeFavorites
// } catch (error: any) {
// throw new Error(error)
// }
// }

// app/actions/getFavoriteListings.ts
import prisma from '@/app/libs/prismadb'
import getCurrentUser from './getCurrentUser'
import { Listing } from '@prisma/client' // Assuming your Prisma model is named Listing
import { Prisma } from '@prisma/client'

export default async function getFavoriteListings() {
try {
Expand All @@ -10,15 +76,15 @@ export default async function getFavoriteListings() {
return []
}

const favorites: Listing[] = await prisma.listing.findMany({
const favorites: Prisma.ListingGetPayload<{}>[] = await prisma.listing.findMany({
where: {
id: {
in: [...(currentUser.favoriteIds || [])]
}
}
})

const safeFavorites = favorites.map((favorite: Listing) => ({
const safeFavorites = favorites.map((favorite) => ({
...favorite,
createdAt: favorite.createdAt.toISOString()
}))
Expand All @@ -28,3 +94,4 @@ export default async function getFavoriteListings() {
throw new Error(error)
}
}

0 comments on commit b04d88b

Please sign in to comment.