Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
bayasdev committed Jun 7, 2023
1 parent 87d8eb5 commit 62dc9d7
Show file tree
Hide file tree
Showing 79 changed files with 5,115 additions and 237 deletions.
16 changes: 16 additions & 0 deletions .env.sample
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=""
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

.env
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
37 changes: 37 additions & 0 deletions app/actions/getCurrentUser.ts
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;
}
}
30 changes: 30 additions & 0 deletions app/actions/getFavoritesListings.ts
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);
}
}
37 changes: 37 additions & 0 deletions app/actions/getListingById.ts
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);
}
}
94 changes: 94 additions & 0 deletions app/actions/getListings.ts
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);
}
}
52 changes: 52 additions & 0 deletions app/actions/getReservations.ts
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);
}
}
69 changes: 69 additions & 0 deletions app/api/favorites/[listingId]/route.ts
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);
}
3 changes: 0 additions & 3 deletions app/api/hello/route.ts

This file was deleted.

34 changes: 34 additions & 0 deletions app/api/listings/[listingId]/route.ts
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);
}
Loading

0 comments on commit 62dc9d7

Please sign in to comment.