-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathchangelog.ts
132 lines (118 loc) · 3.78 KB
/
changelog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use server';
import {GET as handler} from 'app/changelog/api/auth/[...nextauth]/route';
import {revalidatePath} from 'next/cache';
import {redirect} from 'next/navigation';
import {getServerSession} from 'next-auth/next';
import {prisma} from '../prisma';
export async function unpublishChangelog(formData: FormData) {
const session = await getServerSession(handler);
if (!session) {
return {message: 'Unauthorized'};
}
const id = formData.get('id') as string;
try {
await prisma.changelog.update({
where: {id},
data: {published: false, publishedAt: null},
});
} catch (error) {
console.error('DELETE ACTION ERROR:', error);
return {message: 'Unable to unpublish changelog'};
}
return revalidatePath(`/changelog/_admin`);
}
export async function publishChangelog(formData: FormData) {
const session = await getServerSession(handler);
if (!session) {
return {message: 'Unauthorized'};
}
const id = formData.get('id') as string;
try {
await prisma.changelog.update({
where: {id},
data: {published: true, publishedAt: new Date().toISOString()},
});
} catch (error) {
console.error('DELETE ACTION ERROR:', error);
return {message: 'Unable to publish changelog'};
}
return revalidatePath(`/changelog/_admin`);
}
export async function createChangelog(formData: FormData) {
const session = await getServerSession(handler);
if (!session) {
return {message: 'Unauthorized'};
}
const categories = formData.getAll('categories');
await prisma.category.createMany({
data: categories.map(category => ({name: category as string})),
skipDuplicates: true,
});
const connect = categories.map(category => {
return {name: category as string};
});
const user = await prisma.user.findUnique({
// @ts-ignore
where: {email: session?.user?.email as string},
});
const data = {
title: formData.get('title') as string,
content: formData.get('content') as string,
summary: formData.get('summary') as string,
image: formData.get('image') as string,
slug: formData.get('slug') as string,
author: {connect: {id: user?.id as string}},
categories: formData.get('categories') !== '' ? {connect} : {},
};
await prisma.changelog.create({data});
return redirect(`/changelog/_admin`);
}
export async function editChangelog(formData: FormData) {
const session = await getServerSession(handler);
if (!session) {
return {message: 'Unauthorized'};
}
const id = formData.get('id') as string;
const categories = formData.getAll('categories');
await prisma.category.createMany({
data: categories.map(category => ({name: category as string})),
skipDuplicates: true,
});
const connect = categories.map(category => {
return {name: category as string};
});
try {
const data = {
title: formData.get('title') as string,
content: formData.get('content') as string,
summary: formData.get('summary') as string,
image: formData.get('image') as string,
slug: formData.get('slug') as string,
categories: formData.get('categories') !== '' ? {set: [...connect]} : {set: []},
};
await prisma.changelog.update({
where: {id},
data,
});
} catch (error) {
console.error('EDIT ACTION ERROR:', error);
return {message: error};
}
return redirect(`/changelog/_admin`);
}
export async function deleteChangelog(formData: FormData) {
const session = await getServerSession(handler);
if (!session) {
return {message: 'Unauthorized'};
}
const id = formData.get('id') as string;
try {
await prisma.changelog.delete({
where: {id},
});
} catch (error) {
console.error('DELETE ACTION ERROR:', error);
return {message: 'Unable to delete changelog'};
}
return revalidatePath(`/changelog/_admin`);
}