committing summative
This commit is contained in:
1 parent
73ac8e85be
commit
95b138f29e
44 files changed
+5319
-755
No files matched your search
@@ -0,0 +1,19 @@
|
||||
"use server";
|
||||
|
||||
import { auth, signIn, signOut } from "@/auth";
|
||||
|
||||
export async function handleGoogleLogin() {
|
||||
await signIn("google", {redirectTo: "/"})
|
||||
|
||||
}
|
||||
|
||||
export async function checkGoogleSession() {
|
||||
const session = await auth();
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function handleGoogleLogout() {
|
||||
await signOut({redirectTo: "/login"})
|
||||
}
|
||||
|
||||
// creates our functions to check session, login, logout using functions we exported from auth's nextauth functions
|
||||
@@ -0,0 +1,276 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "../lib/prisma";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { auth } from "@/auth";
|
||||
import { NewsletterStatus } from "@/lib/generated/prisma/enums";
|
||||
|
||||
async function getAuthorId() {
|
||||
const session = await auth(); // returns valid session object
|
||||
|
||||
if (!session?.user?.id) { // if no session, user, or id
|
||||
throw new Error("You must be logged in");
|
||||
}
|
||||
|
||||
return session.user.id;
|
||||
}
|
||||
|
||||
export async function getPubNewsletters() {
|
||||
return await prisma.newsletters.findMany({
|
||||
where: {
|
||||
status: "Sent",
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
include: { // includes author
|
||||
author: {
|
||||
select: {
|
||||
name: true, // only takes authors name not private info
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPubNewsletter(id: string) { // single pub newsletter
|
||||
return await prisma.newsletters.findUnique({
|
||||
where: {
|
||||
id,
|
||||
status: "Sent",
|
||||
},
|
||||
include: {
|
||||
author: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserNewsletters() {
|
||||
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
return await prisma.newsletters.findMany({
|
||||
where: {
|
||||
authorID,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc", // newest first
|
||||
},
|
||||
include: { // includes author
|
||||
author: {
|
||||
select: {
|
||||
name: true, // only takes authors name not private info
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserNewsletter(id: string) {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
return await prisma.newsletters.findUnique({
|
||||
where: {
|
||||
id,
|
||||
authorID,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function publishNewsletter(id: string) {
|
||||
try {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
const newsletter = await prisma.newsletters.update({
|
||||
where: {
|
||||
id,
|
||||
authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
||||
},
|
||||
data: {
|
||||
status: "Sent",
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/dashboard");
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath(`/newsletters/${id}`);
|
||||
revalidatePath(`/dashboard/${id}`);
|
||||
|
||||
return { success: true, newsletter };
|
||||
} catch (error) {
|
||||
console.error("Error publishing newsletter:", error);
|
||||
return { success: false, error: "Failed to publish newsletter" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function unPublishNewsletter(id: string) {
|
||||
try {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
const newsletter = await prisma.newsletters.update({
|
||||
where: {
|
||||
id,
|
||||
authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
||||
},
|
||||
data: {
|
||||
status: "Draft",
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/dashboard");
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath(`/newsletters/${id}`);
|
||||
revalidatePath(`/dashboard/${id}`);
|
||||
|
||||
return { success: true, newsletter };
|
||||
} catch (error) {
|
||||
console.error("Error unpublishing newsletter:", error);
|
||||
return { success: false, error: "Failed to unpublish newsletter" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function scheduleNewsletter(id: string) {
|
||||
try {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
const newsletter = await prisma.newsletters.update({
|
||||
where: {
|
||||
id,
|
||||
authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
||||
},
|
||||
data: {
|
||||
status: "Schedule",
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/dashboard");
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath(`/newsletters/${id}`);
|
||||
revalidatePath(`/dashboard/${id}`);
|
||||
|
||||
return { success: true, newsletter };
|
||||
} catch (error) {
|
||||
console.error("Error publishing newsletter:", error);
|
||||
return { success: false, error: "Failed to publish newsletter" };
|
||||
}
|
||||
}
|
||||
|
||||
// export async function likeNewsletter(id: string) {
|
||||
// try {
|
||||
// const authorID = await getAuthorId();
|
||||
|
||||
// const newsletter = await prisma.newsletters.update({
|
||||
// where: {
|
||||
// id,
|
||||
// authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
||||
// },
|
||||
// data: {
|
||||
// status: "Sent",
|
||||
// },
|
||||
// });
|
||||
|
||||
// revalidatePath("/dashboard");
|
||||
// revalidatePath("/newsletters");
|
||||
// revalidatePath(`/newsletters/${id}`);
|
||||
// revalidatePath(`/dashboard/${id}`);
|
||||
|
||||
// return { success: true, newsletter };
|
||||
// } catch (error) {
|
||||
// console.error("Error publishing newsletter:", error);
|
||||
// return { success: false, error: "Failed to publish newsletter" };
|
||||
// }
|
||||
// }
|
||||
|
||||
export async function addNewsletter(data: {
|
||||
title: string;
|
||||
imageUrl: string;
|
||||
content: string;
|
||||
}
|
||||
) {
|
||||
try {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
const newNewsletter = await prisma.newsletters.create({
|
||||
data: {
|
||||
title: data.title.trim(),
|
||||
imageUrl: data.imageUrl.trim(),
|
||||
content: data.content, // keep spaces
|
||||
authorID,
|
||||
}
|
||||
})
|
||||
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath("/dashboard");
|
||||
|
||||
return { success: true, newsletter: newNewsletter };
|
||||
} catch (error) {
|
||||
console.error("Error adding newsletter:", error);
|
||||
return { success: false, error: "Failed to add newsletter" };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export async function editNewsletter(
|
||||
id: string,
|
||||
data: {
|
||||
title: string;
|
||||
imageUrl: string;
|
||||
content: string;
|
||||
},
|
||||
) {
|
||||
try {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
const updatedNewsletter = await prisma.newsletters.update({
|
||||
where: {
|
||||
id, // find target newsletter
|
||||
authorID, // thats also owned by current session owner
|
||||
status: {
|
||||
not: "Sent"
|
||||
}
|
||||
},
|
||||
data: {
|
||||
title: data.title.trim(),
|
||||
imageUrl: data.imageUrl.trim(),
|
||||
content: data.content,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath("/dashboard");
|
||||
revalidatePath(`/newsletters/${id}`);
|
||||
revalidatePath(`/dashboard/${id}`);
|
||||
|
||||
return { success: true, newsletter: updatedNewsletter };
|
||||
} catch (error) {
|
||||
console.error("Error editing newsletter:", error);
|
||||
return { success: false, error: "Failed to edit newsletter" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeNewsletter(id: string) {
|
||||
try {
|
||||
const authorID = await getAuthorId();
|
||||
|
||||
const deletedNewsletter = await prisma.newsletters.delete({
|
||||
where: {
|
||||
id,
|
||||
authorID,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath("/dashboard");
|
||||
revalidatePath(`/newsletters/${id}`);
|
||||
revalidatePath(`/dashboard/${id}`);
|
||||
|
||||
return { success: true, newsletter: deletedNewsletter };
|
||||
} catch (error) {
|
||||
console.error("Error deleting newsletter:", error);
|
||||
return { success: false, error: "Failed to delete newsletter" };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "../lib/prisma";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function addUser(data: {
|
||||
name?: string;
|
||||
email: string;
|
||||
pass: string;
|
||||
}) {
|
||||
try {
|
||||
const newUser = await prisma.users.create({ // creates a new user from parameter object data
|
||||
data: {
|
||||
name: data.name,
|
||||
email: data.email,
|
||||
passwordHash: data.pass,
|
||||
},
|
||||
});
|
||||
revalidatePath("/users"); // next function informs changes made to page so refreshes cached data
|
||||
return { success: true, user: newUser }; // returns object containing status and object returned from prisma.users.create
|
||||
} catch (error) { // if it fails return error
|
||||
console.error("Error adding user:", error);
|
||||
return { success: false, error: "Failed to add user" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function editUser(
|
||||
id: string,
|
||||
data: {
|
||||
name?: string;
|
||||
email?: string;
|
||||
pass?: string;
|
||||
},
|
||||
) {
|
||||
try {
|
||||
const updatedUser = await prisma.users.update({
|
||||
where: { id }, // shorthand for where: { id: id } - finds user by id in table matching id parameter
|
||||
data: {
|
||||
...(data.name && { name: data.name }), // if data.name exists then update name to data.name
|
||||
...(data.email && { email: data.email }), // likely as prisma cant update a field with an object it must be a property value of data object
|
||||
...(data.pass && { passwordHash: data.pass }), //. if data.name is empty (false) the expression is false and spread will crteate an empty object which will not update the name field in the database
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/users");
|
||||
return { success: true, user: updatedUser };
|
||||
} catch (error) {
|
||||
console.error("Error editing user:", error);
|
||||
return { success: false, error: "Failed to edit user" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeUser(id: string) { // this should be clear from the above explanations
|
||||
try {
|
||||
const deletedUser = await prisma.users.delete({
|
||||
where: { id },
|
||||
});
|
||||
revalidatePath("/users");
|
||||
revalidatePath("/newsletters");
|
||||
revalidatePath("/dashboard");
|
||||
return { success: true, user: deletedUser };
|
||||
} catch (error) {
|
||||
console.error("Error deleting user:", error);
|
||||
return { success: false, error: "Failed to delete user" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUsers() {
|
||||
return await prisma.users.findMany({
|
||||
orderBy: { createdAt: "desc" } // changed from lab code as my id's are string - newest first
|
||||
}); // returns all users in the table in descending order of id
|
||||
}
|
||||
Reference in new issue
Block a user