-
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
1 parent
53124be
commit af9538f
Showing
12 changed files
with
317 additions
and
92 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,5 +1,10 @@ | ||
DATABASE_URL= | ||
GOOGLE_CLIENT_ID= | ||
GOOGLE_CLIENT_SECRET= | ||
NEXT_PUBIC_BASE_URL= | ||
SECRET= | ||
SECRET= | ||
POSTGRES_USER="postgres" | ||
POSTGRES_PASSWORD="postgres" | ||
POSTGRES_DB="testimonial_space" | ||
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB} | ||
REDIS_HOST="localhost" | ||
REDIS_PORT=6379 |
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,28 @@ | ||
version: '3.8' | ||
|
||
services: | ||
redis: | ||
image: redis:latest | ||
container_name: redis | ||
ports: | ||
- "6379:6379" | ||
volumes: | ||
- redis_data:/data | ||
|
||
postgres: | ||
image: postgres:latest | ||
container_name: postgres | ||
ports: | ||
- "5432:5432" | ||
environment: | ||
POSTGRES_USER: ${POSTGRES_USER} | ||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} | ||
POSTGRES_DB: ${POSTGRES_DB} | ||
volumes: | ||
- postgres_data:/var/lib/postgresql/data | ||
|
||
volumes: | ||
redis_data: | ||
postgres_data: | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,14 @@ | ||
import { Redis } from "ioredis"; | ||
|
||
export class RedisManager { | ||
static instance: Redis; | ||
|
||
getInstance: () => Redis = () => { | ||
if (!RedisManager.instance) RedisManager.instance = new Redis(); | ||
return RedisManager.instance; | ||
}; | ||
} | ||
|
||
export const redisManager = new RedisManager(); | ||
|
||
export const redisClient = redisManager.getInstance(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
import { prisma } from "@/prisma/client"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
import { redisClient } from "@redis/redis"; | ||
|
||
export const emailSchema = z.string().email("invalid email") | ||
export const emailSchema = z.string().email("invalid email"); | ||
|
||
export async function GET(req: NextRequest){ | ||
export async function GET(req: NextRequest) { | ||
try { | ||
// To do : Replace with user id | ||
const email = emailSchema.parse(req.nextUrl.searchParams.get("email")); | ||
|
||
const spaces = await prisma.space.findMany({ | ||
where:{ | ||
ownerEmail: email | ||
}, | ||
include: { | ||
testimonials: true, | ||
} | ||
}); | ||
|
||
return NextResponse.json({spaces, status : 200}); | ||
/** | ||
* caching spaces of a email | ||
*/ | ||
const redisKey = `spaces[email]:${email}`; | ||
const cachedData = await redisClient.get(redisKey); | ||
let spaces; | ||
|
||
if (cachedData != null) { | ||
spaces = await JSON.parse(cachedData); | ||
} else { | ||
spaces = await prisma.space.findMany({ | ||
where: { | ||
ownerEmail: email, | ||
}, | ||
include: { | ||
testimonials: true, | ||
}, | ||
}); | ||
await redisClient.setex(redisKey, 2 * 3600, JSON.stringify(spaces)); | ||
} | ||
|
||
return NextResponse.json({ spaces, status: 200 }); | ||
} catch (error) { | ||
console.log(error); | ||
return NextResponse.json({ error: "An error occurred" }, { status: 500 }); | ||
} | ||
} | ||
} |
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,23 +1,38 @@ | ||
import { prisma } from "@/prisma/client"; | ||
import { redisClient } from "@redis/redis"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(req: NextRequest){ | ||
export async function GET(req: NextRequest) { | ||
try { | ||
const spaceName:string = req.nextUrl.searchParams.get("spaceName")!; | ||
const space = await prisma.space.findFirst({ | ||
where:{ | ||
spaceName | ||
}, | ||
include:{ | ||
testimonials: true | ||
} | ||
}); | ||
if(!space) | ||
return NextResponse.json({msg:"Space does not exist", status:401}); | ||
|
||
return NextResponse.json({status: 200, msg: "success", space}); | ||
const spaceName: string = req.nextUrl.searchParams.get("spaceName")!; | ||
|
||
/** | ||
* storing the data related to a space | ||
*/ | ||
const redisKey: string = `space[name]:${spaceName}`; | ||
const cachedData = await redisClient.get(redisKey); | ||
|
||
let space; | ||
|
||
if (cachedData != null) { | ||
space = await JSON.parse(cachedData); | ||
} else { | ||
space = await prisma.space.findFirst({ | ||
where: { | ||
spaceName, | ||
}, | ||
include: { | ||
testimonials: true, | ||
}, | ||
}); | ||
await redisClient.setex(redisKey, 2 * 3600, JSON.stringify(space)); | ||
} | ||
if (!space) | ||
return NextResponse.json({ msg: "Space does not exist", status: 401 }); | ||
|
||
return NextResponse.json({ status: 200, msg: "success", space }); | ||
} catch (error) { | ||
console.log(error); | ||
return NextResponse.json({status:401, msg: "Internal server error"}); | ||
return NextResponse.json({ status: 401, msg: "Internal server error" }); | ||
} | ||
} | ||
} |
Oops, something went wrong.