generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from ridabensalem/fitchprofile
Fitchprofile
- Loading branch information
Showing
12 changed files
with
21,672 additions
and
7,327 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
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,43 @@ | ||
import { NextResponse } from 'next/server'; | ||
import prisma from "@/app/libs/prismadb"; | ||
import getCurrentUser from "@/app/actions/getCurrentUser"; | ||
|
||
// Handler for GET request to retrieve preferences | ||
export async function GET() { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
// Retrieve user preferences from the database | ||
const user = await prisma.user.findUnique({ | ||
where: { id: currentUser.id }, | ||
select: { | ||
currency: true, | ||
language: true, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(user); | ||
} | ||
|
||
// Handler for POST request to save preferences | ||
export async function POST(request: Request) { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
const body = await request.json(); | ||
const { currency, language } = body; | ||
|
||
// Save preferences to the database | ||
await prisma.user.update({ | ||
where: { id: currentUser.id }, | ||
data: { currency, language }, | ||
}); | ||
|
||
return NextResponse.json({ message: 'preferences saved successfully' }); | ||
} |
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 { NextResponse } from "next/server"; | ||
import prisma from "@/app/libs/prismadb"; | ||
import getCurrentUser from "@/app/actions/getCurrentUser"; | ||
|
||
// Handler for the PUT request | ||
export async function PUT(request: Request) { | ||
try { | ||
// Get the current user | ||
const currentUser = await getCurrentUser(); | ||
|
||
// If there is no current user, return an error response | ||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
// Parse the request body as JSON | ||
const body = await request.json(); | ||
|
||
// Destructure the image URL from the request body | ||
const { image } = body; | ||
|
||
// Update the user's profile image in the database | ||
const user = await prisma.user.update({ | ||
where: { id: currentUser.id }, | ||
data: { image }, | ||
}); | ||
|
||
// Return the updated user profile as a JSON response | ||
return NextResponse.json(user); | ||
} catch (error) { | ||
console.error('Error updating profile image:', error); | ||
return NextResponse.error(); | ||
} | ||
} | ||
|
||
|
||
// Handler for the GET request | ||
export async function GET(request: Request) { | ||
try { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
const userProfile = await prisma.user.findUnique({ | ||
where: { id: currentUser.id }, | ||
select: { | ||
username: true, | ||
name: true, | ||
surname: true, | ||
email: true, | ||
telephone: true, | ||
dateOfBirth: true, | ||
gender: true, | ||
createdAt: true, | ||
image: true, | ||
}, | ||
}); | ||
|
||
if (!userProfile) { | ||
return NextResponse.error(); | ||
} | ||
|
||
return NextResponse.json(userProfile); | ||
} catch (e) { | ||
console.error(e); | ||
return NextResponse.error(); | ||
} | ||
} | ||
// Handler for the DELETE request | ||
export async function DELETE(request: Request) { | ||
try { | ||
// Get the current user | ||
const currentUser = await getCurrentUser(); | ||
|
||
// If there is no current user, return an error response | ||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
// Update the user's profile image to null in the database | ||
const user = await prisma.user.update({ | ||
where: { id: currentUser.id }, | ||
data: { image: null }, | ||
}); | ||
|
||
// Return the updated user profile as a JSON response | ||
return NextResponse.json(user); | ||
} catch (error) { | ||
console.error('Error deleting profile image:', error); | ||
return NextResponse.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 prisma from "@/app/libs/prismadb"; | ||
import bcrypt from "bcrypt"; | ||
import getCurrentUser from "@/app/actions/getCurrentUser"; | ||
|
||
|
||
export async function PUT(request: Request) { | ||
try { | ||
const body = await request.json(); | ||
|
||
const { password , recoveryEmail } = body; | ||
|
||
// Get the current user | ||
const currentUser = await getCurrentUser(); | ||
|
||
// If no user is found, return an error response | ||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
// Prepare the update data object | ||
const updateData: any = {}; | ||
|
||
// If password is provided, hash it and add to update data | ||
if (password) { | ||
updateData.hashedPassword = await bcrypt.hash(password, 12); | ||
} | ||
// If recoveryEmail is provided, add to update data | ||
if (recoveryEmail) { | ||
updateData.recoveryEmail = recoveryEmail; | ||
} | ||
|
||
const user = await prisma.user.update({ | ||
where: { | ||
id: currentUser.id | ||
}, | ||
data: updateData, | ||
}); | ||
|
||
return NextResponse.json(user); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json({ error: "internal Server Error" }, { status: 500 }); | ||
} | ||
} | ||
//Delete user account endpoint | ||
export async function DELETE(request: Request) { | ||
try { | ||
// Get the current user | ||
const currentUser = await getCurrentUser(); | ||
|
||
// If no user is found, return an error response | ||
if (!currentUser) { | ||
return NextResponse.error(); | ||
} | ||
|
||
// Delete the user from the database | ||
await prisma.user.delete({ | ||
where: { | ||
id: currentUser.id | ||
} | ||
}); | ||
|
||
// Return a success response | ||
return NextResponse.json({ message: "user account deleted successfully" }); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json({ error: "Internal Server Error" }, { 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
Oops, something went wrong.