Skip to content
Snippets Groups Projects
Commit 65ecff61 authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

profile page remake 1

parent 73ac2152
No related branches found
No related tags found
1 merge request!38Profile
Pipeline #39379 failed
export default async function Media() {
return (
<div>
<h1>Media Page WIP</h1>
</div>
)
}
\ No newline at end of file
export default async function WithReplies() {
return (
<div>
<h1>WithReplies Page WIP</h1>
</div>
)
}
\ No newline at end of file
import { db } from "@/lib/db"
import { getCurrentUser } from "@/lib/session"
import { revalidatePath } from "next/cache"
import { NextRequest, NextResponse } from "next/server"
export async function GET(req: NextRequest) {
const sessionUser = await getCurrentUser()
if (!sessionUser) {
return NextResponse.json({ status: 401, message: 'Unauthorized' })
}
let follows = undefined
try {
follows = await db.follows.findMany({
where: {
followerId: sessionUser.id
}
})
} catch (error) {
console.log("error", error)
}
return NextResponse.json({ status: 200, message: "fetched follows", follows })
}
export async function PUT(req: NextRequest) {
const sessionUser = await getCurrentUser()
const data = await req.json()
if (!sessionUser) {
return NextResponse.json({ status: 401, message: 'Unauthorized' })
}
try {
const dbUser = await db.user.findFirst({
where: {
id: sessionUser.id
}
})
const follow = await db.follows.findFirst({
where: {
followerId: sessionUser.id,
followingId: data.followingId
}
})
console.log("follow", follow)
if (follow) {
// User is already following, so unfollow
console.log("delete follow")
const delfollow = await db.follows.delete({
where: {
followerId_followingId: {
followerId: sessionUser.id,
followingId: data.followingId
}
}
})
console.log("del follow:", delfollow)
} else {
// User is not following, so follow
console.log("create follow")
await db.follows.create({
data: {
followerId: sessionUser.id,
followingId: data.followingId
},
})
}
} catch (error) {
console.log("err", error)
}
const path = req.nextUrl.searchParams.get('path') || '/'
revalidatePath(path)
return NextResponse.json({ status: 200, message: 'Follow handled' })
}
\ No newline at end of file
import { db } from "@/lib/db"
import { NextResponse } from "next/server"
import { z } from "zod"
export async function GET(request: Request, context: { params: { id: string } }) {
const { id } = context.params
const idSchema = z.string().cuid()
const zod = idSchema.safeParse(id)
if (!zod.success) {
return NextResponse.json(zod.error, { status: 400 })
}
try {
const user = await db.user.findUnique({
where: {
id,
},
select: {
id: true,
name: true,
username: true,
email: true,
image: true,
banner: true,
createdAt: true,
bio: true,
location: true,
followers: true,
following: true,
_count: {
select: {
followers: true,
following: true,
},
},
},
})
return NextResponse.json(user, { status: 200 })
} catch (error: any) {
return NextResponse.json(error.message, { status: 500 })
}
}
export async function PUT(request: Request) {
const {
userId,
name,
bio,
location,
banner,
image,
} = await request.json()
const userSchema = z
.object({
userId: z.string().cuid(),
name: z.string().min(1).max(50),
bio: z.string().max(160),
location: z.string().max(30),
banner: z.string(),
image: z.string(),
})
.strict()
const zod = userSchema.safeParse({
userId,
name,
bio,
location,
banner,
image,
})
if (!zod.success) {
return NextResponse.json(zod.error, { status: 400 })
}
try {
const user = await db.user.update({
where: {
id: userId,
},
data: {
name,
bio,
location,
banner,
image,
},
})
return NextResponse.json(user, { status: 200 })
} catch (error: any) {
return NextResponse.json(error.message, { status: 500 })
}
}
\ No newline at end of file
...@@ -4,13 +4,13 @@ import { revalidatePath } from "next/cache" ...@@ -4,13 +4,13 @@ import { revalidatePath } from "next/cache"
import { NextRequest, NextResponse } from "next/server" import { NextRequest, NextResponse } from "next/server"
export async function PUT(req: NextRequest) { export async function PUT(req: NextRequest) {
const user = await getCurrentUser() const session = await getCurrentUser()
if (!user) { if (!session) {
return NextResponse.json({ status: 401, message: 'Unauthorized' }) return NextResponse.json({ status: 401, message: 'Unauthorized' })
} }
const userId = user.id const userId = session.id
const data = await req.json() const data = await req.json()
data.gameId = parseInt(data.gameId) data.gameId = parseInt(data.gameId)
......
import { db } from "@/lib/db"
import { getCurrentUser } from "@/lib/session"
import { NextResponse } from "next/server"
import { z } from "zod"
// get following or followers information
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const userId = searchParams.get("userId") || undefined
const type = searchParams.get("type") || undefined
const userIdSchema = z.string().cuid().optional()
const zod = userIdSchema.safeParse(userId)
if (!zod.success) {
return NextResponse.json(zod.error, { status: 400 })
}
try {
if (type === "followers") {
const followers = await db.user
.findUnique({
where: {
id: userId,
},
})
.followers()
return NextResponse.json(followers, { status: 200 })
} else if (type === "following") {
const following = await db.user
.findUnique({
where: {
id: userId,
},
})
.following()
return NextResponse.json(following, { status: 200 })
}
} catch (error: any) {
return NextResponse.json(error.message, { status: 500 })
}
}
// follow a user
export async function PUT(request: Request) {
const session = await getCurrentUser()
const { userId } = await request.json()
const followerIdSchema = z
.object({
userId: z.string().cuid(),
})
.strict()
const zod = followerIdSchema.safeParse({ userId })
if (!zod.success) {
return NextResponse.json(zod.error, { status: 400 })
}
try {
await db.user.update({
where: {
id: userId,
},
data: {
followers: {
connect: {
id: session?.id,
},
},
},
})
return NextResponse.json(
{
message: "followed",
}, { status: 200 }
)
} catch (error: any) {
return NextResponse.json(error.message, { status: 500 })
}
}
// unfollow a user
export async function DELETE(request: Request) {
const session = await getCurrentUser()
const { searchParams } = new URL(request.url)
const userId = searchParams.get("userId") || undefined
const followerIdSchema = z
.object({
userId: z.string().cuid(),
})
.strict()
const zod = followerIdSchema.safeParse({ userId })
if (!zod.success) {
return NextResponse.json(zod.error, { status: 400 })
}
try {
await db.user.update({
where: {
id: userId,
},
data: {
followers: {
disconnect: {
id: session?.id,
},
},
},
})
return NextResponse.json(
{
message: "unfollowed",
}, { status: 200 },
)
} catch (error: any) {
return NextResponse.json(error.message, { status: 500 })
}
}
\ No newline at end of file
import { db } from "@/lib/db"
import { NextResponse } from "next/server"
import { z } from "zod"
// get all other users
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const id = searchParams.get("id") || undefined
const idSchema = z.string().cuid().optional()
const zod = idSchema.safeParse(id)
if (!zod.success) {
return NextResponse.json(zod.error, { status: 400 })
}
try {
const users = await db.user.findMany({
where: {
NOT: {
id,
},
},
orderBy: {
createdAt: "desc",
},
select: {
id: true,
name: true,
username: true,
email: true,
image: true,
following: true,
followers: true,
},
})
return NextResponse.json(users, { status: 200 })
} catch (error: any) {
return NextResponse.json(error.message, { status: 500 })
}
}
\ No newline at end of file
"use client" "use client"
import { Follows, Prisma } from '@prisma/client' import { Prisma } from '@prisma/client'
import { useSession } from 'next-auth/react' import { useSession } from 'next-auth/react'
import { usePathname } from "next/navigation" import { usePathname } from "next/navigation"
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
...@@ -31,12 +31,12 @@ export default function FollowButton({ user, followingId }: { user: UserWithFoll ...@@ -31,12 +31,12 @@ export default function FollowButton({ user, followingId }: { user: UserWithFoll
async function fetchFollows() { async function fetchFollows() {
try { try {
const response = await fetch('/api/followers') const response = await fetch('/api/users/follow')
const data = await response.json() const data = await response.json()
setFollows(data.follows) setFollows(data.follows)
setIsFollowing(false) setIsFollowing(false)
data.follows?.forEach((f: Follows) => { data.follows?.forEach((f: any) => {
if (f.followerId == user.id && f.followingId == followingId) { if (f.followerId == user.id && f.followingId == followingId) {
setIsFollowing(true) setIsFollowing(true)
...@@ -56,7 +56,7 @@ export default function FollowButton({ user, followingId }: { user: UserWithFoll ...@@ -56,7 +56,7 @@ export default function FollowButton({ user, followingId }: { user: UserWithFoll
async function handleFollow(e: any) { async function handleFollow(e: any) {
e.preventDefault() e.preventDefault()
const response = await fetch('/api/followers', { const response = await fetch('/api/users/follow', {
method: 'PUT', method: 'PUT',
body: JSON.stringify({ user, followingId }) body: JSON.stringify({ user, followingId })
}) })
......
...@@ -81,24 +81,12 @@ model User { ...@@ -81,24 +81,12 @@ model User {
regweets Regweet[] regweets Regweet[]
likes Like[] likes Like[]
following Follows[] @relation("following") following User[] @relation("followers")
followers Follows[] @relation("follower") followers User[] @relation("followers")
@@map("users") @@map("users")
} }
model Follows {
followerId String
followingId String
createdAt DateTime @default(now()) @map("created_at")
follower User @relation("following", fields: [followerId], references: [id])
following User @relation("follower", fields: [followingId], references: [id])
@@id([followerId, followingId])
@@map("follows")
}
model Gweet { model Gweet {
id String @id @default(cuid()) id String @id @default(cuid())
authorId String @map("user_id") authorId String @map("user_id")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment