Skip to content

Commit

Permalink
Database actions WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowens20832 committed Jun 11, 2024
1 parent 0e097b4 commit f7ecc8f
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dist/
.vscode/
packages/elegant-ui/node_modules/
node_modules/
node_modules/
source/elegant/src/utils/PrismaRepository.ts
28 changes: 10 additions & 18 deletions source/elegant/db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,34 @@ model User {
role String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
posts Post[]
}

model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
}

model Collection {
id String @id @default(cuid())
name String
id String @id @default(cuid())
title String
description String?
coverImage String?
createdAt DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Post Post? @relation(fields: [postId], references: [id])
postId String?
}

model Post {
id String @id @default(cuid())
id String @id @default(cuid())
title String
status Status
description String
coverImage String?
content String?
author User @relation(fields: [authorId], references: [id])
author User @relation(fields: [authorId], references: [id])
authorId String
slug String?
collections Collection[]
tags String[]
createdAt DateTime
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

enum Status {
Expand Down
3 changes: 1 addition & 2 deletions source/elegant/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const createJestConfig = nextJest({
const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
setupFilesAfterEnv: ['<rootDir>/src/utils/mocks/prisma-singleton.ts'],
moduleNameMapper: {
'@/auth': '<rootDir>/src/utils/mocks/auth.ts',
'next-auth/providers/credentials':
Expand Down
14 changes: 13 additions & 1 deletion source/elegant/src/components/Admin/Pages/Onboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function Onboard() {
<form
className="space-y-6 text-left"
action={async (data: FormData) => {
createAdmin(data).then((res: any) => {
createAdminUser(data).then((res: any) => {
router.refresh();
}).catch((error) => {
console.log(error)
Expand Down Expand Up @@ -99,4 +99,16 @@ export default function Onboard() {
</div>
</main>
);
}

export async function createAdminUser(formData: FormData) {
const name = formData.get("name") as string;
const email = formData.get("email") as string;
const password = formData.get("password") as string;

return await createAdmin({
name: name,
email: email,
password: password
});
}
36 changes: 11 additions & 25 deletions source/elegant/src/utils/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ import { signIn } from "./Auth";
import { hashPassword } from "./Bcrypt";
import prisma from "./Prisma";

export async function createAdmin(formData: FormData) {
const name = formData.get("name") as string;
const email = formData.get("email") as string;
const password = formData.get("password") as string;
export interface createAdmin {
name: string;
email: string;
password: string;
};

export async function createAdmin(user: createAdmin) {

try {
await prisma.user.create({
data: {
name: name,
email: email,
password: await hashPassword(password),
name: user.name,
email: user.email,
password: await hashPassword(user.password),
role: "admin"
}
});

const response = await signIn('credentials', formData);
const response = await signIn('credentials', user);

return response;
}
Expand Down Expand Up @@ -56,21 +59,4 @@ export async function getAdminCount() {
});

return response.length;
}

export async function createCollection() {
try {
const response = await prisma.collection.create({
data: {

}
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}
Empty file.
76 changes: 76 additions & 0 deletions source/elegant/src/utils/Db/Actions/Collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use server'

import prisma from "@/utils/Prisma";

export interface CreateCollection {
title: string;
coverImage: string;
};

export async function createCollection(collection: CreateCollection) {
try {
const response = await prisma.collection.create({
data: collection
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}

export interface GetCollectionByName {
title: string;
};

export async function getCollectionByName(collection: GetCollectionByName) {

};

export interface UpdateCollection {
id: string;
title: string;
coverImage: string;
};

export async function updateCollection(collection: UpdateCollection) {
try {
const response = await prisma.collection.update({
where: {
id: collection.id,
},
data: collection
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}

export interface DeleteCollection {
id: string;
}

export async function deleteCollection(collection: DeleteCollection) {
try {
const response = await prisma.collection.delete({
where: {
id: collection.id,
}
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}
97 changes: 97 additions & 0 deletions source/elegant/src/utils/Db/Actions/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use server'

import prisma from "@/utils/Prisma";

export interface CreatePost {
title: string;
status: "DRAFT" | "PUBLISHED";
description: string;
coverImage: string;
content: string;
authorId: string;
slug: string;
collection: string;
tags: string[];
};

export async function createPost(post: CreatePost) {
try {
const response = await prisma.post.create({
data: post
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}

export interface GetPostById {
id: string;
}

export async function getPostById(post: GetPostById) {
try {
const response = await prisma.post.findUnique({
where: {
id: post.id,
}
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}

export interface GetPostByTitle {
name: string;
}

export async function getPostByTitle(post: GetPostByTitle) {
try {
const response = await prisma.post.findFirst({
where: {
title: post.name,
}
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}

export interface UpdatePost {
id: string;
}

export async function updatePost(post: UpdatePost) {
try {
const response = await prisma.post.findFirst({
where: {
id: post.id,
}
});

return response;
}
catch (error: any) {
return {
error: error.message,
};
}
}

export async function deletePost() {

}
Empty file.
15 changes: 15 additions & 0 deletions source/elegant/src/utils/mocks/prisma-singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PrismaClient } from '@prisma/client';
import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended';

import prisma from './../Prisma';

jest.mock('./../Prisma', () => ({
__esModule: true,
default: mockDeep<PrismaClient>(),
}));

beforeEach(() => {
mockReset(prismaMock);
});

export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;

0 comments on commit f7ecc8f

Please sign in to comment.