-
Notifications
You must be signed in to change notification settings - Fork 0
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
e566831
commit 6563890
Showing
22 changed files
with
271 additions
and
186 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,79 @@ | ||
import { BusinessCode, responseHandler } from "@/lib/fetch_utils"; | ||
import prisma from "@/lib/pg"; | ||
import { PostTypes } from "@/types/post"; | ||
import { getSession } from "next-auth/react"; | ||
|
||
export const dynamic = "force-dynamic"; // defaults to auto | ||
export async function GET(request: Request) { | ||
const key = new URL(request.url).searchParams.get("key"); | ||
if (!key) { | ||
return responseHandler( | ||
null, | ||
BusinessCode.normal, | ||
BusinessCode.abnormal, | ||
"无key" | ||
); | ||
} else { | ||
try { | ||
const res = await prisma.post.findUnique({ | ||
where: { blog_key: key }, | ||
}); | ||
|
||
if (!res) { | ||
return responseHandler( | ||
null, | ||
BusinessCode.normal, | ||
BusinessCode.abnormal, | ||
"key值错误" | ||
); | ||
} else { | ||
return responseHandler(res); | ||
} | ||
} catch (e) { | ||
return responseHandler( | ||
null, | ||
BusinessCode.normal, | ||
BusinessCode.abnormal, | ||
"未知异常" | ||
); | ||
} | ||
} | ||
} | ||
|
||
export async function POST(request: Request) { | ||
const body: PostTypes.PostDetail = await request.json(); | ||
const session = await getSession(); | ||
const post = await prisma.post.findUnique({ | ||
where: { | ||
blog_key: body.blog_key, | ||
}, | ||
}); | ||
|
||
if (post && session?.user.email) { | ||
const user = await prisma.user.findUnique({ | ||
where: { | ||
email: session.user.email, | ||
}, | ||
}); | ||
const likes_count = post.likes_count; | ||
|
||
if (user) { | ||
const idx = likes_count.findIndex((item) => item === user.id); | ||
if (idx > -1) { | ||
likes_count.splice(idx, 1); | ||
} else { | ||
likes_count.push(user.id); | ||
} | ||
const updatePost = await prisma.post.update({ | ||
where: { | ||
blog_key: body.blog_key, | ||
}, | ||
data: { | ||
likes_count, | ||
}, | ||
}); | ||
|
||
return responseHandler(updatePost); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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,55 @@ | ||
"use client"; | ||
|
||
import { get, post } from "@/lib/fetch"; | ||
import { EyeOutlined, LikeOutlined } from "@ant-design/icons"; | ||
import { PostTypes } from "@/types/post"; | ||
import { useSession } from "next-auth/react"; | ||
import LoginModal from "../login/page"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function LikeAndCollect(data: { blogKey: string }) { | ||
const [detail, setDetail] = useState<PostTypes.PostDetail>(); | ||
|
||
const getData = async () => { | ||
const res = await get<PostTypes.PostDetail>("blog/detail", { | ||
key: data.blogKey, | ||
}); | ||
|
||
setDetail(res); | ||
}; | ||
|
||
useEffect(() => { | ||
getData(); | ||
}, [data.blogKey]); | ||
const session = useSession(); | ||
|
||
const like = async () => { | ||
if (session.status === "authenticated") { | ||
const res = await post<PostTypes.PostDetail>("blog/detail", { | ||
blog_key: detail?.blog_key, | ||
}); | ||
setDetail(res); | ||
} else { | ||
setVisible(true); | ||
} | ||
}; | ||
|
||
const [visible, setVisible] = useState(false); | ||
return ( | ||
detail && ( | ||
<div className="py-4 flex items-center space-x-4 text-md text-[--text-color] opacity-70"> | ||
<div> | ||
<EyeOutlined /> {detail.views_count} | ||
</div> | ||
|
||
<div onClick={() => like()} className="text-primary-100 cursor-pointer"> | ||
<LikeOutlined /> {detail.likes_count.length} | ||
</div> | ||
<LoginModal | ||
open={visible} | ||
onClose={() => setVisible(false)} | ||
></LoginModal> | ||
</div> | ||
) | ||
); | ||
} |
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 { useState, useEffect } from "react"; | ||
import LoginModal from "../login/page"; | ||
|
||
export default function useLoginModal() { | ||
const [visible, setVisible] = useState(true); | ||
useEffect(() => { | ||
console.log(visible, "visbile"); | ||
visible && ( | ||
<LoginModal open={visible} onClose={() => setVisible(false)}></LoginModal> | ||
); | ||
}, [visible]); | ||
|
||
return [setVisible]; | ||
} |
Oops, something went wrong.