-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
79 changed files
with
5,115 additions
and
237 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Environment variables declared in this file are automatically made available to Prisma. | ||
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema | ||
|
||
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. | ||
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings | ||
|
||
DATABASE_URL="" | ||
NEXTAUTH_SECRET="" | ||
|
||
GITHUB_ID="" | ||
GITHUB_SECRET="" | ||
|
||
GOOGLE_CLIENT_ID="" | ||
GOOGLE_CLIENT_SECRET="" | ||
|
||
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME="" |
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 |
---|---|---|
|
@@ -33,3 +33,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
.env |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"typescript.enablePromptUseWorkspaceTsdk": true | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getServerSession } from 'next-auth/next'; | ||
|
||
import { authOptions } from '@/pages/api/auth/[...nextauth]'; | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
export async function getSession() { | ||
return await getServerSession(authOptions); | ||
} | ||
|
||
export default async function getCurrentUser() { | ||
try { | ||
const session = await getSession(); | ||
|
||
if (!session?.user?.email) { | ||
return null; | ||
} | ||
|
||
const currentUser = await prisma.user.findUnique({ | ||
where: { | ||
email: session.user.email as string, | ||
}, | ||
}); | ||
|
||
if (!currentUser) { | ||
return null; | ||
} | ||
|
||
return { | ||
...currentUser, | ||
createdAt: currentUser.createdAt.toISOString(), | ||
updatedAt: currentUser.updatedAt.toISOString(), | ||
emailVerified: currentUser.emailVerified?.toISOString() || null, | ||
}; | ||
} catch (error: any) { | ||
return null; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
import getCurrentUser from './getCurrentUser'; | ||
|
||
export default async function getFavoritesListings() { | ||
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); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
interface IParams { | ||
listingId?: string; | ||
} | ||
|
||
export default async function getListingById(params: IParams) { | ||
try { | ||
const { listingId } = params; | ||
|
||
const listing = await prisma.listing.findUnique({ | ||
where: { | ||
id: listingId, | ||
}, | ||
include: { | ||
user: true, | ||
}, | ||
}); | ||
|
||
if (!listing) { | ||
return null; | ||
} | ||
|
||
return { | ||
...listing, | ||
createdAt: listing.createdAt.toString(), | ||
user: { | ||
...listing.user, | ||
createdAt: listing.user.createdAt.toString(), | ||
updatedAt: listing.user.updatedAt.toString(), | ||
emailVerified: listing.user.emailVerified?.toString() || null, | ||
}, | ||
}; | ||
} catch (error: any) { | ||
throw new Error(error); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
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) { | ||
try { | ||
const { | ||
userId, | ||
guestCount, | ||
roomCount, | ||
bathroomCount, | ||
startDate, | ||
endDate, | ||
locationValue, | ||
category, | ||
} = params; | ||
|
||
let query: any = {}; | ||
|
||
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 }, | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
const listings = await prisma.listing.findMany({ | ||
where: query, | ||
orderBy: { | ||
createdAt: 'desc', | ||
}, | ||
}); | ||
|
||
const safeListings = listings.map((listing) => ({ | ||
...listing, | ||
createdAt: listing.createdAt.toISOString(), | ||
})); | ||
|
||
return safeListings; | ||
} catch (error: any) { | ||
throw new Error(error); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
interface IParams { | ||
listingId?: string; | ||
userId?: string; | ||
authorId?: string; | ||
} | ||
|
||
export default async function getReservations(params: IParams) { | ||
try { | ||
const { listingId, userId, authorId } = params; | ||
|
||
const query: any = {}; | ||
|
||
if (listingId) { | ||
query.listingId = listingId; | ||
} | ||
|
||
if (userId) { | ||
query.userId = userId; | ||
} | ||
|
||
if (authorId) { | ||
query.listing = { userId: authorId }; | ||
} | ||
|
||
const reservations = await prisma.reservation.findMany({ | ||
where: query, | ||
include: { | ||
listing: true, | ||
}, | ||
orderBy: { | ||
createdAt: 'desc', | ||
}, | ||
}); | ||
|
||
const safeReservations = reservations.map((reservation) => ({ | ||
...reservation, | ||
createdAt: reservation.createdAt.toISOString(), | ||
startDate: reservation.startDate.toISOString(), | ||
endDate: reservation.endDate.toISOString(), | ||
listing: { | ||
...reservation.listing, | ||
createdAt: reservation.listing.createdAt.toISOString(), | ||
}, | ||
})); | ||
|
||
return safeReservations; | ||
} catch (error: any) { | ||
throw new Error(error); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import getCurrentUser from '@/app/actions/getCurrentUser'; | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
interface IParams { | ||
listingId?: string; | ||
} | ||
|
||
export async function POST(request: Request, { params }: { params: IParams }) { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
const { listingId } = params; | ||
|
||
if (!listingId || typeof listingId !== 'string') { | ||
throw new Error('Invalid Id'); | ||
} | ||
|
||
let favoriteIds = [...(currentUser.favoriteIds || [])]; | ||
|
||
favoriteIds.push(listingId); | ||
|
||
const user = await prisma.user.update({ | ||
where: { | ||
id: currentUser.id, | ||
}, | ||
data: { | ||
favoriteIds, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(user); | ||
} | ||
|
||
export async function DELETE( | ||
request: Request, | ||
{ params }: { params: IParams } | ||
) { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
const { listingId } = params; | ||
|
||
if (!listingId || typeof listingId !== 'string') { | ||
throw new Error('Invalid Id'); | ||
} | ||
|
||
let favoriteIds = [...(currentUser.favoriteIds || [])]; | ||
|
||
favoriteIds = favoriteIds.filter((id) => id !== listingId); | ||
|
||
const user = await prisma.user.update({ | ||
where: { | ||
id: currentUser.id, | ||
}, | ||
data: { | ||
favoriteIds, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(user); | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import getCurrentUser from '@/app/actions/getCurrentUser'; | ||
import prisma from '@/app/libs/prismadb'; | ||
|
||
interface IParams { | ||
listingId?: string; | ||
} | ||
|
||
export async function DELETE( | ||
request: Request, | ||
{ params }: { params: IParams } | ||
) { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
const { listingId } = params; | ||
|
||
if (!listingId || typeof listingId !== 'string') { | ||
throw new Error('Invalid Id'); | ||
} | ||
|
||
const listing = await prisma.listing.deleteMany({ | ||
where: { | ||
id: listingId, | ||
userId: currentUser.id, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(listing); | ||
} |
Oops, something went wrong.