2024-06-26 16:18:44 +00:00
|
|
|
"use server"
|
2024-09-19 09:37:01 +00:00
|
|
|
import { Genre, Pub, Story, Sub } from "@prisma/client"
|
2024-06-26 16:18:44 +00:00
|
|
|
import prisma from "./db"
|
|
|
|
import { revalidatePath } from "next/cache"
|
|
|
|
import { redirect } from "next/navigation"
|
2024-09-18 09:56:08 +00:00
|
|
|
import { z } from "zod"
|
|
|
|
import { storySchema } from "app/ui/forms/schemas"
|
2024-09-19 09:37:01 +00:00
|
|
|
import { pubSchema } from "app/ui/forms/schemas"
|
|
|
|
import { subSchema } from "app/ui/forms/schemas"
|
2024-09-25 10:23:16 +00:00
|
|
|
import { prepGenreData, prepStoryData } from "./validate"
|
2024-06-26 16:18:44 +00:00
|
|
|
|
2024-09-18 09:56:08 +00:00
|
|
|
//TODO - data validation, error handling, unauthorized access handling
|
|
|
|
|
2024-09-26 10:37:52 +00:00
|
|
|
export async function createStory(data: Story & { genres: number[] }): Promise<{ success: string }> {
|
2024-09-18 09:56:08 +00:00
|
|
|
// will return undefined if middleware authorization fails
|
2024-06-26 16:18:44 +00:00
|
|
|
"use server"
|
2024-09-18 09:56:08 +00:00
|
|
|
try {
|
2024-09-25 10:23:16 +00:00
|
|
|
const storyData = await prepStoryData(data)
|
|
|
|
const genresArray = await prepGenreData(data.genres)
|
2024-09-19 09:37:01 +00:00
|
|
|
|
|
|
|
//submit
|
2024-09-18 09:56:08 +00:00
|
|
|
const res = await prisma.story.create({ data: storyData })
|
|
|
|
await prisma.story.update({
|
|
|
|
where: { id: res.id },
|
|
|
|
data: {
|
|
|
|
genres: { set: genresArray }
|
|
|
|
}
|
|
|
|
})
|
2024-09-19 09:37:01 +00:00
|
|
|
revalidatePath("/story")
|
2024-09-26 10:37:52 +00:00
|
|
|
return { success: `Created the story '${data.title}'.` }
|
2024-09-18 09:56:08 +00:00
|
|
|
} catch (error) {
|
2024-09-19 09:37:01 +00:00
|
|
|
console.error(error)
|
2024-09-25 10:23:16 +00:00
|
|
|
return false
|
2024-09-18 09:56:08 +00:00
|
|
|
}
|
2024-09-25 10:23:16 +00:00
|
|
|
|
2024-06-26 16:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-26 10:37:52 +00:00
|
|
|
export async function createPub(data: Pub & { genres: number[] }): Promise<{ success: string }> {
|
2024-06-26 17:32:18 +00:00
|
|
|
"use server"
|
2024-09-19 09:37:01 +00:00
|
|
|
//prepare data
|
|
|
|
const pubData = {
|
|
|
|
title: data.title,
|
|
|
|
link: data.link,
|
|
|
|
query_after_days: data.query_after_days
|
|
|
|
}
|
2024-06-26 17:32:18 +00:00
|
|
|
const genresArray = data.genres.map(e => { return { id: e } })
|
2024-09-19 09:37:01 +00:00
|
|
|
|
|
|
|
//prepare schemas
|
|
|
|
const schema = pubSchema.omit({ genres: true })
|
|
|
|
const genreSchema = z.object({ id: z.number() })
|
|
|
|
const genresSchema = z.array(genreSchema)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
//validate
|
|
|
|
schema.parse(pubData)
|
|
|
|
genresSchema.safeParse(genresArray)
|
|
|
|
|
|
|
|
//submit
|
|
|
|
const res = await prisma.pub.create({
|
|
|
|
data: pubData
|
|
|
|
})
|
|
|
|
const genresRes = await prisma.pub.update({
|
|
|
|
where: { id: res.id },
|
|
|
|
data:
|
|
|
|
{ genres: { set: genresArray } }
|
|
|
|
})
|
|
|
|
revalidatePath("/publication")
|
2024-09-26 10:37:52 +00:00
|
|
|
return { success: `Created the publication '${data.title}'.` }
|
2024-09-19 09:37:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
2024-09-25 10:23:16 +00:00
|
|
|
return false
|
2024-09-19 09:37:01 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 17:32:18 +00:00
|
|
|
}
|
2024-06-26 16:18:44 +00:00
|
|
|
|
2024-06-26 17:55:18 +00:00
|
|
|
|
|
|
|
|
2024-09-26 10:37:52 +00:00
|
|
|
export async function createSub(data: Sub): Promise<Sub | boolean> {
|
2024-06-26 17:55:18 +00:00
|
|
|
"use server"
|
2024-09-19 09:37:01 +00:00
|
|
|
try {
|
|
|
|
subSchema.parse(data)
|
|
|
|
const res = await prisma.sub.create({ data })
|
|
|
|
revalidatePath("/submission")
|
|
|
|
return res
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
2024-09-26 10:37:52 +00:00
|
|
|
return false
|
2024-09-19 09:37:01 +00:00
|
|
|
}
|
2024-06-26 17:55:18 +00:00
|
|
|
}
|