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,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