Skip to content

Commit 56cd4be

Browse files
committed
Add article action functions and refactor author pages
Introduce helper functions for author-related queries in `articleActions.ts`. Refactor author pages to utilize these new helper functions, promoting code reuse and simplifying data retrieval logic. Took 10 minutes
1 parent 66fe3d7 commit 56cd4be

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

app/articles/articleActions.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { prisma } from "@/lib/prisma"
2+
3+
export async function getAuthorByUsername(username: string) {
4+
return await prisma.user.findUnique({
5+
where: { username },
6+
select: {
7+
id: true,
8+
name: true,
9+
username: true,
10+
image: true,
11+
bio: true,
12+
_count: {
13+
select: { authoredArticles: true }
14+
}
15+
}
16+
})
17+
}
18+
19+
export async function getAuthors() {
20+
return await prisma.user.findMany({
21+
where: {
22+
authoredArticles: {
23+
some: {}
24+
}
25+
},
26+
select: {
27+
id: true,
28+
name: true,
29+
username: true,
30+
image: true,
31+
bio: true,
32+
_count: {
33+
select: { authoredArticles: true }
34+
}
35+
},
36+
orderBy: {
37+
name: 'asc'
38+
}
39+
})
40+
}
41+
42+
export async function getAuthorForMetadata(username: string) {
43+
return await prisma.user.findUnique({
44+
where: { username },
45+
select: {
46+
name: true,
47+
username: true
48+
}
49+
})
50+
}

app/articles/author/[username]/page.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { prisma } from "@/lib/prisma"
21
import { notFound } from "next/navigation"
32
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
43
import ArticleSearchAndGrid from "@/components/article/ArticleSearchAndGrid"
4+
import { getAuthorByUsername, getAuthorForMetadata } from "../../articleActions"
55

66
export async function generateMetadata({ params }: { params: { username: string } }) {
7-
const author = await prisma.user.findUnique({
8-
where: { username: params.username }
9-
})
7+
const author = await getAuthorForMetadata(params.username)
108

119
if (!author) {
1210
return {
@@ -21,14 +19,7 @@ export async function generateMetadata({ params }: { params: { username: string
2119
}
2220

2321
export default async function AuthorPage({ params }: { params: { username: string } }) {
24-
const author = await prisma.user.findUnique({
25-
where: { username: params.username },
26-
include: {
27-
_count: {
28-
select: { authoredArticles: true }
29-
}
30-
}
31-
})
22+
const author = await getAuthorByUsername(params.username)
3223

3324
if (!author) {
3425
notFound()

app/articles/authors/page.tsx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
import { prisma } from "@/lib/prisma"
21
import AuthorsList from "@/components/authors/AuthorsList"
2+
import { getAuthors } from "../articleActions"
33

44
export const metadata = {
55
title: 'Article Authors',
66
description: 'Browse all article authors'
77
}
88

9-
async function getAuthors() {
10-
const authors = await prisma.user.findMany({
11-
where: {
12-
authoredArticles: {
13-
some: {}
14-
}
15-
},
16-
include: {
17-
_count: {
18-
select: { authoredArticles: true }
19-
}
20-
},
21-
orderBy: {
22-
name: 'asc'
23-
}
24-
})
25-
return authors
26-
}
27-
289
export default async function AuthorsPage() {
2910
const authors = await getAuthors()
3011

0 commit comments

Comments
 (0)