Skip to content

Commit b6c67d2

Browse files
authored
Merge pull request #135 from CodeForStartup/feat/prisma-seed-database
feat: seed data user
2 parents 0b5eaa7 + eea1935 commit b6c67d2

File tree

12 files changed

+89
-16
lines changed

12 files changed

+89
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ turbo dev
3939
🐧 next-auth
4040
🤗 Prisma ORM
4141
🎭 Postgres
42-
🚀 Turborepo
42+
🚀 Turborepo
4343
🎯 TailwindCSS
4444
🔥 shadcn
4545
💌 next-themes
4646
🖋 Zod validation
4747
🎰 React Form Hook
4848
️🥇 Tsup
4949
💒 EditorJs
50-
🐢 react-toastify
50+
🐢 react-toastify
5151
🍾 react-textarea-autosize
5252
🐠 lucide-react icon
5353
🐴 dayjs

apps/web/@/molecules/user/posts/post-item.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import Link from "next/link"
44

5-
import { PostStatus } from "database"
5+
import { deletePost, PostStatus, TPostItem } from "database"
66
import dayjs from "dayjs"
7+
import { useSession } from "next-auth/react"
78
import { useTranslations } from "next-intl"
89
import {
910
Badge,
@@ -17,14 +18,13 @@ import {
1718
} from "ui"
1819

1920
import { togglePostStatus } from "@/actions/manage-post"
20-
import { deletePost } from "@/actions/protect/posts"
21-
import { TPostItem } from "@/types/posts"
2221

2322
export default function PostItem(post: TPostItem) {
2423
const t = useTranslations()
24+
const { data: session } = useSession()
2525

2626
const onDeletePost = async () => {
27-
await deletePost(post.id)
27+
await deletePost(post.id, session?.user?.id)
2828
}
2929

3030
const onTogglePostStatus = async () => {

packages/database/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"db:generate": "prisma generate",
1717
"db:push": "prisma db push --skip-generate",
1818
"db:migrate": "prisma migrate dev",
19-
"db:reset": "prisma migrate reset"
19+
"db:reset": "prisma migrate reset",
20+
"db:seed": "ts-node prisma/seeds/seed.mjs"
2021
},
2122
"main": "./src/index.ts",
2223
"types": "./src/index.ts"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `image` on the `Tags` table. All the data in the column will be lost.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "Tags" DROP COLUMN "image";
9+
10+
-- AlterTable
11+
ALTER TABLE "User" ADD COLUMN "password" TEXT;

packages/database/prisma/schema.prisma

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ model Post {
8282
comments Comment[]
8383
parentOf ParentPost[] @relation("parentPost")
8484
parent ParentPost[] @relation("post")
85-
Image Image? @relation(fields: [imageId], references: [id])
85+
image Image? @relation(fields: [imageId], references: [id])
8686
imageId String?
8787
}
8888

@@ -150,6 +150,7 @@ model User {
150150
email String? @unique
151151
emailVerified DateTime?
152152
image String?
153+
password String?
153154
154155
userType UserType @default(AUTHOR)
155156
@@ -179,7 +180,7 @@ model User {
179180
followings Follower[] @relation("following")
180181
comment Comment[]
181182
commentOnUser CommentOnUser[]
182-
Image Image[]
183+
images Image[]
183184
}
184185

185186
model Follower {
@@ -213,8 +214,7 @@ model Tags {
213214
createdAt DateTime @default(now())
214215
updatedAt DateTime @updatedAt
215216
216-
type TagType @default(TAG)
217-
image String?
217+
type TagType @default(TAG)
218218
219219
totalPost Int @default(0)
220220
totalView Int @default(0)
@@ -228,7 +228,7 @@ model Tags {
228228
count Int @default(0)
229229
230230
tagOnPost TagOnPost[]
231-
Image Image? @relation(fields: [imageId], references: [id])
231+
image Image? @relation(fields: [imageId], references: [id])
232232
imageId String?
233233
}
234234

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { PrismaClient } from "@prisma/client"
2+
3+
const prisma = new PrismaClient()
4+
5+
async function main() {
6+
const user = await prisma.user.upsert({
7+
where: {
8+
email: "admin@codeforstartup.com",
9+
},
10+
create: {
11+
name: "Luan Nguyen",
12+
email: "admin@codeforstartup.com",
13+
password: "12345678",
14+
},
15+
update: {},
16+
})
17+
18+
console.log("seed-user:", { user })
19+
}
20+
21+
main().then(() => {
22+
console.log("User created successfully")
23+
})

packages/database/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { createImage, deleteImage, getImage, getImages, updateImage } from "./images/queries"
22
import { TImage } from "./images/selects"
33
import { IImageFilter, IListImageResponse, ImageOrderBys } from "./images/type"
4-
import { createPost, getPost, getPosts, updatePost, updatePostStatus } from "./posts/queries"
4+
import {
5+
createPost,
6+
deletePost,
7+
getPost,
8+
getPosts,
9+
updatePost,
10+
updatePostStatus,
11+
} from "./posts/queries"
512
import { TCreatePostInput, TPostActionType, TPostItem } from "./posts/selects"
613
import { TGetPostsRequest, TGetPostsResponse } from "./posts/type"
714
import prisma from "./prisma"
@@ -31,6 +38,7 @@ export {
3138
getPosts,
3239
createPost,
3340
updatePost,
41+
deletePost,
3442
updatePostStatus,
3543
FilterValues,
3644
PeriodValues,

packages/database/src/posts/queries.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,24 @@ export const updatePostStatus = async (
306306
}
307307
}
308308
}
309+
310+
export const deletePost = async (id: string, userId: string): Promise<IActionReturn<TPostItem>> => {
311+
try {
312+
const post = await prisma.post.delete({
313+
where: {
314+
id,
315+
authorId: userId,
316+
},
317+
select: postSelect,
318+
})
319+
320+
return {
321+
data: post,
322+
}
323+
} catch (error) {
324+
throw {
325+
data: null,
326+
error: error,
327+
}
328+
}
329+
}

packages/database/src/posts/selects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const postSelect = {
1010
postStatus: true,
1111
totalLike: true,
1212
totalFollow: true,
13-
Image: {
13+
image: {
1414
select: {
1515
id: true,
1616
url: true,

packages/database/src/tags/selects.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ export const tagListSelect = {
55
name: true,
66
slug: true,
77
description: true,
8-
image: true,
8+
image: {
9+
select: {
10+
id: true,
11+
url: true,
12+
},
13+
},
914
type: true,
1015
totalFollowers: true,
1116
updatedAt: true,

0 commit comments

Comments
 (0)